You need to sign in to do that
Don't have an account?
Bob
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;
}
}
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;
}
}
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
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
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
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;
}
}
}
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