Solution:
If the “CopyBillingToShipping” checkbox is selected, this trigger ensures that any update to the billing address is mirrored in the shipping address for consistency.
Practice more scenarios like this with this YouTube playlist.
trigger AccountTrigger on Account (before update) {
if(Trigger.isUpdate){ if(Trigger.isBefore){
AccountTriggerHandler.copyBillToShip(Trigger.New,
Trigger.oldMap);
}
}
}
public class AccountTriggerHandler { public static void
copyBillToShip(List<Account> accList,Map<Id,Account> oldMap){
for(Account acc:accList){ if((oldMap==null && acc.CopyBillingToShipping__c) ||
(!oldMap.get(acc.Id).CopyBillingToShipping__c &&
acc.CopyBillingToShipping__c)){
acc.ShippingCity=acc.BillingCity;
acc.ShippingCountry=acc.BillingCountry;
acc.ShippingPostalCode=acc.BillingPostalCode;
acc.ShippingState=acc.BillingState; acc.ShippingStreet=acc.BillingStreet;
}
}
}
}