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
SFTerrSFTerr 

Lead trigger doesn't recognize custom setting ("Variable does not exist")

Recently I've added trigger on contact populating Area_Code__c field based on the information added to contact.MailingCountry through CountryRegion__c custom setting. It works well:
trigger PopulateContactAreaCode on Contact (before insert, before update) {

    for(Contact contact : Trigger.new)
    {
        string AreaCode =  .getInstance(contact.MailingCountry).Area_Code__c;
        contact.Area_Code__c = AreaCode;
    }
}

but when I'm trying to replicate the code on lead:
trigger PopulateLeadAreaCode on Lead (before insert, before update) {

    for (Lead newLead : Trigger.new)
    {
        string AreaCode = CountryRegion__c.getInstance(Lead.Country). Area_Code__c;
        lead.Area_Code__c = AreaCode;
    }
}

it does gives me an error: "Error: Compile Error: Variable does not exist: CountryRegion__c at line 5 column 27"

CountThat CountryRegion__c custom setting is the same I've used for contacts, so not sure why the system acan't see it this time? Do I need a new separate custom setting for leads?

Thank you in advance!
StephenKennyStephenKenny
Hi there,

I dont see the CountryRegion__c used anywhere in your contact trigger. In any case, this is your issue. Either the custom setting does not exist or it is named somehting else perhaps? The error is telling you that Salesforce does not know what CountryRegion__c is.

Regards
Stephen
SFTerrSFTerr
I cutted it out while writing this question, that's the full contact trigger:
 
trigger PopulateContactAreaCode on Contact (before insert, before update) {

    for(Contact contact : Trigger.new)
    {
        string AreaCode = CountryRegion__c.getInstance(contact.MailingCountry).Area_Code__c;
        contact.Area_Code__c = AreaCode;
    }
}
and country region:

User-added image
StephenKennyStephenKenny
Hmm quite strange, the code looks good. The only thing I can see is that there is a trailing space in your lead trigger line 5 between the "." and "Area_Code__c" at the end. try removing that to see if this resolves the issue:

So 'string AreaCode = CountryRegion__c.getInstance(Lead.Country). Area_Code__c;' should be 'string AreaCode = CountryRegion__c.getInstance(Lead.Country).Area_Code__c;'