You need to sign in to do that
Don't have an account?
M 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;
}
}
}
"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;
}
}
}
All Answers
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
@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.
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
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: Anyone please guide where I missed the part? Thanks in advance.
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?
hope this will help you.
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.
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;
}
}
}
Create the custom field as Match_Billing_Address and inactive the other Triggers which are wrote on Account object .
- 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;
}
}
}
Modify if condition to include a.BillingPostalCode!=Null
if(a.BillingPostalCode!=Null && a.Match_Billing_Address__c == true)
Hi All! I've completed the challenge using the following snippet of code:
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 :)
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;
}
}
}
}
In my case it works with the following code:
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; } } }
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;
}
}
}
}
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
i had a same problem.
It helped me to do challenge in a new playground.
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
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;
}
}
}
}
}