Carolyn Cordeiro
Carolyn Cordeiro

Reputation: 1905

Create an Apex trigger for Account that matches Shipping Address Postal Code with Billing Address Postal Code based on a custom field

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).

What should be the code for trigger ?

Upvotes: 1

Views: 11751

Answers (1)

ChiefMcFrank
ChiefMcFrank

Reputation: 791

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

Upvotes: 3

Related Questions