Development questions
Write a trigger to automatically populate Account shipping address fields with billing address fields if Copy_Billing_To_Shipping__c checkbox is checked.
Use before insert, before update so values are saved without extra DML.
apex
trigger AccountTrigger on Account (before insert, before update) {
for (Account a : Trigger.new) {
if (a.Copy_Billing_To_Shipping__c) {
a.ShippingStreet = a.BillingStreet;
a.ShippingCity = a.BillingCity;
a.ShippingState = a.BillingState;
a.ShippingPostalCode = a.BillingPostalCode;
a.ShippingCountry = a.BillingCountry;
}
}
}
apex-triggertriggercodingscenario
More Development questions
- What is an Apex Trigger in Salesforce and when should you use it over Flows?
- What is the difference between before triggers and after triggers?
- Explain all Trigger context variables (isInsert, isUpdate, isDelete, isUndelete, isBefore, isAfter, isExecuting, size, operationType).
- What is the difference between Trigger.new and Trigger.old?
- In which trigger events are Trigger.new and Trigger.newMap available?
- In which trigger events are Trigger.old and Trigger.oldMap available?