You need to sign in to do that
Don't have an account?

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: []
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: []
Please check the below trigger.
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
Please check the below trigger.
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
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.
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
Not sure what I'm doing wrong. Help!!
Create a Custom field in Account object, with API name Match_Billing_Address__c and type checkbox.
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
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
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
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
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.
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;
}
}
}
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
trigger AccountAddressTrigger on Account (before insert, before update) {
for(Account a : Trigger.New) {
if( a.Match_Billing_Address__c == true){
a.ShippingPostalCode = a.BillingPostalCode;
}
}
}
{
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.
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.
for(Account a : Trigger.New) {
if(a.Match_Billing_Address__c){
a.ShippingPostalCode=a.BillingPostalCode;
}
}
}
How can I complete the challenge, and why is there a discrepency between the object manager and the developer console?
@yash Mehta 12
Create a Custom field in Account object, with API name Match_Billing_Address__c and type with checkbox.
for (Account acc: Trigger.New) {
if (acc.BillingPostalCode != null && acc.Match_Billing_Address__c) {
acc.ShippingPostalCode = acc.BillingPostalCode;
}
}
}
thanks rupak dey its working 100%
trigger AccountAddressTrigger on Account (before insert, before update) {
for(account a:trigger.new){
if(a.Match_Billing_Address__c){
a.ShippingPostalCode=a.BillingPostalCode;
}
}
}
{
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
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;
}
}
}
}
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.
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;
}
}
}
}