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
Anitha J J 2Anitha J J 2 

Trailhead ApexTrigger unit1/2 Executing the trigger did not work as expected.

Hi All,

Below are the requirements to complete Challenge

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'.
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.


I have created new custom Checkbox(MatchBillingAddress) and Below is my trigger:

trigger AccountAddressTrigger on Account (before update,before insert)
{
   List<Account> list1=[SELECT Id,Match_Billing_Address__c,ShippingPostalCode,BillingPostalCode from Account];
  for(Integer i=0;i<list1.size();i++)
   {
    if(list1[i].Match_Billing_Address__c=true && list1[i].BillingPostalCode!=NULL)
    {
    list1[i].ShippingPostalCode=list1[i].BillingPostalCode;
    update list1[i];
    }
   }
  }

I got Error like : Executing the trigger did not work as expected.

Can anyone please help me to achieve this. Thanks
Best Answer chosen by Anitha J J 2
Swati GSwati G
One issue i found is this,
list1[i].Match_Billing_Address__c=true, you are assigning value as true instead of comparing it.

You should consider following points in this implementation:

You dont need to query account records, as you can get current set of records through trigger.new. And also dont explicitly update account list as you are populating field in before insert or update trigger.

Can update code to below:
for(Account acc: Trigger.new)
   {
    if(acc.Match_Billing_Address__c && acc.BillingPostalCode!=NULL)
    {
    acc.ShippingPostalCode=acc.BillingPostalCode;
    }
   }
 

All Answers

Swati GSwati G
One issue i found is this,
list1[i].Match_Billing_Address__c=true, you are assigning value as true instead of comparing it.

You should consider following points in this implementation:

You dont need to query account records, as you can get current set of records through trigger.new. And also dont explicitly update account list as you are populating field in before insert or update trigger.

Can update code to below:
for(Account acc: Trigger.new)
   {
    if(acc.Match_Billing_Address__c && acc.BillingPostalCode!=NULL)
    {
    acc.ShippingPostalCode=acc.BillingPostalCode;
    }
   }
 
This was selected as the best answer
Anitha J J 2Anitha J J 2
Hi Swati,

I m a newbie to Salesforce.Thank you so much it worked. Thanks for your immediate reply.

Trigger.new is there, so we dont have to update seperately by DML update statement. Is this correct?
bouscalbouscal
because this is in a Trigger and is executing before the update occurs you do not need an update statement, that will occur after the trigger is finished.  In fact, an update statement in a before trigger will throw an error.
Anitha J J 2Anitha J J 2
Thanks Bouscal .
Katherine Tucker 7Katherine Tucker 7
How did you create the checkbox object?
SattibabuSattibabu
Katherine: Checkbox filed can create in Salesforce user interface. You can use the below Navigation in you developer edition

Setup --> Customize--> Accounts-->Fields  and Under Account Custom Fields & Relationships section ..you can create Checkbox filed by using Checkbox datatype. 

In this trigger .. label of check box filed is 'Match Billing Address'  and  API Name of check box filed i.e ' Match_Billing_Address__c '  which is created automatically by the system. You can see that API Name used in trigger for validating purpose. 
Tarun Gupta 33Tarun Gupta 33
[Error] Error: Compile Error: Incorrect SObject type: Account should be User at line 1 column 1
i m getting this error while creating trigger for Account!
please help me
 
Punima SharmaPunima Sharma
Create a custom field with  field label : Match Billing Address and field name :Match_Billing_Address and data type = CheckBox and default set as Checked

Apex Trigger Code :
trigger AccountAddressTrigger on Account (before insert,  before update) {

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

Ashwini ns 5Ashwini ns 5

trigger 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 ;  
       
            } 
    

}
Sugandha Kumari 11Sugandha Kumari 11
you cant just write  a.ShippingPostalCode= a.BillingPostalCode ; instead....  a.ShippingPostalCode__c= a.BillingPostalCode__c ;  needs to be written else it doesnt updates the record ,but still the same error comes .What to do !!Error :[Challenge Not yet complete... here's what's wrong: 
Setting 'Match_Billing_Address__c' to true did not update the records as expected.]
kuldeep paliwalkuldeep paliwal
trigger 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;

    }
}
}