Copy Billing to Shipping Address on Update

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;
                      }
             }
     }
}

Want to Apply As Content Writer?

Leave a Comment

Your email address will not be published. Required fields are marked *

Shopping Cart

Let's get you started!

Interested in writing Salesforce Content?

Fill in this form and we will get in touch with you :)