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
seattle_userseattle_user 

Need help with apex code - bulkify opportunity trigger

Hi There,

This trigger is suppose to loop through an opportunity's contact roles and concatenate all the roles into a string and put it in the opportunity. The code works but it runs into too many soql queries limit 101.  I think I need to use a map that holds opportunity IDs and contactRolesString but I'm not able to get it working.

Thanks!


_____________________start of trigger code______________

trigger Opt_ContactRoles_REQ on Opportunity (before insert, before update) {

    if(Trigger.isUpdate || Trigger.isInsert) {
      for(Opportunity opt : trigger.new) {
            String s = '';
          
          
            for(OpportunityContactRole ocr: [SELECT role FROM OpportunityContactRole WHERE OpportunityId =: opt.id])
            {  
                if(ocr.role != null) {
                if(! s.contains(ocr.Role)){
                    if(String.isBlank(s))
                    {
                     s = ocr.Role;
                    }else{
                        s = ocr.Role + ', ' + s ;
                    }
                }
                }
              
            } 
           opt.ContactRoles__c = s;
              
        }
    }
}
Phillip SouthernPhillip Southern
Hi, so high level...anytime opportunity is updated or inserted...you want to loop through all the contact roles and compile a comma seperate string of the roles?

So what we want to do is get all the id's of the opportunity and then do one query to get all the opportunity contact roles and put them in a map to call them later with the compiled values.....so that could be done like this:  (typed freehand, please excuse typos, missed commas, etc)

trigger Opt_ContactRoles_REQ on Opportunity (before insert, before update) {

    if(Trigger.isUpdate || Trigger.isInsert) {

      Set<Id> oppscope = new Set<Id>();
      for(Opportunity opt : trigger.new) {
         oppscope.add(opt.Id);
      }

       Map<Id,String> ocrRolesbyOppId = new Map<Id,String>();
       for(OpportunityContactRole ocr : [Select Role, OpportunityId from OpportunityContactRole Where OpportunityId IN :oppscope])
       {
            if(ocrRolesbyOppId.containskey(ocr.opportunityid))
            {
                string s = ocrolesbyoppid.get(ocr.opportunityid);
                s += ', ' + ocr.Role;
                ocrrolesbyoppid.put(ocr.opportunityid,s);
            }else
            {
                  ocrrolesbyoppid.put(ocr.opportunityid,ocr.Role);
             }
         }

         for(opportunity opp : trigger.new)
         {
             if(ocrrolesbyoppid.containskey(opp.Id))
             {
                   opp.contactroles__c = ocrrolesbyoppid.get(opp.Id);
              }
          }
      }
  }
        
seattle_userseattle_user
Thanks Philip Southern!
Phillip SouthernPhillip Southern
No problem!