0

I am getting an error on below class but not able to find where exactly the issue is happening.

"CASLonLead : CASLonLead: System.LimitException: Apex CPU time limit exceeded"

public class LeadCASLClass {
public static boolean isStartAccount = false;
public static void lead(List<Lead> leads){
    for(Lead lead: leads){
        if(lead.Express_Consent__c == false && lead.Implied_Consent__c == false){
            lead.HasOptedOutOfEmail = TRUE;
            lead.CASL_Opt_In__c = false;
            lead.CASL_Opt_In_Status__c = 'Removed';
            lead.CASL_Opt_Out_Reason__c = 'None';
        }
        else if(lead.Express_Consent__c == true && string.isEmpty(lead.Express_Consent_Source__c )){
            lead.addError('Missing Value for "Express Consent Source Field"');
        }
        else if(lead.Express_Consent__c == true && string.isNotEmpty(lead.Express_Consent_Source__c )){
            if(lead.Express_Consent_Date__c == null){
                lead.Express_Consent_Date__c = system.now();
            }
            lead.CASL_Opt_In__c = true;
            lead.CASL_Opt_In_Status__c = 'Express';
            lead.CASL_Opt_Out_Reason__c = 'None';
            lead.HasOptedOutOfEmail = false;
            if(lead.CASL_Opt_In_Date__c == null){
                lead.CASL_Opt_In_Date__c = system.now();
            }
        }
    }
}
public static void implied(List<Lead> allLeads){
    //set to store the Implied Consent Source value
    set<string> impliedsource = new set<string>();
    //Map to fetch the custom metadata for select implied consent source
    map<string,Implied_Consent_Period__mdt> metadatamap = new map<string,Implied_Consent_Period__mdt>();
    for(Lead impliedlead : allLeads){
        if(impliedlead.Express_Consent__c == false && impliedlead.Implied_Consent__c == false){
            impliedlead.HasOptedOutOfEmail = true;
            impliedlead.CASL_Opt_In__c = false;
            impliedlead.CASL_Opt_In_Status__c = 'Removed';
            if(string.isEmpty(impliedlead.CASL_Opt_Out_Reason__c = null)){
                impliedlead.CASL_Opt_Out_Reason__c = 'None';
            }
        }
        else if(impliedlead.Implied_Consent__c == true && string.isEmpty(impliedlead.Implied_Consent_Source__c)){
            impliedlead.addError('Missing value for "Implied Consent Source Field"');
        }
        else if(impliedlead.Implied_Consent__c == true && string.isNotEmpty(impliedlead.Implied_Consent_Source__c)){
            impliedsource.add(impliedlead.Implied_Consent_Source__c);
            if(impliedlead.Implied_Consent_Date__c == null){
                impliedlead.Implied_Consent_Date__c = system.now();
            }
            if(!impliedsource.isEmpty()){
                for(Implied_Consent_Period__mdt metadata :[select MasterLabel,Consent_Period__c from Implied_Consent_Period__mdt where MasterLabel IN: impliedsource]){
                metadatamap.put(metadata.MasterLabel, metadata);
                }
                if(metadatamap.containsKey(impliedlead.Implied_Consent_Source__c)){
                    if(integer.valueOf(metadatamap.get(impliedlead.Implied_Consent_Source__c).Consent_Period__c) != null){
                    impliedlead.Implied_Consent_Expiry__c = impliedlead.Implied_Consent_Date__c.addMonths(integer.valueOf(metadatamap.get(impliedlead.Implied_Consent_Source__c).Consent_Period__c));
                    //}
                    impliedlead.CASL_Opt_In__c = true;
                    integer var = integer.valueOf(metadatamap.get(impliedlead.Implied_Consent_Source__c).Consent_Period__c);
                    if(var == 6){
                        impliedlead.CASL_Opt_In_Status__c = 'Implied – 6 months';
                    }
                    else{
                        impliedlead.CASL_Opt_In_Status__c = 'Implied – 2 years';
                    }
                    impliedlead.CASL_Opt_Out_Reason__c = 'None';
                    impliedlead.HasOptedOutOfEmail = false;
                        if(impliedlead.CASL_Opt_In_Date__c == null){
                            impliedlead.CASL_Opt_In_Date__c = system.now();
                        }
                }
                //}

            else if(integer.valueOf(metadatamap.get(impliedlead.Implied_Consent_Source__c).Consent_Period__c) == null && string.isNotEmpty(impliedlead.Implied_Consent_Source__c)){
                impliedlead.AddError('Missing Consent Period for selected Implied Consent Source');
            }
                }
                }
        }
    }
}
 }

Can anyone help me out in this issue please.

  • Where are you invoking this Class if it is in a trigger please try to check if your trigger is being fired more than once... if it is due to trigger recursion then You can avoid the recursion by tracking it in a variable and avoiding it – Abhijith E Apr 2 at 12:48
  • A debug log would the best resource to find what is taking long. It might be that the problem is not with this class but somewhere else. Can you post the log here – manjit5190 Apr 2 at 14:14
  • the error is reported in this class only because that is where the execution trace happened to be when time was exceeded. The most likely culprit is earlier in the code stack/sequence where non-optimized operations are occurring. Dev Console or IDE profiler will help here (which analyze the debug log) – cropredy Apr 2 at 14:35
0

I would suggest that this be done in batch processes since it appears that you have a large list of data. If this is from a trigger, you may want to instantiate a queueables to call that batch only 1 time. To prevent recursion, set a static variable when it has been called to notify triggers in the same context that you've already done this. It will reset after the trigger context has expired. As of this posting, it is not fully available yet to track the job status without a custom poller to check the status.

Assuming this is being done in a job, if you created a field/object to track the jobs failures for reporting on the job, you could create a component to view the job results in a data table.

| improve this answer | |

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.