+ Start a Discussion
Jason Good 13Jason Good 13 

Trailhead challenge with Apex Triggers

Create an Apex trigger for Account that matches Shipping Address Postal Code with Billing Address Postal Code based on a custom field

I used the code below and am getting this error... "Challenge Not yet complete... here's what's wrong: 
Setting 'Match_Billing_Address__c' to true did not update the records as expected." I have no other triggers or validation rules activated. any help would be much appreciated. Thanks in advance

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


List<Account> acclst=new List<Account>();
  for(account a:trigger.new){
    if(a.Match_Billing_Address__c==true && a.BillingPostalCode!=null){
    a.ShippingPostalCode=a.BillingPostalCode;
        
    }

}
}
PawanKumarPawanKumar
Hi,
Please try below code.

trigger AccountAddressTrigger on Account(before insert, before update) {
 List < Account > acclst = new List < Account > ();
 for (account a: trigger.new) {
  if (a.Match_Billing_Address__c && String.isNotEmpty(a.BillingPostalCode)) {
   a.ShippingPostalCode = a.BillingPostalCode;
  }
 }
}

Regards,
Pawan Kumar
Jason Good 13Jason Good 13
I used the code you provided and got the same result. The log status says "Assertion Failed: Expected: 60613, Actual: null"
PawanKumarPawanKumar
Please give one more try with below code.

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;
  }
 }
}
Jason Good 13Jason Good 13
Negative. got the same error on both trailhead and on the dev console for the log.
SnehithBoyaSnehithBoya
Try below two versions: 
Version 1:
For(Account a: Trigger.new){
                If (a.Match_Billing_Address__c == true) {
                    a.ShippingPostalCode = a.BillingPostalCode;
                    system.debug('Shipping postal code was updated to '+a.ShippingPostalCode);
                }
            }


Version 2:

For(Account a: Trigger.new){
                If ((a.Match_Billing_Address__c == true) && (a.BillingPostalCode!=null) ) {
                    a.ShippingPostalCode = a.BillingPostalCode;
                    system.debug('Shipping postal code was updated to '+a.ShippingPostalCode);
                }
            }
Jason Good 13Jason Good 13
Thank you for the response but i got the same error when checking challenge. Plus the error in dev console is "unexpected syntax" 'mismatched input' expecting RCURLY"
Jason Good 13Jason Good 13
Could i be missing something somewhere else besides the code itself?
SnehithBoyaSnehithBoya
This code works for me, Can you give this a shot?
I the if condition for isInsert or isUpdate is missing, Just replace the whole trigger with below block:

trigger Test_Account_Trigger on Account (before insert, before update) {
    
        if (Trigger.isinsert  || Trigger.isupdate){
            system.debug('Code block before insertion or update');
            For(Account a: Trigger.new){
                If (a.Match_Billing_Address__c == true) {
                    a.ShippingPostalCode = a.BillingPostalCode;
                    system.debug('Shipping postal code was updated to '+a.ShippingPostalCode);
                }
            }
        }
    
}

 
Jason Good 13Jason Good 13
Thank you for your response but i got the same error "Setting 'Match_Billing_Address__c' to true did not update the records as expected.". after going back and looking further, in my playground i do not have any field labeled BillingpostalCode or ShippingPostalCode anywhere in my object manager or fields. How do i create these fields without having the __c at the end of it? Could this be the cause of it not updating?
Jim Garrison 16Jim Garrison 16

As an update to anyone else having this same issue... 
SnehithBoya's version with one minor change should work for you:

-------------------

trigger AccountAddressTrigger on Account (before insert, before update) {
    
        if (Trigger.isinsert  || Trigger.isupdate){
            system.debug('Code block before insertion or update');
            For(Account a: Trigger.new){
                If (a.Match_Billing_Address__c == true) {
                    a.ShippingPostalCode = a.BillingPostalCode;
                    system.debug('Shipping postal code was updated to '+a.ShippingPostalCode);
                }
            }
        }
    
}

 

SnehithBoyaSnehithBoya

@jason Good 13

Any custom field that we create will be having '__c' at the end of it. The '__c' denominates that it is a custom field. And yes, that it might be the cause for your issue. Please create the field on Account and try again with the suggested code. Let me know how it goes.

HEMPRASANTH KARUNAKARANHEMPRASANTH KARUNAKARAN
I got this same error. "Challenge Not yet complete... here's what's wrong: 
Setting 'Match_Billing_Address__c' to true did not update the records as expected." I found that I have created the Apex trigger in My developer account. After creating the Apex trigger in Trailhead Playground , I am able to pass the challenge.
Tim McDermottTim McDermott
@JasonGood13 and any others who had this question:

after going back and looking further, in my playground i do not have any field labeled BillingpostalCode or ShippingPostalCode anywhere in my object manager or fields.
Some fields are composites of other fields.  In this case, the BillingPostalCode is a composite field of BillingAddress.  See this reference below:

https://developer.salesforce.com/docs/atlas.en-us.object_reference.meta/object_reference/compound_fields_address.htm
karim gabrielkarim gabriel
This code worked for me (below). Couple of things:

1) As stated above, the shipping address is a composite of several fields, so you can't write directly to ShippingAddress you need to write to it's sub components
2) I don't know why I had to check for false first. If I did not, I kept getting the error "Setting 'Match_Billing_Address__c' to false updated the records anyway. The trigger should only act when Match_Billing_Address__c is true.". It's not the most efficient use of code, but it worked. If anyone can explain that, I would appreicate it. 

trigger AccountAddressTrigger on Account (before insert) {

    for(Account a : Trigger.New) {
        
        if (a.Match_Billing_Address__c == false)
            return;
    
    if ( a.BillingAddress != null && a.Match_Billing_Address__c == true)
       
            a.ShippingCity = a.BillingCity; 
            a.ShippingCountry = a.BillingCountry;
            a.ShippingPostalCode = a.BillingPostalCode;
            a.ShippingState = a.BillingState;
            a.ShippingStreet = a.BillingStreet;
     
}
}
MadihaRaza 26MadihaRaza 26
I am sure this must be now resolved. I was having the same error and figured I created custom fields  for shipping and billing postal code ..   Duhh on my end ! 
Suraj Tripathi 47Suraj Tripathi 47
Hi Jason Good 13,

"Try this code."
trigger AccountAddressTriggerChallenge on Account (before insert , before update) {
    for(Account a:Trigger.New){
        if(a.Match_Billing_Address__c && (a.BillingPostalCode != null || a.BillingPostalCode!=' ' )){
           a.ShippingPostalCode = a.BillingPostalCode;
        }
    }
}

If you find your Solution then mark this as the best answer. 

Thank you!

Regards 
Suraj Tripathi