You need to sign in to do that
Don't have an account?

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';
}
}
}
}
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';
}
}
}
}
For What Scenarion you need to convert can you Elobrate
I need to shift logic of the trigger into a class method so that i can call methods inside the triggers.
Update the code as below:
Please also suggest how to write Test class for this.