0
 List<Checklist__c> newCheckLists = new List<Checklist__c>();  
 Map<Id, Account> legalStructAccount = new Map<Id, Account>([SELECT LegalStructure__c FROM Account WHERE Id IN (SELECT Account__c FROM Legitimation__c WHERE Id IN :newLegitimations)]);

        Map<Legitimation__c, List<ChecklistTemplate__c>> ligitimatCheckListTemplate = new Map<Legitimation__c, List<ChecklistTemplate__c>>();

        for (Legitimation__c eachLegitimation : newLegitimations) {

            String legalStructure = legalStructAccount.get(eachLegitimation.Account__c).LegalStructure__c;
            ligitimatCheckListTemplate.put(eachLegitimation, [SELECT Name FROM ChecklistTemplate__c WHERE Entity__c ='legitimation' AND LegalStrucure__c includes (:legalStructure)]);
       }


        for (Legitimation__c eachLegitimationKey : ligitimatCheckListTemplate.keyset()){
             for (ChecklistTemplate__c eachChecklistTemplate : ligitimatCheckListTemplate.get(eachLegitimationKey)){
                Checklist__c checkList = new Checklist__c(
                    Name = eachChecklistTemplate.Name,
                    TemplateId__c = eachChecklistTemplate.Id,
                    LegitimationId__c = eachLegitimationKey.Id
                );
                newCheckLists.add(checkList);
            }
        }

    insert newCheckLists;
    }
0

Added two section for both INCLUDE and IN

 List<Checklist__c> newCheckLists = new List<Checklist__c>();  
 Map<Id, Account> legalStructAccount = new Map<Id, Account>([SELECT LegalStructure__c 
                                                            FROM Account 
                                                            WHERE Id IN (SELECT Account__c FROM Legitimation__c WHERE Id IN :newLegitimations)]);

 Map<string,List<ChecklistTemplate__c>> ChecklistTemplateMap = new Map<string,List<ChecklistTemplate__c>>();

If you want to use IN Clause:

    // if you use IN Clause Block----Start
List<String> legalList = new List<String>(); 
for (Legitimation__c eachString : newLegitimations) {
    legalList.add(legalStructAccount.get(eachString.Account__c).LegalStructure__c);
}
For(ChecklistTemplate__c ckhList:[SELECT  Id,LegalStrucure__c,Name FROM ChecklistTemplate__c WHERE Entity__c ='legitimation' AND LegalStrucure__c IN : legalList]){
        if (ChecklistTemplateMap.containsKey(ckhList.LegalStrucure__c))
           {
             ChecklistTemplateMap.get(ckhList.LegalStrucure__c). add(ckhList);
           }
        else{
             ChecklistTemplateMap.put(ckhList.LegalStrucure__c,ckhList);
           }
       }
// if you use IN Clause Block----End

If you want to use INCLUDE Clause

// if you use INCLUDES Clause Block----Start
   string [] legalList = new List<String>();
   String legalString='';
    for (Legitimation__c eachString : newLegitimations) {
       for(String st : checkmap.get(eachLegitimation).split(';')){
           legalList.add('\''+ st + '\'');
     }       
}
system.debug('legalList---'+legalList);
String query = 'SELECT  Id,LegalStrucure__c,Name FROM ChecklistTemplate__c WHERE Entity__c ='legitimation' AND LegalStrucure__c INCLUDES(' + String.join(legalList, ',') + ')';
For(ChecklistTemplate__c ckhList: Database.query(query)){
        if (ChecklistTemplateMap.containsKey(ckhList.LegalStrucure__c))
           {
             ChecklistTemplateMap.get(ckhList.LegalStrucure__c).add(ckhList);
           }
        else{
             ChecklistTemplateMap.put(ckhList.LegalStrucure__c,ckhList);
           }
       }
// if you use INCLUDES Clause Block----End

   Map<Legitimation__c, List<ChecklistTemplate__c>> ligitimatCheckListTemplate = new Map<Legitimation__c, List<ChecklistTemplate__c>>();

        for (Legitimation__c eachLegitimation : newLegitimations) {

        String legalStructure = legalStructAccount.get(eachLegitimation.Account__c).LegalStructure__c;
        ligitimatCheckListTemplate.put(eachLegitimation,ChecklistTemplateMap.get(legalStructure));
   }
   for (Legitimation__c eachLegitimationKey : ligitimatCheckListTemplate.keyset()){
         for (ChecklistTemplate__c eachChecklistTemplate : ligitimatCheckListTemplate.get(eachLegitimationKey)){
            Checklist__c checkList = new Checklist__c(
                Name = eachChecklistTemplate.Name,
                TemplateId__c = eachChecklistTemplate.Id,
                LegitimationId__c = eachLegitimationKey.Id
            );
            newCheckLists.add(checkList);
        }
    }

    insert newCheckLists;
  • May I know why did you inlcude ** in between ? – user3920709 Apr 28 at 19:47
  • just trying make it bold thats it – Vidhyasagaran Muralidharan Apr 28 at 19:48
  • Thank you but I am using includes but as it is a string it is not matching my legalstructures like for example in legalstructure I get A, A,B and C but when I string it..I only get C but not the rest . Something like this, this is my problem and I cannot hard code the values inside like Includes(A,B) too. You check this for reference developer.salesforce.com/forums/?id=906F00000008wp9IAA – user3920709 Apr 28 at 19:57
  • where you are using include? in this statement SELECT Id,LegalStrucure__c,Name FROM ChecklistTemplate__c (Apply some filter if you can to reduce dataset) – Vidhyasagaran Muralidharan Apr 28 at 20:19
  • yes. I am doing it in the checklist template SOQL – user3920709 Apr 28 at 20:21
0

If you iterate over your newLegitimations list twice, you can collect all the legal structures contained within the list, store them, then query all of your relevant checklist templates at once, map them based on their legal structure and then call from that map on the second iteration of newLegitmitations.

List<Checklist__c> newCheckLists = new List<Checklist__c>();  
Map<Id, Account> legalStructAccount = new Map<Id, Account>([SELECT LegalStructure__c FROM Account WHERE Id IN (SELECT Account__c FROM Legitimation__c WHERE Id IN :newLegitimations)]);
Map<Legitimation__c, List<ChecklistTemplate__c>> ligitimatCheckListTemplate = new Map<Legitimation__c, List<ChecklistTemplate__c>>();
Map<String, List<ChecklistTemplate__c>> checklistMap = new Map<String,List<ChecklistTemplate__c>>();
String legalStructures = '';
    for (Legitimation__c eachLegitimation : newLegitimations) {
        String legalStructure = legalStructAccount.get(eachLegitimation.Account__c).LegalStructure__c;
        legalStructures += legalStructure+',';
   }

   legalStructures = legalStructures.removeEnd(',');

   for(ChecklistTemplate__c template : [SELECT Name, LegalStructure__c FROM ChecklistTemplate__c WHERE Entity__c ='legitimation' AND LegalStructure__c includes (:legalStructures)]){
       if(!checklistMap.containsKey(template.legalStructure__c) checklistMap.put(template.LegalStructure__c, new List<ChecklistTemplate__c>{template});
       else checklistMap.get(template.legalStructure__c).add(template);
   }

   for(Legitimation__c eachLegitimation : newLegitimations){
       String legalStructure = legalStructAccount.get(eachLegitimation.Account__c).LegalStructure__c;
       if(checklistMap.containsKey(legalStructure)) ligitimatCheckListTemplate.put(eachLegitimation, checklistMap.get(legalStructure));
   }


    for (Legitimation__c eachLegitimationKey : ligitimatCheckListTemplate.keyset()){
         for (ChecklistTemplate__c eachChecklistTemplate : ligitimatCheckListTemplate.get(eachLegitimationKey)){
            Checklist__c checkList = new Checklist__c(
                Name = eachChecklistTemplate.Name,
                TemplateId__c = eachChecklistTemplate.Id,
                LegitimationId__c = eachLegitimationKey.Id
            );
            newCheckLists.add(checkList);
        }
    }

insert newCheckLists;
}
  • I am trying to query on multipicklist so, I am trying to put includes in SOQL statement but not in and I get the error. – user3920709 Apr 28 at 19:38
  • I've updated the code. You may have to tweak some things after you query your checklist templates, but the initial logic should get you all the records you're attempting to query – SFDC Noob Apr 28 at 20:56

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.