+ Start a Discussion
SF Beginner 2019SF Beginner 2019 

apex cpu limit how to optimize 3 for loops

public static void insertSharedRecord(Map<Id,Set<Id>>conRepMap, Map<Id,Set<ID>> conCovMemIDS){

     try{  
        createShareRecords = New List<Sobject>();
         for(id agentConId: conCovMemIDS.keyset()){
               if(conRepMap.containsKey(agentConId)){
                  for(id covRecID: conCovMemIDS.get(agentConId)){
                     for(id covUsrID: conRepMap.get(agentConId)){
                         SObject shareOBJ;
                         if(ObjectName == coverageMem){
                            shareOBJ = Schema.getGlobalDescribe().get('Coverage_Member__share').newSObject();  
                          }
                         if(ObjectName == coverage){
                         shareOBJ = Schema.getGlobalDescribe().get('Coverage__share').newSObject();  
                          } 
                         shareOBJ.put('ParentId', covRecId);
                         shareOBJ.put('UserOrGroupId', covUsrID);
                         shareOBJ.put('AccessLevel', 'Read');
                         shareOBJ.put('RowCause', 'Manual');
                         createShareRecords.add(shareOBJ);
                     }                    
                  }             
               }
         }             
        
      }