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
Prashant Pandey.Prashant Pandey. 

Write a trigger to update the state as per the city on Contact. Trigger should work on insert, update. If city is C1 , State should be S1. If city is C2 , State should be S2. If city is C3 , State should be S3.

Avoid IF ELSE bcz if we have 100 or 1000 records then what we can do ?? Need Dynamic trigger or some thing new from you.

WARMS REGARD,
Prashant Pandey
Best Answer chosen by Prashant Pandey.
Maharajan CMaharajan C
Hi Prasant,

By using the below ways you can avoid the Multiple if conditions. But you have to create the data(City State combination ) in custom Setting or Metadata.

1. Use the Custom Setting to store the City, State Combination. For Storing the State in custom setting create one custom field in Custom Setting and use the Standard custom setting Name field to store the city name.

here am using Mailing State, Mailing City for example from Contact Object. Here i created the custom setting named CityState__c

Trigger :
 
trigger contactCityStateTrigger on Contact (before insert,before update) {

    Map<String,CityState__c> cityStateCSMap = CityState__c.getAll(); 
    for(contact c : trigger.new)
    {
        if(cityStateCSMap.containsKey(c.MailingCity)){
            c.MailingState = cityStateCSMap.get(c.MailingCity).State__c;
        }
    }
}


2. You can also use the Custom Metadata instead of Custtom Setting. But you can't use the dataloader for custom metadate. If you want to use the Custom Metadata then change the trigger like below:
 
Create the custom field for storing the state and city in Custom Metadata. Here i created the custom Metadata named City_State_CMD

Trigger:
trigger contactCityStateTrigger on Contact (before insert,before update) {
	List<City_State_CMD__mdt> cityStatemdtList = [Select city__c , State__c from City_State_CMD__mdt];
    Map<String,String> cityStateMap = new Map<String,String>();
    if(cityStatemdtList.size() > 0 )
    {
        for(City_State_CMD__mdt cityState : cityStatemdtList){
			cityStateMap.put(cityState.city__c, cityState.State__c);
        }
    }
    for(contact c : trigger.new)
    {
        if(cityStateMap.containsKey(c.MailingCity)){
            c.MailingState = cityStateMap.get(c.MailingCity);
        }
    }
}


Thanks,
Maharajan.C

All Answers

Christan G 4Christan G 4


Hi Prashant, I hope you are well. I am a little confused by your requirement. Is city and state on different objects? And what do you mean avoid if else? Using if else logic has nothing to do with the amount of records being processed. Also triggers should be only used when complex logic is required. You can most likely fulfill your requirement but using a process builder.
Maharajan CMaharajan C
Hi Prasant,

By using the below ways you can avoid the Multiple if conditions. But you have to create the data(City State combination ) in custom Setting or Metadata.

1. Use the Custom Setting to store the City, State Combination. For Storing the State in custom setting create one custom field in Custom Setting and use the Standard custom setting Name field to store the city name.

here am using Mailing State, Mailing City for example from Contact Object. Here i created the custom setting named CityState__c

Trigger :
 
trigger contactCityStateTrigger on Contact (before insert,before update) {

    Map<String,CityState__c> cityStateCSMap = CityState__c.getAll(); 
    for(contact c : trigger.new)
    {
        if(cityStateCSMap.containsKey(c.MailingCity)){
            c.MailingState = cityStateCSMap.get(c.MailingCity).State__c;
        }
    }
}


2. You can also use the Custom Metadata instead of Custtom Setting. But you can't use the dataloader for custom metadate. If you want to use the Custom Metadata then change the trigger like below:
 
Create the custom field for storing the state and city in Custom Metadata. Here i created the custom Metadata named City_State_CMD

Trigger:
trigger contactCityStateTrigger on Contact (before insert,before update) {
	List<City_State_CMD__mdt> cityStatemdtList = [Select city__c , State__c from City_State_CMD__mdt];
    Map<String,String> cityStateMap = new Map<String,String>();
    if(cityStatemdtList.size() > 0 )
    {
        for(City_State_CMD__mdt cityState : cityStatemdtList){
			cityStateMap.put(cityState.city__c, cityState.State__c);
        }
    }
    for(contact c : trigger.new)
    {
        if(cityStateMap.containsKey(c.MailingCity)){
            c.MailingState = cityStateMap.get(c.MailingCity);
        }
    }
}


Thanks,
Maharajan.C

This was selected as the best answer