You need to sign in to do that
Don't have an account?
Sabine Scheuring
Apex Trigger does not work as expected
The task is to 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. As per the task, I created the checkbox "Match Billing Account" on the Accounts Object first. Here is the code I used which seems to work for other users:
trigger AccountAddressTrigger on Account (before insert, before update){
for(Account account: Trigger.new){
if((account.Match_Billing_Address__c==true)&&(account.BillingPostalCode != NULL)){
account.ShippingPostalCode = account.BillingPostalCode;
}
}
}
The Error is as following: We updated an account that had 'Match_Billing_Address__c' set to false. We expected the trigger not to fire, but it did. Make sure the trigger fires only if 'Match_Billing_Address__c' is true.
I tried different Playgrounds but always end up with the same error.
trigger AccountAddressTrigger on Account (before insert, before update){
for(Account account: Trigger.new){
if((account.Match_Billing_Address__c==true)&&(account.BillingPostalCode != NULL)){
account.ShippingPostalCode = account.BillingPostalCode;
}
}
}
The Error is as following: We updated an account that had 'Match_Billing_Address__c' set to false. We expected the trigger not to fire, but it did. Make sure the trigger fires only if 'Match_Billing_Address__c' is true.
I tried different Playgrounds but always end up with the same error.
your code is looking good but as you said that after setting Match_Billing_Address__c' to false, trigger is firing then check there will be any trigger or process builder,flow,work flow or any formula that make Match_Billing_Address__c to true.
If you are not able to get on account object then check it will be on child object but i'm sure somewhere else it is doing set Match_Billing_Address__c to true. that's why your trigger is firing.
let me know what is your status and if it helps you then don't forget to mark it as best answer.
Thank you
All Answers
I have tried the same code which you used and updated an account which has Match_Billing_Address__c as false. It it not updating the ShippingPostalCode.
Can you please check if there is any logic that is making Match_Billing_Address__c to true .
Thanks,
your code is looking good but as you said that after setting Match_Billing_Address__c' to false, trigger is firing then check there will be any trigger or process builder,flow,work flow or any formula that make Match_Billing_Address__c to true.
If you are not able to get on account object then check it will be on child object but i'm sure somewhere else it is doing set Match_Billing_Address__c to true. that's why your trigger is firing.
let me know what is your status and if it helps you then don't forget to mark it as best answer.
Thank you
Thanks for having a look at my issue. I can't find any other triggers, field dependencies or validation rules that might set the field to "true". Further I also tested the code in the playground with different scenarios (creating a new account, updating an existing account) with either the Checkbox checked or unchecked and this works as expected (not updating if checkbox is not ticked) as it does in your example. I am lost...
Thanks for your input. I couldn't find any trigger that modified the checkbox. However, the code worked when I created a new Playgroound.
Thanks & best regards