+ Start a Discussion
VENKATA RANGAVENKATA RANGA 

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

Hi All,
I'm new to salesforce and started working on Trailhead's. I facing issue with the Trigger Challenge. Here is the triger code im writing 

trigger AccountAddressTrigger on Account (before insert) {
    for(Account a : Trigger.new){
        if(a.Match_Billing_Address__c && a.BillingPostalCode != null){
            a.ShippingPostalCode = a.BillingPostalCode;
        }
    }
    
}

but i'm getting the following error. Not sure where i'm doing wrong. can someone please help me here. Thanks in advance.

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, TestTrigger: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.TestTrigger: line 3, column 1: []
Best Answer chosen by VENKATA RANGA
JyothsnaJyothsna (Salesforce Developers) 
Hi Ranga,

Please check the below trigger.
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 check you any trigger you have on Account object which is creating opportunity ? If yes then please deactivate the trigger and try again.

Please try to de-activate all other triggers and validation rules on Account object and try again.

Let us know if that will helps you!
Best Regards,
Jyothsna

 

All Answers

JyothsnaJyothsna (Salesforce Developers) 
Hi Ranga,

Please check the below trigger.
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 check you any trigger you have on Account object which is creating opportunity ? If yes then please deactivate the trigger and try again.

Please try to de-activate all other triggers and validation rules on Account object and try again.

Let us know if that will helps you!
Best Regards,
Jyothsna

 
This was selected as the best answer
PavanKPavanK
Can you update code line

a.Match_Billing_Address__c

Wit

a.Match_Billing_Address__c!=null

Considering Match_Billing_Address__c field is not checkboox type.

I Would say remove this condition and keep only billingpostcode!=null condition as other condition look irrelevant to me.

Please mark this answer as best if it resolved issue.
VENKATA RANGAVENKATA RANGA
Thanks pavan for your contribution but it did not work. I don't think its has anything to do with the condition (actually trailhead challenge itself require to check that condition). I believe it has something to do with the way I'm accessing the records. But there is also a very similar example I wrote and that worked.
PavanKPavanK
Thanks best of luck with your trailhead. Enjoy
VENKATA RANGAVENKATA RANGA
Jyothsna,
Thanks much. It worked after deactivating the other triggers i created on this Account Object. So we can have only one trigger of same type?(For example: before insert). But when I had some debug statements I can see the values being populated in the Trigger.New. Any idea what is causing nullpointer exception 
Jess BurghJess Burgh
Hey all. Still trying to figure this challenge out. I keep getting this error message, even after trying to create an Account Object. Plus I deactivated my other Account Triggers.  User-added image
Not sure what I'm doing wrong. Help!!
Vik SerbVik Serb
Jess Burgh
Create a Custom field in Account object, with API name Match_Billing_Address__c and type checkbox.
Nagaraju Mogili 31Nagaraju Mogili 31
Challenge Not yet complete... here's what's wrong: 
Setting 'Match_Billing_Address__c' to false updated the records anyway. The trigger should only act when Match_Billing_Address__c is true.

I am getting the above error, could anyone help on this... Please find the below code..


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;
        }   
    } 

}

Regards,
Nagaraju Mogili
kishan patel 32kishan patel 32

Hi Nagaraju Mogili,
we need to check type and equality of objects in Trigger. Everything is ok in your code just check your IF Condition for
If (a.Match_Billing_Address__c == true && a.BillingPostalCode!=Null)

You need to use == to check type conversion of object. after making changes and save Trigger your trigger is works for given condition and you can able to pass Challenge.

trigger AccountAddressTrigger on Account (before insert, before update) {
    List<Account> acct = new List <Account>();
    for(Account a: Trigger.new){
        if( a.Match_Billing_Address__c == true && a.BillingPostalCode!=null ){
            a.ShippingPostalCode = a.BillingPostalCode;
        }
    }

}


Mark as solution if it helps you.

Regards,

Kishan Patel

yash mehta 12yash mehta 12
Getting ErrorHow to solve
yash mehta 12yash mehta 12
Please reply to my question
kishan patel 32kishan patel 32

Hi Yash,
In order to work above code you need to create Custom object on Account.

In Lightininig Experience,
Go to Object Manager -> Account -> Field&Relationship -> Choose Field type as "Checkbox".

Create New Custom object with field label Match_Billing_Address and it's API should be Match_Billing_Address__c

This checkbox match for Billing and Shipping Address. After creating Custom object you can check your above code and it works for you.

Let me know If you can find any difficulty in this.

Mark/Like if your question is solved for others to understand better.


Thanks,

Kishan

yash mehta 12yash mehta 12
Hi Kishan I already created checkbox but after that also same problem occurs.