function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Hanusha GanjiHanusha Ganji 

Apex trigger that sets an account’s Shipping Postal Code to match the Billing Postal Code if the Match Billing Address option is selected

reate an Apex Trigger
Create an Apex trigger that sets an account’s Shipping Postal Code to match the Billing Postal Code if the Match Billing Address option is selected. Fire the trigger before inserting an account or updating an account.
Pre-Work:
Add a checkbox field to the Account object:
Field Label: Match Billing Address
Field Name: Match_Billing_Address
Note: The resulting API Name should be Match_Billing_Address__c.

HI All,

According to the above question i need to create a checkbox , which i created by going to setup-->objectmanager-->account--->fields&relationships---->checkbox--->and give the name and keeping it unchecked  + save

i am clear with the code but my question is i have created the checkbox but i couldn't see the option while i am editing/creating the record in  account object.,

according to the question if i select checked then trigger will fire but i couldn't see any option as such

could you please help me with this
 
Best Answer chosen by Hanusha Ganji
Suraj Tripathi 47Suraj Tripathi 47
Hi,

Please follow the below code you have to write before trigger on account object.
trigger AccountTrigger on Account (before insert, before update){
    
    if(trigger.isbefore){
       
        if(trigger.isInsert || trigger.isupdate){
            
            accountPostal.disp(trigger.new);
        }
    }
}



///////////////////////
public class accountPostal{
  public static void disp(List<account> accountList)
   {
      
        list<account> accountList1 = [select id,name,Match_Billing_Address__c,BillingPostalCode,shippingPostalCode from Account where id in: accountList];

       for(Account ac: accountList1){
                 
                if(ac.Match_Billing_Address__c ==true){
				ac.shippingPostalCode= ac.BillingPostalCode)
                  
        }           
      
   }
}


Please mark it as Best Answer if it helps you.

Thanks & Regards
Suraj Tripathi

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi Hanusha,

Can you try checking if there is any trigger that operates as above mentioned logic and also can you check the accessibility of the field?

Thanks.
Suraj Tripathi 47Suraj Tripathi 47
Hi,

Please follow the below code you have to write before trigger on account object.
trigger AccountTrigger on Account (before insert, before update){
    
    if(trigger.isbefore){
       
        if(trigger.isInsert || trigger.isupdate){
            
            accountPostal.disp(trigger.new);
        }
    }
}



///////////////////////
public class accountPostal{
  public static void disp(List<account> accountList)
   {
      
        list<account> accountList1 = [select id,name,Match_Billing_Address__c,BillingPostalCode,shippingPostalCode from Account where id in: accountList];

       for(Account ac: accountList1){
                 
                if(ac.Match_Billing_Address__c ==true){
				ac.shippingPostalCode= ac.BillingPostalCode)
                  
        }           
      
   }
}


Please mark it as Best Answer if it helps you.

Thanks & Regards
Suraj Tripathi
This was selected as the best answer