Update Account Billing in Shipping address?

Apex Trigger Scenario

→ Apex Trigger 2

Develop an Apex Trigger so that every time when any account is created or updated then Set the Value of the Billing Address is to Shipping Address.

Shipping Address

  • ShippingStreet
  • ShippingCity
  • ShippingState
  • ShippingPostalCode
  • ShippingCountry

Billing Address

  • BillingStreet
  • BillingCity
  • BillingState
  • BillingPostalCode
  • BillingCountry

Hands on

				
					// trigger
trigger AccountTrigger on Account (before insert, before update) {
    AccountTriggerDispatcher.dispatch(Trigger.operationType);
}
				
			

Dispatcher Class

				
					public class AccountTriggerDispatcher {
    public Static void dispatch(System.TriggerOperation operationType){
        switch on operationType{
            WHEN BEFORE_INSERT{
                AccountTriggerHandler.beforeInsert(trigger.new);
            }
            WHEN BEFORE_UPDATE{ 
                AccountTriggerHandler.beforeUpdate(trigger.new);
            }
        }
    }
}
				
			

Handler Class

				
					// Handler Class calls helper class
public with sharing class AccountTriggerHandler {
    public Static void beforeInsert(ListaccountList){
        AccountTriggerHelper.helperBeforeInsertUpdate(accountList);
    }
    public Static void beforeUpdate(ListaccountList){
        AccountTriggerHelper.helperBeforeInsertUpdate(accountList);
    }
 }
				
			

Helper Class

				
					 // Helper class
public class AccountTriggerHelper {
    public Static void helperBeforeInsertUpdate(ListaccountList){
      for(Account acc : accountList){
        acc.ShippingStreet = acc.BillingStreet;
        acc.ShippingCity = acc.BillingCity;
        acc.ShippingState = acc.BillingState;
        acc.ShippingPostalCode = acc.BillingPostalCode;
        acc.ShippingCountry = acc.BillingCountry;
      }
    }
}
				
			

Resources

https://trailhead.salesforce.com/content/learn/modules/apex_triggers/apex_triggers_intro

Vaibhav Kumar
Vaibhav Kumar
Articles: 6

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Leave a Reply

Your email address will not be published. Required fields are marked *

Discover more from Panther Schools

Subscribe now to keep reading and get access to the full archive.

Continue reading