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

Error when trying to create a new Account with a trigger
Hi
I have created an apex trigger to update contact address when the account address is changed. The code worked fine for already created accounts, but when I have created a new account the following error was thrown and I was not able to save the account. Please help me.
Error: updateContact: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.updateContact: line 33, column 1
I have created an apex trigger to update contact address when the account address is changed. The code worked fine for already created accounts, but when I have created a new account the following error was thrown and I was not able to save the account. Please help me.
Error: updateContact: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.updateContact: line 33, column 1
trigger updateContact on Account (after insert, after update) { List<Contact> contactsForUpsert = New List<Contact>(); Map<Id, Contact> contactsByAccountId = new Map<Id, Contact>(); List<Contact> contacts = [SELECT Id, AccountId, mailingstreet, mailingcity, mailingstate, mailingpostalcode FROM Contact WHERE AccountId IN: Trigger.newMap.keySet()]; for (Contact cont: contacts) { contactsByAccountId.put(cont.accountId, cont); } for (Account account: Trigger.new) { Account old; if (Trigger.isUpdate) { old = Trigger.oldMap.get(account.Id); } Boolean isShippingAddressChangedOrNew = Trigger.isInsert ? true : ( account.ShippingStreet != old.ShippingStreet || account.ShippingCity != old.ShippingCity || account.ShippingState != old.ShippingState || account.ShippingPostalCode != old.ShippingPostalCode || account.ShippingCountry != old.ShippingCountry) ? true : false; if (isShippingAddressChangedOrNew) { Contact relatedContact = contactsByAccountId.get(account.Id); relatedContact.mailingstreet = account.Shippingstreet; relatedContact.mailingcity = account.Shippingcity; relatedContact.mailingstate = account.Shippingstate; relatedContact.mailingpostalcode = account.shippingpostalcode; contactsForUpsert.add(relatedContact); } try { upsert contactsForUpsert; } catch(Dmlexception e) { System.debug(LoggingLevel.ERROR, 'Contact insert from createopp.trigger has failed with message:' + e.getMessage()); } } }
u can not run this trigger on 'after insert' cause first we have to insert an account only then we can add a contact to it
so it will run only on update operation. so remove 'after insert'(PLZ CORRECT ME IF I AM WRONG)
All Answers
Please remove after insert event on trigger.
trigger updateContact on Account ( after update) {
Hope this helps you.
Thanks
Varaprasad
@For Support : varaprasad4sfdc@gmail.com
u can not run this trigger on 'after insert' cause first we have to insert an account only then we can add a contact to it
so it will run only on update operation. so remove 'after insert'(PLZ CORRECT ME IF I AM WRONG)
It's my bad that I didn't notice 'after insert'. Now I'm able to ad the accounts.
Thanks for the help