特殊时期,大家一定要保重身体。增强自身免疫力,一切都会过去,一起加油!!!

Apex+VisualforceでAccountオブジェクトのCRUDとページングクエリー関連リストないAccountをクエリする(001)

Salesforce zchao 1060℃ 0评论

取引先(Account)の関連リストはいっぱいありでしょう。(カスタムとおなじ)

目的として、関連リストないAccountをクエリしたいと思います。

例えば、Accountの関連リスト:取引先責任者(Contact)、ケース(Case)、商談(Opportunity )三つが全部なしのAccountを取得する

方法1:

準備:SOQL、Salesforce inspectorツール、Excel

親Objectから、子Objectのレコードを取得

詳しくこちら⇒

主従関係/参照関係にある子から親/親から子を参照SOQLで取得するー子リレーション

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 演算子を使用する代わりに、適切な準結合または反結合形式でクエリを記述します。

比較演算子:https://developer.salesforce.com/docs/atlas.ja-jp.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_comparisonoperators.htm

 

以下の通りです。

開発コンソール⇒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レコードですね。

 

以上となります。

何か問題とかコメント頂れば嬉しいです

 

 

转载请注明:zchao博客之家 » Apex+VisualforceでAccountオブジェクトのCRUDとページングクエリー関連リストないAccountをクエリする(001)

喜欢 (3)or分享 (0)

您必须 登录 才能发表评论!