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
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.
Vikrum JaitlyVikrum Jaitly
Its done. 
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 
then create the trigger in Developer Console
Celio XavierCelio Xavier
Segue a minha solução, e não esqueçam de criar o checkbox (Match Billing Address) solicitado no exercício, e para testar crie uma conta indicando o BillingPostalCode e logo verá que a trigger irá igualar o valor do campo ShippingPostalCode. 
 
trigger AccountAddressTrigger on Account (before insert,before update) {
    for(account add:Trigger.New){		
    		if(add.Match_Billing_Address__c == True)
           	add.ShippingPostalCode = add.BillingPostalCode; 
		}
}

 
Rupak DeyRupak Dey
trigger AccountAddressTrigger on Account (before insert, before update) {
    for (Account acc: Trigger.New) {
        if (acc.BillingPostalCode != null && acc.Match_Billing_Address__c) {
            acc.ShippingPostalCode = acc.BillingPostalCode;
        }
    }
}

This one is working for me, Try this out.
Sayed Sajid AliSayed Sajid Ali
Try this it works:

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;
        }
    }
}
Jawad AfzalJawad Afzal
Hi,
Below check my first experience.


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

}

thanks
Walid RomdhaniWalid Romdhani



trigger AccountAddressTrigger on Account (before insert, before update) {
    for(Account a : Trigger.New) {
        
            if( a.Match_Billing_Address__c == true){
                
                a.ShippingPostalCode = a.BillingPostalCode;
        
    }
    } 

    
    
    

}
Abhinav Yadhuvanshi 5Abhinav Yadhuvanshi 5
trigger AccountAddressTrigger on Account (before insert , before update)
{
    
    List<Account> accList = new List<Account>();
    
    for(Account a : Trigger.new)
    {
      
        if(a.Match_Billing_Address__c == true)
                {

                      a.ShippingPostalCode = a.BillingPostalCode;
            
                } 
        
       }
}


Hi Everone,
Try this code this every easy to understand and Please forget to like it if you like my code.

 
ankit garg 128ankit garg 128
trigger AccountAddressTrigger on Account (before insert,before update) {
    for(account acc: Trigger.NEW){
        if(acc.Match_Billing_Address__c == true){
            acc.ShippingPostalCode = acc.BillingPostalCode;
        }
    }
    
}

Hi Everone,
Try this code this every easy to understand and Please dont forget to like it if you like my code.

 
Tejiri IbihwioriTejiri Ibihwiori
trigger AccountAddressTrigger on Account (before insert) {
         for(Account a : Trigger.New) {
             if(a.Match_Billing_Address__c){
               a.ShippingPostalCode=a.BillingPostalCode; 
             }
        
    }
}
somp9somp9
I did the steps above, and deactivated the other triggers, it still gives the error that the field doesn't exist, even though it appears in the object manager in Account's fields. On the other hand, the field doesn't show up in developer console in file/open. 
How can I complete the challenge, and why is there a discrepency between the object manager and the developer console?
 
Ganta SureshGanta Suresh

@yash Mehta 12 

Create a Custom field in Account object, with API name Match_Billing_Address__c and type with checkbox.

srujith chintha 4srujith chintha 4
trigger AccountAddressTrigger on Account (before insert, before update) {
    for (Account acc: Trigger.New) {
        if (acc.BillingPostalCode != null && acc.Match_Billing_Address__c) {
            acc.ShippingPostalCode = acc.BillingPostalCode;
        }
    }
}

thanks rupak dey its working 100%
Natalia BannikovaNatalia Bannikova
It's right:
trigger AccountAddressTrigger on Account (before insert, before update) {
        for(account a:trigger.new){
            if(a.Match_Billing_Address__c){
                a.ShippingPostalCode=a.BillingPostalCode;        
            }
        }
}
Avinash Patnaik 9Avinash Patnaik 9
how can one  find the  field name ShippingPostalCode from the account object in salesforce. it doesnt show up in Set up --> objects --> account --> Fields and Relationships. 
User-added image

 
Gaurav BhingareGaurav Bhingare
trigger AccountAddressTrigger on Account (before insert, before update)
{
    if((trigger.isBefore) && (trigger.isInsert || trigger.isUpdate))
       {
           for (account acc:trigger.new)
           {
               if(acc.Match_Billing_Address__c == true) 
                   acc.ShippingPostalCode = acc.BillingPostalCode;
           }
               
       }

}

use this code it will work
Nazli FatimaNazli Fatima
 Create an Apex trigger
Pre-Work:
    Add a checkbox field to the Account object:
        Field Label: Match Billing Address
        Field Name: Match_Billing_Address
        Note: The resulting API Name should be Match_Billing_Address__c.


trigger AccountAddressTrigger on Account (before insert, before update)
{
    if(trigger.isBefore && (trigger.isInsert || trigger.isUpdate) ){
        for(Account acc: trigger.new)
        {
            if(acc.Match_Billing_Address__c==true)
            {
                acc.ShippingPostalCode=acc.BillingPostalCode;
            }
        }
    }
}
Aman Verma 92Aman Verma 92
can someone help I have an error in this question 
We updated an account that had 'Match_Billing_Address__c' set to false. We expected the Apex trigger not to fire, but it did. Make sure the Apex trigger fires only if 'Match_Billing_Address__c' is true.

 
Subbalakshmi ValluriSubbalakshmi Valluri
Create a Seperate ORg for PD2 related modules
Create the Checkbox : default :Checked
Trigger Code :

trigger AccountAddressTrigger on Account (before insert, before update)
{
    if(trigger.isBefore && (trigger.isInsert || trigger.isUpdate) ){
        for(Account acc: trigger.new)
        {
            if(acc.Match_Billing_Address__c==true)
            {
                acc.ShippingPostalCode=acc.BillingPostalCode;
            }
        }
    }
}
Ayushi Agarwal 18Ayushi Agarwal 18
trigger AccountAddressTrigger on Account (before insert, before update)
{
    List<Account> accList = new List<Account>();
    for (Account acc : trigger.new)
    {
         if (acc.BillingPostalCode != null && acc.Match_Billing_Address__c == true) 
        {
            acc.ShippingPostalCode = acc.BillingPostalCode;
        }
    }  
}

With this code, Im facing this error - "We updated an account that had 'Match_Billing_Address__c' set to false. We expected the Apex trigger not to fire, but it did. Make sure the Apex trigger fires only if 'Match_Billing_Address__c' is true."
sravani proddatur 10sravani proddatur 10
Hi Ranga,

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