0

I want to create a trigger that, before insert or update, checks for a checkbox, and if the checkbox field is true, sets the Shipping Postal Code (whose API name is ShippingPostalCode) to be the same as the Billing Postal Code (BillingPostalCode).

  • The Apex trigger must be called 'AccountAddressTrigger'.
  • The Account object will need a new custom checkbox that should have the Field Label 'Match Billing Address' and Field Name of 'Match_Billing_Address'. The resulting API Name should be 'Match_Billing_Address__c'.
  • With 'AccountAddressTrigger' active, if an Account has a Billing Postal Code and 'Match_Billing_Address__c' is true, the record should have the Shipping Postal Code set to match on insert or update.

What should be the code for trigger ?

Carolyn Cordeiro
  • 1,525
  • 3
  • 11
  • 26

1 Answers1

1

I would suggest doing this with Process Builder rather than code. You can easily define logic of "if Match Billing Address is TRUE and BillingPostalCode is not blank, update ShippingPostalCode with the value of BillingPostalCode." It will be faster to implement and easier to maintain.
You can read more about Process Builder here: https://help.salesforce.com/articleView?id=process_overview.htm&type=5

If you are determined to use a trigger, it would look something like this:

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

    for(Account a : Trigger.new) {

        if(a.Match_Billing_Address__c && a.BillingPostalCode != null) {

            a.ShippingPostalCode = a.BillingPostalCode;
        }
    }
}
ChiefMcFrank
  • 721
  • 4
  • 18