function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
@DarkCloud@DarkCloud 

how Can I remove query in for loop And want to avoid for loop with in for loop

public static void afterInsert(List<Lead> newLeads,Map<ID,Lead> oldLeadMap){
        //insert subscription records on field update -  NCPC_Pardot_Avail_Sub_Interest_Ids__c
        subscriptionInterestRecords(newLeads,oldLeadMap);

    }
    
    
    //Process to create subscription from the indicated field update
    public static void subscriptionInterestRecords(List<Lead> newList,Map<ID,Lead> oldMap){
        try{            
            Map<Id,String> leadIdToAvailSubMap = new Map<Id,String>();
            if(schema.SObjectType.Lead.isAccessible()
               && schema.SObjectType.Lead.fields.NCPC_Pardot_Avail_Sub_Interest_Ids__c.isAccessible()){ 
                for(Lead leadRec : newList){
                       if( !String.isBlank(leadRec.NCPC_Pardot_Avail_Sub_Interest_Ids__c)){ 
                           //available subscription and interest are changing
                           leadIdToAvailSubMap.put(leadRec.Id,leadRec.NCPC_Pardot_Avail_Sub_Interest_Ids__c);
                       }
                   }
  
               if(!leadIdToAvailSubMap.isEmpty()){
                              if(schema.SObjectType.ncpc__PC_Subscription__c.isAccessible()
                      && schema.SObjectType.ncpc__PC_Interest__c.isAccessible()
                      && schema.SObjectType.ncpc__PC_Available_Subscription_Interest__c.isAccessible() 
                      && schema.SObjectType.ncpc__PC_Available_Subscription_Interest__c.fields.ncpc__Type__c.isAccessible()
                      && schema.SObjectType.ncpc__PC_Subscription__c.fields.ncpc__Contact__c.isAccessible()
                      && schema.SObjectType.ncpc__PC_Interest__c.fields.ncpc__Contact__c.isAccessible()
                     ){

                             for(Id leadId : leadIdToAvailSubMap.keySet()){
                                 List<String>  availSubscriptionInterestList = leadIdToAvailSubMap.get(leadId).split(',');
                                 List<ncpc__PC_Subscription__c> addSubscriptionList = new List<ncpc__PC_Subscription__c>();
                                 List<ncpc__PC_Interest__c> addInterestList = new List<ncpc__PC_Interest__c>();
                                 Set<Id> subsIdSet = new Set<Id>();
                                 Set<Id> interestIdSet = new Set<Id>();
                                 
                                 if(availSubscriptionInterestList.size()>0){
                                     for(ncpc__PC_Available_Subscription_Interest__c avail : [SELECT Id, ncpc__Type__c  FROM ncpc__PC_Available_Subscription_Interest__c 
                                                                                              WHERE Id  IN : availSubscriptionInterestList ]){
                                                                                                  if(avail.ncpc__Type__c == 'Subscription'){
                                                                                                      subsIdSet.add(avail.Id);
                                                                                                  }
                                                                                                  if(avail.ncpc__Type__c == 'Interest'){
                                                                                                      interestIdSet.add(avail.Id);
                                                                                                  }
                                                                                              }
                                     //subscriptionList
                                     for( ncpc__PC_Subscription__c sub : [SELECT Id, ncpc__Lead__c,ncpc__Opt_In__c, ncpc__Related_Subscription_Interest__c FROM ncpc__PC_Subscription__c WHERE ncpc__Lead__c =: leadId]){
                                                                              if(subsIdSet.contains(sub.ncpc__Related_Subscription_Interest__c)){
                                                                                  if(sub.ncpc__Opt_In__c == false){
                                                                                      ncpc__PC_Subscription__c s = new ncpc__PC_Subscription__c (Id=sub.Id);
                                                                                      s.ncpc__Opt_In__c = true;
                                                                                      s.ncpc__Opt_In_Date__c = Date.today();
                                                                                      addSubscriptionList.add(s);
                                                                                  }
                                                                                  //do nothing -- don't process this record and remove from set
                                                                                  subsIdSet.remove(sub.ncpc__Related_Subscription_Interest__c);
                                                                              }
                                                                          }
                                     if(subsIdSet.size()>0){
                                         //for remaining subscriptions
                                         for(Id sub : subsIdSet){
                                             ncpc__PC_Subscription__c recordSub = new ncpc__PC_Subscription__c();
                                             recordSub.ncpc__Opt_In__c = true;
                                             recordSub.ncpc__Lead__c = leadId;
                                             recordSub.ncpc__Opt_In_Source__c = 'Pardot Form';
                                             recordSub.ncpc__Related_Subscription_Interest__c = sub;
                                             addSubscriptionList.add(recordSub);
                                         }
                                     }
                                     
                                     
                                     //interestList
                                     for( ncpc__PC_Interest__c sub : [SELECT Id, ncpc__Lead__c, ncpc__Selected__c, ncpc__Interest_Selected__c  FROM ncpc__PC_Interest__c WHERE ncpc__Lead__c =: leadId]){
                                                                          if(interestIdSet.contains(sub.ncpc__Interest_Selected__c)){
                                                                              if(sub.ncpc__Selected__c == false){
                                                                                  ncpc__PC_Interest__c a = new ncpc__PC_Interest__c(Id=sub.Id);
                                                                                  a.ncpc__Selected__c = true;
                                                                                  a.ncpc__Captured_Date__c = Date.today();
                                                                                  addInterestList.add(a);
                                                                              }
                                                                              //do nothing -- don't process this record and remove from set
                                                                              interestIdSet.remove(sub.ncpc__Interest_Selected__c);
                                                                          }
                                                                      }
                                     if(interestIdSet.size()>0){
                                         //for remaining interest
                                         for(Id ins : interestIdSet){
                                             ncpc__PC_Interest__c recordSub = new ncpc__PC_Interest__c();
                                             recordSub.ncpc__Selected__c = true;
                                             recordSub.ncpc__Lead__c = leadId;
                                             recordSub.ncpc__Captured_Date__c = Date.today();
                                             recordSub.ncpc__Interest_Selected__c = ins;
                                             addInterestList.add(recordSub);
                                         }
                                     }
                                     
                                     if(addSubscriptionList.size()>0){
                                         upsert addSubscriptionList;
                                     }
                                     
                                     if(addInterestList.size()>0){
                                         upsert addInterestList;
                                     }
                                 }
                             }
                         }
                     }
               }
        }catch(Exception e){
            //catch error in some object
            System.debug(+e.getMessage() + e.getStackTraceString());
        }finally{
            //blank out NCPC_Pardot_Avail_Sub_Interest_Ids__c to handle optouts and field not getting changed
            List<Lead> updateLeadList = new List<Lead>();
            for(Lead l : newList){
                //update the field to blank once the subscription/interest records got created
                if(String.isNotBlank(l.NCPC_Pardot_Avail_Sub_Interest_Ids__c)){
                    Lead lnew = new Lead(Id=l.Id,NCPC_Pardot_Avail_Sub_Interest_Ids__c='');
                    updateLeadList.add(lnew);
                }
            }
            try{
                if(updateLeadList.size()>0)
                    update updateLeadList;
            }catch(Exception e){ System.debug(e.getStackTraceString() + ' ' + e.getMessage() );}
        }
    }
}
AbhishekAbhishek (Salesforce Developers) 
Your query is answered here,

https://developer.salesforce.com/forums/?id=9060G0000005cmiQAA

Try the code snippet as mentioned above.

I hope you find the above information is helpful. If it does, please mark as Best Answer to help others too.

Thanks.
@DarkCloud@DarkCloud
Hi Abhishek,

I've gone through that post before but I'm unable to bulkify the code, needs help if you help it out!

Thanks,
AbhishekAbhishek (Salesforce Developers) 
Let me try If I find anything I will let you.
@DarkCloud@DarkCloud
sure Abhishek, Thank you!