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
BobBob 

Account field update trigger

I am trying to write a trigger that will update a picklist field on the Account object if a checkbox is true. when the (Account.Billing_Address_Same_As_Shipping_Address__c=true) then i want the  Account.StateOrProvince__c  to equal Account.Shipping_State__c. any help would be appreciated.  my trigger code is below.


trigger Trigger_UpdateBillingAddress on Account (after update) {
    
    for (Account obj: trigger.new){
        if(Account.Billing_Address_Same_As_Shipping_Address__c=true)
        Account.StateOrProvince__c = Account.Shipping_State__c;
    }

}
Best Answer chosen by Bob
BDatlaBDatla
Hi Bob,

For insert triiger  trigger.oldMap is not avilable .
Please use the below code for before insert trigger.


trigger Trigger_UpdateBillingAddress on Account (before insert ,before update) {

if(Trigger.isInsert){
  for (Account obj: trigger.new){
         if(obj.Billing_Address_Same_As_Shipping_Address__c==true ){
                 obj.StateOrProvince__c = obj.Shipping_State__c;
                 obj.Billing_Country__c = obj.Shipping_Country__c;
     }
 }
}
if(Trigger.isUpdate){
  for (Account obj: trigger.new){
 if(obj.Billing_Address_Same_As_Shipping_Address__c==true 
        && trigger.oldMap.get(obj.Id).Billing_Address_Same_As_Shipping_Address__c != obj.Billing_Address_Same_As_Shipping_Address__c){
                 obj.StateOrProvince__c = obj.Shipping_State__c;
                 obj.Billing_Country__c = obj.Shipping_Country__c;
     }

 }
}
}


Let me know for any issues/calrificaations

Regards,
BDatla



 

All Answers

Sagar PareekSagar Pareek
trigger Trigger_UpdateBillingAddress on Account (after update) {
    
    for (Account obj: trigger.new){
        if(obj.Account.Billing_Address_Same_As_Shipping_Address__c==true)
        obj.Account.StateOrProvince__c = obj.Account.Shipping_State__c;
    }

}
BDatlaBDatla
Hi Bob,

trigger Trigger_UpdateBillingAddress on Account (before update) {

  for (Account obj: trigger.new){
         if(obj.Account.Billing_Address_Same_As_Shipping_Address__c==true 
        && trigger.oldMap.get(obj.Billing_Address_Same_As_Shipping_Address__c) !=      obj.Account.Billing_Address_Same_As_Shipping_Address__c)
                 obj.Account.StateOrProvince__c = obj.Account.Shipping_State__c;
     }
 }

To avoid updating everytime with the same value compare it with the old vallue and then only update.
Use before update as you are updating the same object.

Regards,
BD
BobBob
I’ll try it, thank you
BDatlaBDatla
Hi Bob,

trigger Trigger_UpdateBillingAddress on Account (before update) {

  for (Account obj: trigger.new){
         if(obj.Account.Billing_Address_Same_As_Shipping_Address__c==true 
        && trigger.oldMap.get(obj.Id).Billing_Address_Same_As_Shipping_Address__c !=      obj.Account.Billing_Address_Same_As_Shipping_Address__c){
                 obj.Account.StateOrProvince__c = obj.Account.Shipping_State__c;
     }
 }
}
Please use this version.

Reagrds,
BD
BobBob
Thank you for your help. I really appreciate it. below is the finished version.

trigger Trigger_UpdateBillingAddress on Account (before update) {

  for (Account obj: trigger.new){
         if(obj.Billing_Address_Same_As_Shipping_Address__c==true 
        && trigger.oldMap.get(obj.Id).Billing_Address_Same_As_Shipping_Address__c != obj.Billing_Address_Same_As_Shipping_Address__c){
                 obj.StateOrProvince__c = obj.Shipping_State__c;
                 obj.Billing_Country__c = obj.Shipping_Country__c;
     }
 }
}
BobBob
I have one more issue with this trigger.  When a new account is created and a user completes the shipping address field information and checks the Bill to Same as the Ship to checkbox, and the record is save the trigger doesnt work. I tried adding after insert, before upsert, but it cause an error. Any suggegtion for this trigger to execute when the record is saved?
BDatlaBDatla
Hi Bob,

For insert triiger  trigger.oldMap is not avilable .
Please use the below code for before insert trigger.


trigger Trigger_UpdateBillingAddress on Account (before insert ,before update) {

if(Trigger.isInsert){
  for (Account obj: trigger.new){
         if(obj.Billing_Address_Same_As_Shipping_Address__c==true ){
                 obj.StateOrProvince__c = obj.Shipping_State__c;
                 obj.Billing_Country__c = obj.Shipping_Country__c;
     }
 }
}
if(Trigger.isUpdate){
  for (Account obj: trigger.new){
 if(obj.Billing_Address_Same_As_Shipping_Address__c==true 
        && trigger.oldMap.get(obj.Id).Billing_Address_Same_As_Shipping_Address__c != obj.Billing_Address_Same_As_Shipping_Address__c){
                 obj.StateOrProvince__c = obj.Shipping_State__c;
                 obj.Billing_Country__c = obj.Shipping_Country__c;
     }

 }
}
}


Let me know for any issues/calrificaations

Regards,
BDatla



 
This was selected as the best answer
BobBob
Thank you so much that worked perfectly. I appreciate all your help!!