Search Here

When an account is inserted or updated and the CopyBillingToShipping checkbox is checked then automatically copy the account billing address into account shipping address triggers in Salesforce

 To automatically copy the Billing Address into the Shipping Address when the CopyBillingToShipping checkbox is checked on an Account record, you can create an Apex Trigger on the Account object. This trigger will run when an Account is inserted or updated and will copy the Billing Address fields to the Shipping Address fields if the checkbox (CopyBillingToShipping) is checked.

trigger CopyBillingToShipping on Account (before insert, before update) {

    // Loop through all Account records being inserted or updated

    for (Account acc : Trigger.new) {

        // Check if the CopyBillingToShipping checkbox is checked

        if (acc.CopyBillingToShipping__c == true) {

            // Copy Billing Address to Shipping Address

            acc.ShippingStreet = acc.BillingStreet;

            acc.ShippingCity = acc.BillingCity;

            acc.ShippingState = acc.BillingState;

            acc.ShippingPostalCode = acc.BillingPostalCode;

            acc.ShippingCountry = acc.BillingCountry;

        }

    }

}


Explanation of the Code

  1. Trigger Event:

    • The trigger is fired on the before insert and before update events because we want to copy the Billing Address to the Shipping Address before the Account record is saved to the database.
  2. Logic:

    • The trigger checks if the CopyBillingToShipping__c checkbox is checked (true).
    • If the checkbox is checked, it copies the values from the Billing Address fields to the Shipping Address fields. The fields copied include:
      • BillingStreetShippingStreet
      • BillingCityShippingCity
      • BillingStateShippingState
      • BillingPostalCodeShippingPostalCode
      • BillingCountryShippingCountry
  3. Bulk Processing:

    • This trigger is bulkified, meaning it handles multiple Account records in a single transaction.
@isTest
private class CopyBillingToShippingTest {
    @isTest
    static void testCopyBillingToShipping() {
        // Create an Account with a billing address and the CopyBillingToShipping checkbox checked
        Account testAccount = new Account(
            Name = 'Test Account',
            BillingStreet = '123 Billing St',
            BillingCity = 'Test City',
            BillingState = 'TS',
            BillingPostalCode = '12345',
            BillingCountry = 'Testland',
            CopyBillingToShipping__c = true // Checkbox is checked
        );
        insert testAccount;

        // Query the Account to verify that the Shipping Address has been copied
        Account insertedAccount = [SELECT ShippingStreet, ShippingCity, ShippingState, ShippingPostalCode, ShippingCountry 
                                   FROM Account WHERE Id = :testAccount.Id];

        // Assert that the Shipping Address has the same values as the Billing Address
        System.assertEquals('123 Billing St', insertedAccount.ShippingStreet, 'Shipping Street should match Billing Street');
        System.assertEquals('Test City', insertedAccount.ShippingCity, 'Shipping City should match Billing City');
        System.assertEquals('TS', insertedAccount.ShippingState, 'Shipping State should match Billing State');
        System.assertEquals('12345', insertedAccount.ShippingPostalCode, 'Shipping PostalCode should match Billing PostalCode');
        System.assertEquals('Testland', insertedAccount.ShippingCountry, 'Shipping Country should match Billing Country');
    }

    @isTest
    static void testNoCopyWhenCheckboxIsNotChecked() {
        // Create an Account without the checkbox being checked
        Account testAccount = new Account(
            Name = 'Test Account No Copy',
            BillingStreet = '123 Billing St',
            BillingCity = 'Test City',
            BillingState = 'TS',
            BillingPostalCode = '12345',
            BillingCountry = 'Testland',
            CopyBillingToShipping__c = false // Checkbox is not checked
        );
        insert testAccount;

        // Query the Account to verify that the Shipping Address is not copied
        Account insertedAccount = [SELECT ShippingStreet, ShippingCity, ShippingState, ShippingPostalCode, ShippingCountry 
                                   FROM Account WHERE Id = :testAccount.Id];

        // Assert that the Shipping Address is not populated (i.e., remains null)
        System.assertEquals(null, insertedAccount.ShippingStreet, 'Shipping Street should be null');
        System.assertEquals(null, insertedAccount.ShippingCity, 'Shipping City should be null');
        System.assertEquals(null, insertedAccount.ShippingState, 'Shipping State should be null');
        System.assertEquals(null, insertedAccount.ShippingPostalCode, 'Shipping PostalCode should be null');
        System.assertEquals(null, insertedAccount.ShippingCountry, 'Shipping Country should be null');
    }
}



Tags

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.