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
sf ostsf ost 

error in handling

Hi Guys,

I am getting this error while clearing a task in trailhead.

Challenge Not yet complete... here's what's wrong:
Setting 'Match_Billing_Address__c' to true did not update the records as expected.

Question: For this challenge, you need to create a trigger that, before insert or update, checks for a checkbox, and if the checkbox field is true, sets the Shipping Postal Code (whose API name is ShippingPostalCode) to be the same as the Billing Postal Code (BillingPostalCode).
  1. The Apex trigger must be called 'AccountAddressTrigger'.
  2. The Account object will need a new custom checkbox that should have the Field Label 'Match Billing Address' and Field Name of 'Match_Billing_Address'. The resulting API Name should be 'Match_Billing_Address__c'.
  3. With 'AccountAddressTrigger' active, if an Account has a Billing Postal Code and 'Match_Billing_Address__c' is true, the record should have the Shipping Postal Code set to match on insert or update.
My code is below..
 
trigger AccountAddressTrigger on Account (before insert, before update) {

if(trigger.isInsert && trigger.isBefore)
{
   for( Account a : Trigger.New)
    {
        if(a.Match_Billing_Address__c == true && a.ShippingPostalCode!=null)
          {

                 a.ShippingPostalCode= a.BillingPostalCode;
             
           }
      }
}
}

What is wrong in the code... ?
 
Amit Chaudhary 8Amit Chaudhary 8
Please try below code.
rigger AccountAddressTrigger on Account (before insert, before update) {

    for(Account a : Trigger.new){
        If (a.Match_Billing_Address__c == true && a.BillingPostalCode!=Null) {
            a.ShippingPostalCode = a.BillingPostalCode;
        }   
    } 

}

You need to check BillingPostalCode != null

I tested the same that is working fine in my dev org .Let us know if that will help you.