How to update Account Billing address in Shipping address , Only If Shipping address is Blank ?

on

|

views

and

comments

If you have missed the previous scenarios you can get the scenarios from the below link 

https://www.pantherschools.com/category/developer/

Table of Contents

Introduction to scenario

→ Apex Trigger 3

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

You only need to update the Shipping Address information with the Billing Address if the Shipping Address (Compound Field) has having blank value.

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

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

Outcome

Resources

Share this

Leave a review

Excellent

SUBSCRIBE-US

Book a 1:1 Call

Must-read

How to Utilize Salesforce CLI sf (v2)

The Salesforce CLI is not just a tool; it’s the cornerstone of development on the Salesforce Platform. It’s your go-to for building, testing, deploying, and more. As one of the most important development tools in our ecosystem

Save the day of a Developer with Apex Log Analyzer

Table of Contents What is Apex Log Analyzer? Apex Log Analyzer, a tool designed with Salesforce developers in mind, is here to simplify and accelerate your...

Salesforce PodCast

Introduction Hey Everyone, Welcome to my podcast, the first-ever podcast in India for Salesforce professionals. Achievement We are happy to announce that we have been selected as Top...

Recent articles

More like this

LEAVE A REPLY

Please enter your comment!
Please enter your name here

5/5

Stuck in coding limbo?

Our courses unlock your tech potential