Reputation: 1905
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
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