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
Aqua MatrixAqua Matrix 

I want help in writing trigger.

Hi,
I had created data in the custom metadata for city and zipcode...
I want to write the apex trigger to autopopulate the cityname If I select the zipcode which s present in the custom metadata...
Could you please help me to write the trigger for the above scenario...
I had written the apex code to fetch the custom metadata
And now I want to write the trigger to autopopulate the city name if we choose the zipcode.
trigger Autopopulate on Contact (after insert) {
// When a new account is created, fill in the city and state based on zip code

           if (trigger.isAfter && trigger.isInsert) {

                      zipcodemetda.fetchAllMetadata();
               

           }

}


 
Best Answer chosen by Aqua Matrix
Anju KumarAnju Kumar
Trigger Code
 
trigger ContactTrigger on Contact (before insert) {
    if(trigger.isInsert && trigger.isBefore)
        ContactTriggerHandler.onBeforeInsert(trigger.new);

}

There are two ways out here: 
1.> If the number of records in the metadata is less then you can directly query all the metadata records.
2.> If the number of records in the metadata is more then you should get all the zipcodes first and then query the metadata.

Approach 1:
Handler Code

public class ContactTriggerHandler {
    public static void onBeforeInsert(List<Contact> newContacts){
        Map<String,String> mapZipCodeToCityName = fetchAllMetadata();
        for(Contact objContact : newContacts){
            if(mapZipCodeToCityName.containsKey(objContact.ZipCodeFieldName__c))
                objContact.CityFieldName__c = mapZipCodeToCityName.get(objContact.ZipCodeFieldName__c);
        }

    }
    public static Map<String, String> fetchAllMetadata(){
        Map<String, String> mapZipCodeToCityName = new Map<String, String>(); 
        List<ZipCodeCity__mdt> lstMtd = [SELECT CityName__c, Zipcode__c FROM ZipCodeCity__mdt];
        for(ZipCodeCity__mdt objMtd : lstMtd){
            mapZipCodeToCityName.put(objMtd.Zipcode__c, objMtd.CityName__c);
        }
        return mapZipCodeToCityName;
    }
}
Approach 2:
public class ContactTriggerHandler {
    public static void onBeforeInsert(List<Contact> newContacts){
        
        List<String> lstZipCode = new List<String>();
        for(Contact objContact : newContacts){
            lstZipCode.add(objContact.ZipCodeFieldName__c);
        }
        Map<String,String> mapZipCodeToCityName = fetchAllMetadata(lstZipCode);
        for(Contact objContact : newContacts){
            if(mapZipCodeToCityName.containsKey(objContact.ZipCodeFieldName__c))
                objContact.CityNameFieldName__C = mapZipCodeToCityName.get(objContact.ZipCodeFieldName__c);
        }

    }
    public static Map<String, String> fetchAllMetadata(List<String> lstZipCodes){
        Map<String, String> mapZipCodeToCityName = new Map<String, String>(); 
        List<ZipCodeCity__mdt> lstMtd = [SELECT CityName__c, Zipcode__c FROM ZipCodeCity__mdt];
        for(ZipCodeCity__mdt objMtd : lstMtd){
            mapZipCodeToCityName.put(objMtd.Zipcode__c, objMtd.CityName__c);
        }
        return mapZipCodeToCityName;
    }
}

​​​​​​​

All Answers

Anju KumarAnju Kumar
Trigger Code
 
trigger ContactTrigger on Contact (before insert) {
    if(trigger.isInsert && trigger.isBefore)
        ContactTriggerHandler.onBeforeInsert(trigger.new);

}

There are two ways out here: 
1.> If the number of records in the metadata is less then you can directly query all the metadata records.
2.> If the number of records in the metadata is more then you should get all the zipcodes first and then query the metadata.

Approach 1:
Handler Code

public class ContactTriggerHandler {
    public static void onBeforeInsert(List<Contact> newContacts){
        Map<String,String> mapZipCodeToCityName = fetchAllMetadata();
        for(Contact objContact : newContacts){
            if(mapZipCodeToCityName.containsKey(objContact.ZipCodeFieldName__c))
                objContact.CityFieldName__c = mapZipCodeToCityName.get(objContact.ZipCodeFieldName__c);
        }

    }
    public static Map<String, String> fetchAllMetadata(){
        Map<String, String> mapZipCodeToCityName = new Map<String, String>(); 
        List<ZipCodeCity__mdt> lstMtd = [SELECT CityName__c, Zipcode__c FROM ZipCodeCity__mdt];
        for(ZipCodeCity__mdt objMtd : lstMtd){
            mapZipCodeToCityName.put(objMtd.Zipcode__c, objMtd.CityName__c);
        }
        return mapZipCodeToCityName;
    }
}
Approach 2:
public class ContactTriggerHandler {
    public static void onBeforeInsert(List<Contact> newContacts){
        
        List<String> lstZipCode = new List<String>();
        for(Contact objContact : newContacts){
            lstZipCode.add(objContact.ZipCodeFieldName__c);
        }
        Map<String,String> mapZipCodeToCityName = fetchAllMetadata(lstZipCode);
        for(Contact objContact : newContacts){
            if(mapZipCodeToCityName.containsKey(objContact.ZipCodeFieldName__c))
                objContact.CityNameFieldName__C = mapZipCodeToCityName.get(objContact.ZipCodeFieldName__c);
        }

    }
    public static Map<String, String> fetchAllMetadata(List<String> lstZipCodes){
        Map<String, String> mapZipCodeToCityName = new Map<String, String>(); 
        List<ZipCodeCity__mdt> lstMtd = [SELECT CityName__c, Zipcode__c FROM ZipCodeCity__mdt];
        for(ZipCodeCity__mdt objMtd : lstMtd){
            mapZipCodeToCityName.put(objMtd.Zipcode__c, objMtd.CityName__c);
        }
        return mapZipCodeToCityName;
    }
}

​​​​​​​
This was selected as the best answer
Tejashwini S 1Tejashwini S 1
This answer was very helpfulll.
Thank you so much  Anju Kumar
Aqua MatrixAqua Matrix
 Once I create the new record on contact it will auto populate the city name but once I edit it or change the zipcode to another city name it will remane same
For example if I create the zip code 560 083 and once I save it it will autopolate the corresponding city
But once I edit the record and change the zipcode to 560 043 then the city name is not getting change instead it remains as it is
What's the reason for this
Is it because of trigger before insert.
please help me solve this.