You need to sign in to do that
Don't have an account?
Mark Erdeljac
Getting Started with Apex Triggers Issue/Question
I was working on the Getting Started with Apex Triggers and my code doesn't seem to be working, could someone please, help me find the error. I have stared at it for awhile and can't seem to figure out where my mistake is, any help that can be provided would be greatly appreciated, thank you in advance for your help and cooperation.
trigger AccountAddressTrigger on Account (before insert, before update) {
for (Account a : [SELECT BillingPostalCode, ShippingPostalCode FROM Account WHERE BillingPostalCode != null AND Match_Billing_Address__c = TRUE AND Id IN :Trigger.New]) {
a.ShippingPostalCode = a.BillingPostalCode;
}
}
And my error is: Challenge not yet complete... here's what's wrong:
Setting 'Match_Billing_Address__c' to true did not update the records as expected.
trigger AccountAddressTrigger on Account (before insert, before update) {
for (Account a : [SELECT BillingPostalCode, ShippingPostalCode FROM Account WHERE BillingPostalCode != null AND Match_Billing_Address__c = TRUE AND Id IN :Trigger.New]) {
a.ShippingPostalCode = a.BillingPostalCode;
}
}
And my error is: Challenge not yet complete... here's what's wrong:
Setting 'Match_Billing_Address__c' to true did not update the records as expected.
As a common practice, if your question is answered, please choose 1 best answer.
But you can give every answer a thumb up if that answer is helpful to you.
Thanks
All Answers
As a common practice, if your question is answered, please choose 1 best answer.
But you can give every answer a thumb up if that answer is helpful to you.
Thanks
"iterate over the entire collection of records" --> yes but only for records that are changed so for 99% of the situations, the number of records is 1.
Only when you have batch or other jobs/program like importing data, will you have more than 1. But if you import you should look into turning your triggers off otherwise it will be SLOW.
Your code does not buy you anything except errors and headaches since your are trying to recreate what you already have by using the IN :Trigger.New clause.
Thx.
"yes but only for records that are changed so for 99% of the situations, the number of records is 1." - So your saying its ok for it to crash that 1% of the time? I totally disagree. Also who is to say that 99% of the time its a single record? Different orgs operate differently. Some orgs do mass uploads and updates daily.
"But if you import you should look into turning your triggers off otherwise it will be SLOW." - This is just not good advice. It is not at all best practice to deactivating triggers for bulk operations. What is the point of having triggers if you have to turn them off for bulk operatons? Your triggers should be bulkified so whether you are updating 1 or 10,000 records should not make a difference. If you need to deactivate your triggers to do bulk operations, then you have poorly written triggers.
@Mark, again, the code that @william provided is sound and works. I just wanted to make sure you didn't think that you should be deactivating triggers to do bulk operations. That is not at all recomended or best practice.
for ( Account a : Trigger.New ){
if( a . Match_Billing_Address__c == true) {
a . ShippingPostalCode = a . BillingPostalCode;
}
}
}
Iterating through the Account object is needed because this trigger happens BEFORE INSERT and BEFORE UPDATE. We don't know which record we should pinpoint and so in order to check if any Account record has a Billing Postal Code and the 'Match_Billing_Address__c' is set as true, you will need to scan through all records in your Account object. In terms of performance, it will definitely slow down performance but that's outside the scope of this trailhead question. The purpose here is to learn how to write a simple trigger function.
@William Tan
You forgot to check if the BillingPostalCode is empty. Below is what I think the condition in the if statement should look like:
if (a.BillingPostalCode != null && a.Match_Billing_Address__c == true)
If you have some BillingPastalCode in the Acoount, then you can write the code without checking the BillingPastalCode.
Your code also right you can check both BillingPastalCode and Match_Billing_Address__c
Thanks