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
Nico WrennNico Wrenn 

Help writing my first apex trigger - I have a simple use case in mind

Hello World!
I have a use case that requires a trigger and it sounds about as simple as it gets. We have an external application called BrightTALK integrated with our Salesforce org. Trouble is, we use State and Country Picklists and BrightTALK tries to push Leads into our Salesforce org with Country names that do not match the countries in our Salesforce org. As far as I know, there are two ways to tackle this issue. 1) Change the list of Countries in our Salesforce org. 2) Write a before trigger, which, if I understand correctly, could change a Country value like "China (Hong Kong)" into "China", which our org would then accept because the value matches a value in our State and Country Picklists setup.

I have never written a trigger before, and I took one programming class 10 years ago. Does anyone have any recommended places to start, sample code to look at, reference material to refer me to where I can see the methods available to me in an before trigger and learn some syntax? Any help is much appreciated.
Best Answer chosen by Nico Wrenn
venkat-Dvenkat-D
Hi Nico,
1) Assuming there are no other integrations, One other option is to use API name of picklist values. Change API name to match with the country name of  Bright TALK systems.
2) If there are other systems or code that use these names in the current system 

Create a custom metadata that has mapping between Country code in salesfroce and corresponding country code in Bright Talk
Then create a trigger on the lead object and populate sfdc country with from mapping this way you dont have to change code if there are new countries added or changed in future .

You can go through https://trailhead.salesforce.com/en/modules/custom_metadata_types/units/custom_metadata_types_overview 

and then create trigger and developer community will be able to help once you start based on solution you choose. 

All Answers

HARSHIL U PARIKHHARSHIL U PARIKH
Hi Nico,

I would suggest something like below,
 
Trigger ChangingTheCountry On Lead(Before Insert, Before Update, After UnDelete){
    
    If(Trigger.IsInsert || Trigger.IsUpdate || Trigger.IsUnDelete){
        For(Lead Ld : Trigger.New){
            If(Ld.Country != NUll)
            {
                If(Ld.Country == 'China (Hong Kong)')
                {
                    Ld.Country = 'China';
                }
                
                 If(Ld.Country == 'us')
                {
                    Ld.Country = 'United States';
                }
                
                 If(Ld.Country == 'SOMETHING')
                {
                    Ld.Country = 'SOMETHING';
                }
                
                // And so on....
            }
        }
    }
}

Trigger is also taken cared for All newly coming records, all updating records and also for the records who are getting undeleted who once had "China (Hong Kong)" as a value in country field.

Hope it helps and if it solves the question then please mark it as best answer!

venkat-Dvenkat-D
Hi Nico,
1) Assuming there are no other integrations, One other option is to use API name of picklist values. Change API name to match with the country name of  Bright TALK systems.
2) If there are other systems or code that use these names in the current system 

Create a custom metadata that has mapping between Country code in salesfroce and corresponding country code in Bright Talk
Then create a trigger on the lead object and populate sfdc country with from mapping this way you dont have to change code if there are new countries added or changed in future .

You can go through https://trailhead.salesforce.com/en/modules/custom_metadata_types/units/custom_metadata_types_overview 

and then create trigger and developer community will be able to help once you start based on solution you choose. 
This was selected as the best answer
Nico WrennNico Wrenn
Thank you both so much for your ideas! The sample code is really helpful. The custom metadata Types sounds like the best solution since updates to it don't require updates to the trigger code. I'll take a stab at it and post again if/when I run into issues.