+ Start a Discussion
subramanyam karanamvsubramanyam karanamv 

Apex trigger for Account that matches Shipping Address Postal Code with Billing Address Postal Code based on a custom field

Hi All , 

I am facing the below error while trying to complete the Trial head challenge 

Challenge : Create an Apex trigger for Account that matches Shipping Address Postal Code with Billing Address Postal Code based on a custom field

Error : Setting 'Match_Billing_Address__c' to false updated the records anyway. The trigger should only act when Match_Billing_Address__c is true.

Thanks 
K.VenkataSubramanyam
Khan AnasKhan Anas (Salesforce Developers) 
Hi Subramanyam,

Greetings to you!

Please try below code:
trigger AccountAddressTrigger on Account (before insert,before update) {
   
    for(Account a : trigger.new) {
        if(a.Match_Billing_Address__c) {
            a.ShippingPostalCode = a.BillingPostalCode;
        }
    }        
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
subramanyam karanamvsubramanyam karanamv
Hi Anas , 
Now i am getting the below error 

Setting 'Match_Billing_Address__c' to false updated the records anyway. The trigger should only act when Match_Billing_Address__c is true.

Regards
Subramanyam
Khan AnasKhan Anas (Salesforce Developers) 
Please follow below steps:
Create a custom field: Setup-> Object Manager-> Account -> Fields and Relationships 
Select New --> Field Type (Checkbox)
Enter Name as Match Billing Address 
Checkbox  should be Unchecked 
Save.
Now write the below code, it is working without any errors
trigger AccountAddressTrigger on Account (before insert , before update) {
    for(Account a:Trigger.New){
        if(a.Match_Billing_Address__c && (a.BillingPostalCode != null || a.BillingPostalCode!=' ' )){
           a.ShippingPostalCode = a.BillingPostalCode;
        }
    }
}

I hope it helps you!
subramanyam karanamvsubramanyam karanamv
Thanks Anas , It Works 
Ajay K DubediAjay K Dubedi
Hi Subramanyam,

* Please try below code it works fine:
 
trigger Match_Shipping_And_Billing_Address on Account(before insert,before update) 
    {
        for(Account ac : Trigger.new) 
        {
            if(a.Match_Billing_Address__c == True)
            {
                a.ShippingPostalCode = a.BillingPostalCode;
            }
        }        
    }


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com
 
Deepali KulshresthaDeepali Kulshrestha
Hi Subramanyam,

I have gone through your question. It giving an error because in your trigger you not check the condition Match_Billing_Address__c is True or not.
First thing is that you update your trigger.

Please use the code given below:-
trigger AccountAddressTrigger on Account (before insert , before update) {

    for(Account ac : Trigger.new){
    
        if(ac.Match_Billing_Address__c == true && ac.BillingPostalCode != null ){
        
            ac.ShippingPostalCode = ac.BillingPostalCode;
            
        }
    }
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
 
Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com