I am attempting the challenge at https://trailhead.salesforce.com/content/learn/modules/apex_triggers/apex_triggers_intro?trail_id=force_com_dev_beginner It says:
Create 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. Create an Apex trigger: Name: AccountAddressTrigger Object: Account Events: before insert and before update Condition: Match Billing Address is true Operation: set the Shipping Postal Code to match the Billing Postal Code
I have code that succeeded and code that failed.
Code that succeeded:
trigger AccountAddressTrigger on Account (before insert, before update) {
for (Account a : Trigger.new){
if(a.match_billing_address__c==true){
a.shippingpostalcode=a.billingPostalCode;}
}
}
Code that failed:
trigger AccountAddressTrigger on Account (before insert, before update) {
for (Account a : [SELECT Id FROM Account WHERE (match_billing_address__c=true) AND
(Id in :Trigger.new)]){
a.shippingpostalcode=a.billingPostalCode;
}
}
Error message on code that failed:
We updated an account that had 'Match_Billing_Address__c' set to true. We expected the trigger to fire, but it didn’t. Make sure the trigger fires if 'Match_Billing_Address__c' is true.
To me, these two codes seem equivalent. Why does the second one fail? Note: in the original form of this question, I linked to someone else's question regarding the same challenge that failed, asking for explanation on the answers. I thought it was similar enough to my attempt, but it turns out it isn't, which is why I changed this one.