0

I have requirement where on Case Update if ContactId in same of accounts contact associated with that case then needs to throw an error.

Example; Case 1 AccountId 1 and contactId 1

If case 1(accounts contacts contain -->contactId 1) then error.

I have written code which is working fine ,but just wanted to what is the other way if we need to remove ForLoop on contact. Is there any way to direclty check if contact is same and associated with case and account.

public static void CaseValidation(list<case> caseList){
    map<string,map<string,string>> casemap = new map<string,map<string,string>>();
    map<string,string> accconIds = new map<string,string>();
    for(case cs:caseList){
        accconIds.put(cs.AccountId,cs.ContactId);
        casemap.put(cs.id,accconIds);
    }
    list<contact> conlist = new list<contact>();
    map<id,list<contact>> conmap = new map<id,list<contact>>();
    for(account acc:[select id,(select id from contacts) from account where id in :accconIds.keyset()]){
        conlist =acc.contacts;
        conmap.put(acc.id,conlist);
    }  
    system.debug('conmap--->'+conmap);
        for(case c:caselist){
                for(contact con:conmap.get(c.AccountId)){
                    if(con.id == c.contactid){
                        system.debug('insider--->');
                        c.adderror('contact is associated with same account and referring same case');
                    }
                }
        } 
}
1
  • Please stop creating new Salesforce Stack Exchange accounts to post each question. Please do contact Stack Exchange using the link at the bottom of every page and request that your existing accounts be merged.
    – David Reed
    May 14 '20 at 14:38
1

Tushar,

I've gone through your code and try to put it without nested for loop. To avoid nested for loops we must make use of maps in such a way that we get everything we need from one iteration.

So for that we can make changes to accconIds value from :

accconIds.put(cs.AccountId,cs.ContactId);

to :

accconIds.put(cs.AccountId,cs.ContactId+'###'+cs.Id);

Now instead of this nested for loop :

for(case c:caselist){
    for(contact con:conmap.get(c.AccountId)){                   
        if(con.id == c.contactid){
            system.debug('insider--->');
                c.adderror('contact is associated with same account and referring same case');
        }
    }
}

Please try to write this:

List<ID> conID_caseID ;
List<Case> caseListNew = new List<Case>();
for(String accountID : conmap.keyset()){
    if(accconIds.containsKey(accountID)){
        conID_caseID = new List<ID>();
        conID_caseID = accconIds.get(accountID).split('###');
        if(conmap.get(accountID).contains(conID_caseID[0].trim())){
            system.debug('insider--->');
            Case c = new Case();
            c.id = conID_caseID[1].trim();
            c.adderror('contact is associated with same account and referring same case');
            caseListNew.add(c);
        }
     }
}

So, after the whole modification the code must look like :

public static void CaseValidation(list<case> caseList){
    map<string,map<string,string>> casemap = new map<string,map<string,string>>();
    map<string,string> accconIds = new map<string,string>();
    for(case cs:caseList){
        accconIds.put(cs.AccountId,cs.ContactId+'###'+cs.Id);
        casemap.put(cs.id,accconIds);
    }
    list<contact> conlist = new list<contact>();
    map<id,list<contact>> conmap = new map<id,list<contact>>();
    for(account acc:[select id,(select id from contacts) from account where id in :accconIds.keyset()]){
        conlist =acc.contacts;
        conmap.put(acc.id,conlist);
    }  
    system.debug('conmap--->'+conmap);
    List<ID> conID_caseID = new List<ID>();
    List<Case> caseListNew = new List<Case>();
    for(String accountID : conmap.keyset()){
        if(accconIds.containsKey(accountID)){
            conID_caseID = new List<ID>();
            conID_caseID = accconIds.get(accountID).split('###');
            if(conmap.get(accountID).contains(conID_caseID[0].trim())){
                system.debug('insider--->');
                Case c = new Case();
                c.id = conID_caseID[1].trim();
                c.adderror('contact is associated with same account and referring same case');
                caseListNew.add(c);
            }
         }
    }
}
6
  • Downvoted because this is a code dump.
    – Derek F
    May 14 '20 at 11:17
  • What do mean it's a code dump? How should I write it @DerekF?
    – Navii
    May 14 '20 at 11:22
  • @navil--- I need to add error on Case .How can i add it there
    – tushar
    May 14 '20 at 11:55
  • It's an answer that is just a block of code with no explanation as to how it works or how it differs from what OP (Original Post) currently has. The explanation is as important as the solution. Without a sufficient explanation, it's difficult for OP to understand the issue and avoid similar mistakes in the future and difficult for other people with similar (but different) problems to apply to their own situations.
    – Derek F
    May 14 '20 at 12:43
  • @DerekF thanks, I got your point. I have also make edit to my previous answer in this regards. I think now I don't deserve a downvote.
    – Navii
    May 14 '20 at 14:14

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.