triggerssalesforceapexapex-trigger

Apex Triggers - Trailhead


The code itself doesn't give any errors, but anytime I run it Trailhead gives me this message:

"Challenge not yet complete... here's what's wrong: Executing the trigger did not work as expected. "

Here are the instructions:

For this challenge, you need to create a trigger that, before insert or update, checks for a checkbox. If the checkbox field is true, it sets the Shipping Postal Code (whose API name is ShippingPostalCode) to be the same as the Billing Postal Code (BillingPostalCode).

My code:

trigger AccountAddressTrigger on Account (before insert,before update) {

    for(Account a : [SELECT Id FROM Account WHERE  Match_Billing_Address__c = TRUE AND BillingPostalCode != NULL])
    {
        a.ShippingPostalCode = a.BillingPostalCode;
        update a;        
    }//end for     
}

Solution

  • Your Trigger is like this way.

    trigger AccountAddressTrigger on Account (before insert,before update) {
    
        //Iterate all accounts that is updated or inserted.
        for(Account acc :Trigger.New){
            //if match is true set values.
            if(acc.Match_Billing_Address__c){
                acc.ShippingPostalCode = acc.BillingPostalCode;
            }
        }
    }