0

I am working on one trigger which will throw error messages on save button but the sequence of error messages is not working perfectly.

My trigger is on lead object, on lead if Implied_Consent__c = true and Implied_Consent_Source__c isnull then I have one error message which should fire first and I have one more error message to check if map is null, ifmap is null then error message should come.

But regardless of fields, I am getting error message of map only first.

Can anyone helps me out in this issue.

my code is below.

if(lead.Implied_Consent__c == true && (string.isEmpty(lead.Implied_Consent_Source__c))){
        lead.addError('Missing value for "Implied Consent Source Field"');
    }
    else if(lead.Implied_Consent_Date__c == null && lead.Implied_Consent__c == true){
        lead.Implied_Consent_Date__c = system.now();
    }
    if(!impliedsource.isEmpty()){
        for(Implied_Consent_Period__mdt metadata1 :[select MasterLabel,Consent_Period__c from Implied_Consent_Period__mdt where MasterLabel IN: impliedsource]){
            metadatamap.put(metadata1.MasterLabel,metadata1);
        }
    //for(Lead lead1: leads){
        if(metadatamap.containskey(lead.Implied_Consent_Source__c)){
            //integer var = integer.valueof(metadatamap.get(lead1.Implied_Consent_Source__c).Consent_Period__c);
            lead.Implied_Consent_Expiry__c = system.now().addMonths(integer.valueOf(metadatamap.get(lead.Implied_Consent_Source__c).Consent_Period__c));
            lead.CASL_Opt_In__c = true;
        }
    }
        else {
            lead.addError('Missing Consent Period for selected Implied Consent Source');
        }
    //}

}

from the above code, if I select Implied_Consent__cfirld and left Implied_Consent_Source__c then I am getting error message as of map is not null one only.

0

Possible problem could be here

(string.isEmpty(lead.Implied_Consent_Source__c)

String.isEmpty() function doesn't check for whitespaces, try with String.isBlank().

Secondly, put System.debug() before each condition to check for their respective value, for debugging your code if the above doesn't work.

e.g. System.debug('Implied Source::' + lead.Implied_Consent_Source__c);

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.