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.