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
Yogesh Kamble 2Yogesh Kamble 2 

Hello All, I have just started using Trialhead and got stuck at this module.. Create an Apex trigger for Account that matches Shipping Address Postal Code with Billing Address Postal Code based on a custom field..please help me in this..

Best Answer chosen by Yogesh Kamble 2
Ashish KeshariAshish Keshari
Hi Abderrahman,
Please use this. 
trigger AccountAddressTrigger on Account (before insert, before update) {
    for (Account a : Trigger.New){
        if (a.Match_Billing_Address__c == true && !String.isBlank(a.BillingPostalCode)){
            a.ShippingPostalCode = a.BillingPostalCode;
            System.debug('------' + a.ShippingPostalCode);
        }
    }
}

All Answers

Abderrahman EL HARBYLYAbderrahman EL HARBYLY
trigger AccountAddressTrigger on Account (before insert, before update) {
    for(Account a : Trigger.New) {
        if(a.BillingPostalCode!=null && a.Match_Billing_Address__c==true){
            a.ShippingPostalCode = a.BillingPostalCode;
        }
    }
}
Ashish KeshariAshish Keshari
Hi Abderrahman,
Please use this. 
trigger AccountAddressTrigger on Account (before insert, before update) {
    for (Account a : Trigger.New){
        if (a.Match_Billing_Address__c == true && !String.isBlank(a.BillingPostalCode)){
            a.ShippingPostalCode = a.BillingPostalCode;
            System.debug('------' + a.ShippingPostalCode);
        }
    }
}
This was selected as the best answer
Yogesh Kamble 2Yogesh Kamble 2
Thanks for the reply,

I created checkbox with field label 'Match Billing Address' and it is checked after that created trigger named as 'AccountAddressTrigger' with following code:

trigger AccountAddressTrigger on Account (before insert, before update) {
    for(Account a : Trigger.New) {
        if(a.BillingPostalCode!=null && a.Match_Billing_Address__c==true){
            a.ShippingPostalCode = a.BillingPostalCode;
        }
    }
}
and saving it. I am just clicking check challenge button and it is giving me error as:

Challenge Not yet complete... here's what's wrong: 
No Apex trigger named 'AccountAddressTrigger' was found
please tell me what wrong i am doing here?
Thanks..
Ashish KeshariAshish Keshari
It seems the Trailhead connected to your org has not even the Trigger. It means either you have created the Trigger in a different org ? I would say refresh the Trailhead page and then at the bottom where it says 'Launch your dev org' and make sure it is same where to made the code changes.
Abderrahman EL HARBYLYAbderrahman EL HARBYLY
Hi Yogesh,
I think you didn't created a Apex Trigger, you had created a Class Apex so you must delete this one and creating a new one with the Apex Trigger :
File -> New -> Apex Trigger
Yogesh Kamble 2Yogesh Kamble 2
Ashish bro..your code works absolutely fine.Thanks for the help
Ashish KeshariAshish Keshari
Hi Yogesh.
Can you please mark by response as best answer since it helped you resolving the error ? thanks.
Yogesh Kamble 2Yogesh Kamble 2
I already did it Ashish..
Hi Abderrahman,
I folllowed the same procedure but i created trigger in different org. Now it is working fine.
Thanks
Deep ThackerDeep Thacker
Ashish....I have a doubt so would you help me understand this....

When i saw this challenge, I was confused as I did not see any postal code fields on my account object and I just went on googling for answer. I got your answer and that worked perfectly fine after creating the checkbox property. But I still am unsure that how the postal code came into picture where it shows nowhere on object manager for Account object. 

Then I saw billing and shipping address fields with type as Address, and I thought if you create address type field then it will give you the postal code. And I just went on trying to create one more address type field but I did not find that data type when I clicked on "New" for fields...How did this happen?????

or is it like, Account is an starndard object so these fields just came up with it... (however i did see the postal codes when I tried to creat new account object, under address field section)

Help me to understand how this happened !!!!!