Mr. Man
Mr. Man

Reputation: 3

Trigger unexpectedly failing (Trailhead - Get Started with Apex Triggers)

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.

Upvotes: 0

Views: 1206

Answers (1)

Dieter Wittgens
Dieter Wittgens

Reputation: 11

There are a few things wrong with the original code. Primarily, there is no reason to run a SOQL query in this trigger. In the accepted answer, notice the use of the Trigger.New collection, which contains all the Account records that are being inserted or updated. By using this variable, we can access the values of the records being inserted or updated without querying using SOQL (which would have the effect of updating records that aren't currently being updated or inserted as well).

Additionally, the original code calls update DML on the accounts. The main problem is that with before triggers like this, you don't need to call update, just setting the field value on the sObject will change it's value as you are setting the value before the update or insert is completed!

Finally, the original code is iterating through a collection and calling DML within the loop. This is bad practise, violating the concept of Bulkification in Salesforce.

Upvotes: 1

Related Questions