You need to sign in to do that
Don't have an account?
Jason 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;
}
}
}
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;
}
}
}
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
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;
}
}
}
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);
}
}
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);
}
}
}
}
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);
}
}
}
}
@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.
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.
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
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;
}
}
"Try this code."
If you find your Solution then mark this as the best answer.
Thank you!
Regards
Suraj Tripathi