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
ram sahuram sahu 

issue regarding isInsert and isUpdate for trigger in apex?

I need to convert my trigger into isInsert and isUpdate.
Trigger :

trigger GenerateTimeZone on Lead (before insert,before update) {
    List<Timezone_Setup_Data__mdt> TimezoneSetupList = [SELECT State__c,TimeZone__c FROM Timezone_Setup_Data__mdt];
    if ( TimezoneSetupList != null && !TimezoneSetupList.isEmpty() ){
        Map<String, String> timeZoneMap = new Map<String, String>();
        for(Timezone_Setup_Data__mdt convertToMap : TimezoneSetupList){
            timeZoneMap.put(convertToMap.State__c,convertToMap.TimeZone__c);
        }
        for(Lead leadRec:Trigger.New){
            if(leadRec.Borrower_AddressState__c != NULL){
                if(timeZoneMap.containsKey(leadRec.Borrower_AddressState__c)){
                    leadRec.Lead_Timezone__c = timeZoneMap.get(leadRec.Borrower_AddressState__c);
                }
            }
            else if(leadRec.Property_State__c != NULL){
                if(timeZoneMap.containsKey(leadRec.Property_State__c)){
                    leadRec.Lead_Timezone__c = timeZoneMap.get(leadRec.Property_State__c);
                }
            }
            else{
                   leadRec.Lead_Timezone__c='PST';
            }
        }
    } 

}
Shubham4462Shubham4462
Hello Ram ,

For What Scenarion you need to convert can you Elobrate
ram sahuram sahu
Hello Shubham,
I need to shift logic of the trigger into a class method so that i can call methods inside the triggers.
Sonali_takkeSonali_takke
HI RAM,

Update the code as below:

 
trigger GenerateTimeZone on Lead (before insert,before update) {
  if(trigger.isInsert || Trigger.isUpdate) {
    GenerateTimeZoneHandler.GenerateTime(trigger.new);
  }
}


public class GenerateTimeZoneHandler {
    public static GenerateTime(){
        List<Timezone_Setup_Data__mdt> TimezoneSetupList = [SELECT State__c,TimeZone__c FROM Timezone_Setup_Data__mdt];
    if ( TimezoneSetupList != null && !TimezoneSetupList.isEmpty() ){
        Map<String, String> timeZoneMap = new Map<String, String>();
        for(Timezone_Setup_Data__mdt convertToMap : TimezoneSetupList){
            timeZoneMap.put(convertToMap.State__c,convertToMap.TimeZone__c);
        }
        for(Lead leadRec:Trigger.New){
            if(leadRec.Borrower_AddressState__c != NULL){
                if(timeZoneMap.containsKey(leadRec.Borrower_AddressState__c)){
                    leadRec.Lead_Timezone__c = timeZoneMap.get(leadRec.Borrower_AddressState__c);
                }
            }
            else if(leadRec.Property_State__c != NULL){
                if(timeZoneMap.containsKey(leadRec.Property_State__c)){
                    leadRec.Lead_Timezone__c = timeZoneMap.get(leadRec.Property_State__c);
                }
            }
            else{
                   leadRec.Lead_Timezone__c='PST';
            }
        }
    } 
    
    }
}

 
ram sahuram sahu
Hi Dev341,

Please also suggest how to write Test class for this.