+ Start a Discussion
M ParnellM Parnell 

'Getting Started with Apex Triggers' Challenge Issue

When attempting to create my Apex trigger for this Trailhead development exercise, I receive an error. I don't understand how this is possible, because the records haven't been updated, and still aren't updating.
"Challenge not yet complete... here's what's wrong:
Setting 'Match_Billing_Address__c' to false updated the records anyway. The trigger should only act when Match_Billing_Address__c is true."


Here's my code:
trigger AccountAddressTrigger on Account (before insert, before update) {

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

}


 
Best Answer chosen by M Parnell
Peter YohannaPeter Yohanna
You should use == insted of = , so your code should read as follows:
trigger AccountAddressTrigger on Account (before insert, before update) {

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

}

 

All Answers

Peter YohannaPeter Yohanna
You should use == insted of = , so your code should read as follows:
trigger AccountAddressTrigger on Account (before insert, before update) {

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

}

 
This was selected as the best answer
Rajendra RathoreRajendra Rathore
Hi Marypearl,

Use that trigger code :

trigger AccountAddressTrigger on Account (before insert ,before update) {
    for(Account accnt : trigger.new){
        if(accnt.Match_Billing_Address__c && accnt.BillingPostalCode  != NULL)
            accnt.ShippingPostalCode = accnt.BillingPostalCode ;
    }
    
}


Thanks,
Rajendra
M ParnellM Parnell
Thank you Peter and Rajendra for your suggestions!
Andrew EversleyAndrew Eversley
Thanx to Peter Yohanna for helping me as well, I ran into the same issue and followed your instruction. Thanx Marypearl for posting the issue.
Uvais KomathUvais Komath

@tazur Rahman

thats wrong sorry!

== used for equality check, you are using it for assignment operation.

did your code work?

if so I can't understand how it worked.

GayatriGayatri
Hi All, please help,

I am getting following error message: 

Challenge Not yet complete... here's what's wrong: 
There was an unexpected error in your org which is preventing this assessment check from completing: System.QueryException: List has no rows for assignment to SObject

my code is as follows:

trigger AccountAddressTrigger on Account (before insert,before update) {
    
    // if field Match_Billing_Address__c is true then post the BillingPostalCode to ShippingPostalCode    
    if(trigger.size>0){        
        for(account a: trigger.New){
            if( a.Match_Billing_Address__c== true && a.BillingPostalCode != null){
                    a.ShippingPostalCode = a.BillingPostalCode;                
            } 
        }      
    }
}

Thanks

Gayatri
Maulik Patel 20Maulik Patel 20
I am getting the same error as Gayatri does... Anyhelp ???
Susan TurpinSusan Turpin
The double equals worked for me.   ==
Yuvraj ProgrammerYuvraj Programmer
I have created the account trigger to complete the trailhead task. But I'm facing following error:
Error:
Challenge Not yet complete... here's what's wrong: 
Setting 'Match_Billing_Address__c' to false updated the records anyway. The trigger should only act when Match_Billing_Address__c is true.


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

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

}
Anyone please guide where I missed the part? Thanks in advance.
 
Shikha Raheja 3Shikha Raheja 3
I am using the same code. But still I am getting below error.

Challenge Not yet complete... here's what's wrong: 
Setting 'Match_Billing_Address__c' to false updated the records anyway. The trigger should only act when Match_Billing_Address__c is true.

Any idea or suggestions?
Amit ShrivastavaAmit Shrivastava
I also faced similar issues, for this I set the field visibility to all and it worked for me.

hope this will help you.
Ravi Mishra 58Ravi Mishra 58
trigger AccountAddressTrigger on Account (before insert, before update) {

    for(Account acc : trigger.new)
    {
        if(acc.Match_Billing_address__c==TRUE)
        {
         	acc.ShippingPostalCode = acc.BillingPostalCode ;
        }
    }
    
}

I am doing this above code after creating ShippingPostalCode & BillingPostalCode as the custom text field and Match_Billing_Billing _Address as the checkbox field in Standard Account object. Still I am getting the error as

There was an unexpected error in your org which is preventing this assessment check from completing: System.QueryException: List has no rows for assignment to SObject.

Please guide where am I making the mistake.
 
Prashant Katyayan 9Prashant Katyayan 9
@Ravi.. I used the similar code and it worked. Please confirm once if you have created the Checkbox field correctly.. ?
 
Ashoknaidu NeelamAshoknaidu Neelam
I was tried this code. But its not working, it shows below error


Setting 'Match_Billing_Address__c' to false updated the records anyway. The trigger should only act when Match_Billing_Address__c is true.
ashoknaidu@neelam.com
Launch
Check challenge to earn 500 points

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

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

}


 
Jonathan A FoxJonathan A Fox
You need to ensure that when creating the check box field, visibility is set to all.
Govind GovindGovind Govind
@AshokNaidu

Create the custom field as Match_Billing_Address and inactive the other Triggers which are wrote on Account object .
 
trigger AccountAddressTrigger on Account (before insert, before update) {
    for(Account a : Trigger.New){
        if(a.Match_Billing_Address__c == true){
            a.ShippingPostalCode = a.BillingPostalCode;
        }
    }

}

 
Sandun FonsekaSandun Fonseka
Hi all,
 - First i created the trigger on the developer console
 - Then edited the trigger using the trailhead org (Setup --> Apex Trigger --> edit) to the below version

trigger AccountAddressTrigger on Account (before insert, before update) {
    for(Account a : Trigger.New){
        if(a.Match_Billing_Address__c == true){
            String newSPcode = a.ShippingPostalCode;
            String newBPcode = a.BillingPostalCode;
            newSPcode = newBPcode;
            a.ShippingPostalCode = newSPcode;
            a.BillingPostalCode = newBPcode;
        }
    }
}
Sudeep AgrawalSudeep Agrawal
@Sandun Fonseka : Can you please explain how your code worked, while the simple implementaion as below does not work : 
 
a.ShippingPostalCode = a.BillingPostalCode;

 
Aditya Sharma 151Aditya Sharma 151
Hi All
Modify if condition to include a.BillingPostalCode!=Null
if(a.BillingPostalCode!=Null && a.Match_Billing_Address__c == true)
Maria DesfassiauxMaria Desfassiaux

Hi All! I've completed the challenge using the following snippet of code:

trigger AccountAddressTrigger on Account (before insert, before update) {
    for (Account a : Trigger.new) {
        if (a.Match_Billing_Address__c == True) {
        a.ShippingPostalCode = a.BillingPostalCode;
        } else If (a.Match_Billing_Address__c == False) {
            System.debug('Checkbox not active');
        }
    } 
}
I found that adding the else if statement helped me when encountering an error when trying to complete the challenge: Setting 'Match_Billing_Address__c' to false updated the records anyway. The trigger should only act when Match_Billing_Address__c is true.
 

Hope this helps :)

 

Gautam ChaskarGautam Chaskar
trigger AccountAddressTrigger on Account (before insert, before update) {
    if(Trigger.isInsert){
        for(Account a:Trigger.new){
            IF(a.Match_Billing_Address__c == True && a.BillingPostalCode!=Null){
                a.ShippingPostalCode = a.BillingPostalCode;
            }
        }
    }
    else if(Trigger.isUpdate){
        for(Account a:Trigger.new){
            IF(a.Match_Billing_Address__c == True){
                a.ShippingPostalCode = a.BillingPostalCode;
            }
        }
    }
}
freddy munivefreddy munive
It very importante disable all active Triggers related Account and create Match_Billing_Address__c field before of continue with the challenge. 

In my case it works with the following code:
 
trigger AccountAddressTrigger on Account (before insert, before update) {
  for(Account a : Trigger.new){
      if(a.Match_Billing_Address__c){
         a.ShippingPostalCode = a.BillingPostalCode;
      }
  }
}