Skip to main content


Simplifying Address Management with Apex Triggers in Salesforce

Optimizely Configured Commerce Best Practices

Hello Trailblazers!

In Salesforce, managing addresses can sometimes be a tedious task, especially when dealing with duplicate data or ensuring consistency across different records. However, with the power of Apex triggers, we can automate address management processes to streamline operations and improve data accuracy.

In this blog, we’ll explore how to use an Apex trigger to automatically synchronize shipping and billing addresses on the Account object based on a checkbox field.

Scenario Overview

Imagine you’re working with the “Account Request” object in Salesforce, and you have two sets of address fields: one for billing address and another for shipping address. To simplify data entry and ensure consistency, you decide to implement a checkbox field named “Billing address same as the shipping address.” When this checkbox is checked, the billing address fields should automatically be populated with the values from the shipping address fields.

If you would like to learn more about how it appears on UI, follow this link.

Creating the Apex Trigger

To achieve this functionality, we’ll create an Apex trigger on the “Account Request” object that fires whenever the “Billing address same as shipping address” checkbox is checked. Here’s how we can implement the trigger:


trigger AccountRequestTrigger on AccountRequest__c (before insert) {

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



public class AccountRequestTriggerHandler {
    //trigger_handler for making billing address same as shipping address when checkbox = true .... 
    public static void createAccountRequest(List<AccountRequest__c> accReq){
        System.debug('accReq Value ==>' +accReq);
        for(AccountRequest__c acc : accReq){
            if(acc.Billing_Add_same_as_Shipping_Add__c  == true){
                acc.BillingStreet__c = acc.ShippingStreet__c ;
                acc.BillingCity__c = acc.ShippingCity__c ;
                acc.BillingState__c = acc.ShippingState__c ;
                acc.BillingCountry__c = acc.ShippingCountry__c ;
                acc.BillingPostalCode__c = acc.ShippingPostalCode__c ;



public class AccReqTrigHandlerTest {
    public static void testCheckboxVal(){
        List<AccountRequest__c> accList = new List<AccountRequest__c>();
        AccountRequest__c accR = new AccountRequest__c();
        accR.Name = 'Prathamesh';
        accR.FirstName__c = 'Pratham';
        accR.LastName__c = 'Parab';
        accR.PrimaryEmail__c = '' ;
        accR.ShippingStreet__c = 'IT Garden';
        accR.ShippingCity__c = 'Sawantwadi' ;
        accR.ShippingState__c = 'Maharashtra';
        accR.ShippingPostalCode__c = '896523' ;
        accR.ShippingCountry__c = 'India';
        accR.Billing_Add_same_as_Shipping_Add__c = true;
        insert accR;
        System.debug('Account Req Record==>' +accR);
        List<AccountRequest__c> accReqest = [SELECT Id, Billing_Add_same_as_Shipping_Add__c , ShippingStreet__c,ShippingCity__c,ShippingState__c,ShippingPostalCode__c,ShippingCountry__c from AccountRequest__c];

        for( AccountRequest__c acc : accReqest){
            if(acc.Billing_Add_same_as_Shipping_Add__c == true){
                acc.BillingStreet__c = acc.ShippingStreet__c ;
                acc.BillingCity__c = acc.ShippingCity__c ;
                acc.BillingState__c = acc.ShippingState__c ;
                acc.BillingPostalCode__c = acc.ShippingPostalCode__c ;
                acc.BillingCountry__c = acc.ShippingCountry__c ;
        update accList;


In the Apex trigger and its Handler Class

  1. We define a before-insert trigger that fires before the Account Request records are inserted. (You can use before insert, before update, etc. as per the requirements.)
  2. In the handler class, we iterate over the Account Request records in the context variable.
  3. If the BillingAddressSameAsShipping__c checkbox is checked, we synchronize the billing address fields with the corresponding shipping address fields.

In the Apex Test Class

  1. We create a test Account Request record with shipping address fields populated.
  2. We also set the BillingAddressSameAsShipping__c checkbox to true and insert the record.
  3. We query the Account Request record, iterate it, and synchronize the billing address fields with the shipping address fields.

So, with this, we can successfully populate the billing address fields with the values of the shipping address fields using Apex Trigger.

If you did not check the previous part of this blog about how we can implement this functionality with the help of LWC, then you can go with this link.


With the help of Apex triggers, we automated the synchronization of shipping and billing addresses on the Account Request object in Salesforce. With the help of Salesforce triggers, we can streamline data management processes, improve data accuracy, and enhance user productivity.

Happy Reading!


  1. Lightning Web Components Developer Guide
  2. Lightning Component Library
  3. Trailhead: Lightning Web Components Basics

You Can Also Read:

  1. An Introduction to Salesforce CPQ
  2. Salesforce CPQ and its Key Features
  3. Unlocking the Power of AI: Einstein for Developers
  4. Revolutionizing Customer Engagement: The Salesforce Einstein Chatbot

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Abhinav Masane

Abhinav Masane is an Associate Technical Consultant at Perficient based in Nagpur. He is a Salesforce Certified Associate and Developer. Abhinav is always keen to learn and explore new technologies.

More from this Author

Follow Us