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
Boudewijn TimmersBoudewijn Timmers 

Apex triggers trailhead challenge question

Again I need to ask this as I am not sure what Apex triggers trailhead challenge expects from me. Need to create a trigger that checks if Match_Billing_Address__c is checked and if BillingPostalCode is not empty. In that case set ShippingPostalCode__c to match BillingPostalCode. My code works, it behaves as expected but the challenge won't accept it. 
trigger AccountAddressTrigger on Account (before insert, before update) {
    for(Account a : Trigger.new){
        If(a.Match_Billing_Address__c) {
            If(a.BillingPostalCode != ''){
                a.ShippingPostalCode__c = a.BillingPostalCode;
            }
        }
    }
}

What is wrong with my code?

regards,
boudewijn
Best Answer chosen by Boudewijn Timmers
sfdcMonkey.comsfdcMonkey.com
One modify version 
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 :)

All Answers

sfdcMonkey.comsfdcMonkey.com
Hi try this 
trigger AccountAddressTrigger on Account (before insert, before update) { for(Account a : Trigger.new){ If (a.Match_Billing_Address__c == true) { a.ShippingPostalCode = a.BillingPostalCode; } } }

sfdcMonkey.comsfdcMonkey.com
One modify version 
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 :)
This was selected as the best answer
Boudewijn TimmersBoudewijn Timmers
Hi Soni,

yes thanks, I just found out that I had created a shippingpostalcode__c myself even thoug the standard shipping address compound field already contains a shipping postal code. So upon changing it it worked!

Thanks, Boudewijn