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

 
Alberto AcostaAlberto Acosta
You should errase the  trigger called AddRealatedRecord. 
In my cas It  worked with this 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; } } }
Anup Sharma 31Anup Sharma 31
Please try this code as well,

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

    }
    
}
 
Sosthene Grosset-Janin 6Sosthene Grosset-Janin 6

Hello Team,
In my case the variables ( Shipping Postal Code and Billing Postal Code) do not exist at the Account object level within my Trailhead Playroung.
Therefore I am unlikely to pass this challenge ever

Any suggestions please?
P.S I tried creating: Account.ShippingPostalCode__c  and Account.BillingPostalCode__c
But I know that this will never validate this challenge.

Thanks

Martin SojakMartin Sojak
Hello,
i had a same problem.
It helped me to do challenge in a new playground.
Ishan Singh 15Ishan Singh 15
I am using the below code for Getting Started with Apex Triggers' Challenge.

trigger AccountAddressTrigger on Account (before insert, before update) {
     for(Account a : Trigger.new) {
      If (a.Match_Billing_Address__c == true) {
         a.ShippingPostalCode = a.BillingPostalCode;
      }
    }  
}
But Getting An Error - "We updated an account that had 'Match_Billing_Address__c' set to false. We expected the Apex trigger not to fire, but it did. Make sure the Apex trigger fires only if 'Match_Billing_Address__c' is true."

Please Help
Jiwesh ChauhanJiwesh Chauhan
trigger AccountAddressTrigger on Account (before insert, before update) {
    Switch on Trigger.operationType{
        
        when BEFORE_INSERT{         
            for (Account a : Trigger.new){
                if(a.Match_Billing_Address__c == true && a.BillingPostalCode != Null){
                a.ShippingPostalCode = a.BillingPostalCode; 
               }
            }  
        }
        When BEFORE_UPDATE{
            for (Account a : Trigger.new){      
                if(a.Match_Billing_Address__c == true && a.BillingPostalCode != Null){
                a.ShippingPostalCode = a.BillingPostalCode; 
               }
            }
        }
    }
}