0

i have below code where i am doing 2 queries and updating the account record.

list<account> parentAccount=new list<account>();
list<account> ChildAccount=new list<account>();
parentAccount = [select id from Account where CSRId__C=:Csrnum LIMIT 1];
if(parentAccount.size()>0){
    acc.parentid=parentAccount.get(0).id;
}
else{
    "Error-parentaccount not found"
}
ChildAccount= [select id from Account where customer__c=:customer LIMIT 1];          
if(ChildAccount.size()>0){
    accountRec.ChildAccountId__c=ChildAccount.get(0).id;
}
else
  "Error-Child account not found"
}

I am using 2 soql queries on account 1 for finding account by Csrnum which is number i will provide and 2 for finding account by customer which is again different number. Please suggest.

We can do like this

List<account> accounts = [select id from Account where CSRId__C=:Csrnum or customer__c=:customer LIMIT 2];
if(accounts.size() == 2) {
    Account parentAccount;
    Account childAccount;
    for(Account a : accounts) {
        if(a.CSRId__C == Csrnum) {
           acc.parentid= a.id;
        } else{
            else{
                "Error-parent account not found"
    }
    if(a.customer__c == customer) {
        acc.ChildAccountId__c= a.id;
    } 
    else{
        "Error-child account not found"
    }
}
1

If you can guarantee that CSRID__c is only populated for Parent account and customer__c is only populated for child accounts, you can do following. Otherwise you'll have to run two queries to get parent and child accounts separately.

List<account> accounts = [select id from Account where CSRId__C=:Csrnum or customer__c=:customer LIMIT 2];
if(accounts.size() == 2) {
    Account parentAccount;
    Account childAccount;
    for(Account a : accounts) {
        if(a.CSRId__C == Csrnum) {
            parentAccount = a;
        } 
        if(a.customer__c == customer) {
            childAccount = a;
        } 
    }

    if(parentAccount != null){
        acc.parentid=parentAccount.get(0).id;
    }
    else{
        "Error-parentaccount not found"
    }

    if(ChildAccount != null){
        accountRec.ChildAccountId__c=ChildAccount.get(0).id;
    }
    else{
        "Error-Child account not found"
    }
}
  • Instead of Posting Code can you Please explain it – SFDC FAN Jul 27 '18 at 10:14
  • How you can guarantee that 2 records are each type with that SOQL query? It could be possible that 2 records return satisfying CSRId__C=:Csrnum itself – Santanu Boral Jul 27 '18 at 10:23
  • @SantanuBoral Yes you are correct!. I'll update the answer. – Ranga Jul 27 '18 at 10:34
0

Since you are using two separate conditions in the WHERE condition and also limiting the record to 1, so 2 separate queries are fine this scenario.

  • What about usage of set @Santhanu. we will collect Csrnum ,customer in one set. write single query and check containskey .will it work? – SFDC FAN Jul 27 '18 at 10:08
  • Since you are limiting 1 record for each Csrnum and customer then one query will not solve your purpose. Even you put LIMIT 2, it will not return one record for each type. Hope it makes sense – Santanu Boral Jul 27 '18 at 10:17
  • Sorry Not in One set :) different sets (In case of many records)...He specified LIMIT 1..It makes sense – SFDC FAN Jul 27 '18 at 10:39
  • can you please tell m how we can use set here.If u can give sample code then it would be good to me – sabba Jul 30 '18 at 10:34
  • As I mentioned you earlier, you need to separate SOQL queries – Santanu Boral Jul 30 '18 at 10:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.