取引先(Account)の関連リストはいっぱいありでしょう。(カスタムとおなじ)
目的として、関連リストないAccountをクエリしたいと思います。
例えば、Accountの関連リスト:取引先責任者(Contact)、ケース(Case)、商談(Opportunity )三つが全部なしのAccountを取得する
方法1:
準備:SOQL、Salesforce inspectorツール、Excel
親Objectから、子Objectのレコードを取得
詳しくこちら⇒
select id,name,phone, (select Accountid from Contacts),(select Accountid from Opportunities), (select Accountid from Cases) from account
開発コンソール⇒Query Editor⇒結果を見ることできる
Salesforce inspectorツール
https://chrome.google.com/webstore/detail/salesforce-inspector/aodjmnfhjibkcdimpodiifdjnnncaafh
Copy(Excel format)、Excelにコピーする、Excelのデータ、フィルター、一行目にして、
三つカラムContacts、Opportunities、Casesを空白にして、残るレコードは答えるということです。
纏めは:49個レコードですね。下の方法で比べてみよう。
方法2:
準備:SOQL
①select id,name,phone from account where id not in (select Accountid from Contact) and id not in (select Accountid from Opportunity)
②select id,name,phone from account where id not in (select Accountid from Contact) and id not in (select Accountid from Case)
③select id,name,phone from account where id not in (select Accountid from Opportunity) and id not in (select Accountid from Case)
④select id,name,phone from account where id not in (select Accountid from Contact) and id not in (select Accountid from Opportunity) and id not in (select Accountid from Case) 、これはダメだった。エラーメッセージを発生したみたい。
基本制限:
-
1 つの WHERE 句で IN または NOT IN ステートメントを 2 つまで使用できます。
-
準結合および反結合と NOT 演算子は一緒に使用できません。併用すると、準結合が反結合に、反結合が準結合に変換されます。NOT 演算子を使用する代わりに、適切な準結合または反結合形式でクエリを記述します。
①
②
③
④
以下の通りです。
開発コンソール⇒Debug⇒Open Execute Anonymous Window⇒
そのCodeを入力してください。
List<Account> acc = [Select id from Account ];
system.debug('acc='+acc.size());
List<Account> accountWithContact = [select id from account where id in (select Accountid from Contact)];
system.debug('関連リストContactあるAccount');
system.debug('accountWithContact ='+accountWithContact .size());
//ListとSetどちらかというと、ListでAccountのIdは重複、SetでAccountのIdは重複してない(最後の結果は関係無し)
List<String> accId = new List<String>();
Set<String> accId2 = new Set<String>();
for(Account a1 : accountWithContact){
accId.add(a1.id);
accId2.add(a1.id);
}
system.debug('accId='+accId.size());
system.debug('accId2='+accId2.size());
List<Account> accountWithOpportunity = [select id from account where id in (select Accountid from Opportunity)];
system.debug('関連リストOpportunityあるAccount');
system.debug('accountWithOpportunity='+accountWithOpportunity.size());
for(Account a2 : accountWithOpportunity){
accId.add(a2.id);
accId2.add(a2.id);
}
system.debug('accId='+accId.size());
system.debug('accId2='+accId2.size());
List<Account> accountWithCase = [select id from account where id in (select Accountid from Case)];
system.debug('関連リストCaseあるAccount');
system.debug('accountWithCase='+accountWithCase.size());
for(Account a3 : accountWithCase){
accId.add(a3.id);
accId2.add(a3.id);
}
system.debug('accId='+accId.size());
system.debug('accId2='+accId2.size());
system.debug('accIdlist='+accId);
system.debug('accIdlist='+accId2);
List<Account> accountWithoutRelatedList = [Select id,Name from Account where id not in:accId];
List<Account> accountWithoutRelatedList2 = [Select id,Name from Account where id not in:accId2];
system.debug('関連リストないのAccountのレコード ='+accountWithoutRelatedList .size());
system.debug('accountWithoutRelatedList='+accountWithoutRelatedList );
system.debug('関連リストないのAccountのレコード ='+accountWithoutRelatedList2 .size());
system.debug('accountWithoutRelatedList2='+accountWithoutRelatedList2 );
Open Logをチェックつけて、Execudeをクリックする、Debug Onlyをチェックつけて、方法1と同じ49レコードですね。
以上となります。
何か問題とかコメント頂れば嬉しいです
Latest posts by zchao (see all)
- Auraでアクションボタン作成して画面のチェックボックス項目一括処理 - 2021年4月12日
- デフォルト項目値を含むレコード作成実例説明(defaultFieldValues) - 2021年1月9日
- Salesforce のノーコード・ローコード開発 - 2020年12月31日
转载请注明:zchao博客之家 » Apex+VisualforceでAccountオブジェクトのCRUDとページングクエリー関連リストないAccountをクエリする(001)