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
Sabine ScheuringSabine 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. 
Best Answer chosen by Sabine Scheuring
ravi soniravi soni
hy Sabine,
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

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Sabine,

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. 
User-added image

Can you please check if there is any logic that is making Match_Billing_Address__c to true .

Thanks,
 
ravi soniravi soni
hy Sabine,
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
This was selected as the best answer
Sabine ScheuringSabine Scheuring
Hi Sai 
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...
Sabine ScheuringSabine Scheuring
Hi Veer 
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