You need to sign in to do that
Don't have an account?
Sunny S
Trigger returning error
Hello friends, need suggestion please.
I have inherited a trigger written by my predecessor which is throwing exception error. Can someone help in getting this sorted? What is the reason of getting this error ?
****** Code Start ******
trigger OnAccountTrigger on Account (before update, before insert) {
// Create the map to store mappings
//Map<String, String> objParentAccountMapping = new Map<String, String>();
// Create set to store ids
Set<Id> arParentAccountIds = new Set<Id>();
// get all the Ids for the accounts that are inserted in that execution context.
for(Account objAccount : Trigger.New) {
// Check if we have a person
if ((objAccount.Person_Account_Parent__c != null) && (objAccount.IsPersonAccount)) {
// Retrieve the related contact
// Retrieve the parent account from objAccount.Person_Account_Parent__c
// Update the local contact from the objAccount.PersonContactId of that retrieved parent
arParentAccountIds.add(objAccount.Person_Account_Parent__c);
}
}
// Store the map
Map<Id, Account> objAccountChildContact = new Map<Id, Account>([select Id, PersonContactId from Account WHERE Id IN : arParentAccountIds]);
System.debug('Map');
System.debug(objAccountChildContact);
// get all the Ids for the accounts that are inserted in that execution context.
for(Account objAccount : Trigger.New) {
// Check if we have a person
if ((objAccount.Person_Account_Parent__c != null)) {
// Retrieve the related contact
// Retrieve the parent account from objAccount.Person_Account_Parent__c
// Update the local contact from the objAccount.PersonContactId of that retrieved parent
// Set the contact
/*below in red is line 32*/
objAccount.Parent__pc = objAccountChildContact.get(objAccount.Person_Account_Parent__c).PersonContactId;
System.debug('Updating contact parent for [' + objAccount.Id + '] to [' + objAccount.Parent__pc +']');
}
}
}
Error Message:
Apex script unhandled trigger exception by user/organization:
OnAccountTrigger: execution of BeforeUpdate
caused by: System.NullPointerException: Attempt to de-reference a null object
Trigger.OnAccountTrigger: line 32, column 1
***** Code End *****
Thanks in advance !
I have inherited a trigger written by my predecessor which is throwing exception error. Can someone help in getting this sorted? What is the reason of getting this error ?
****** Code Start ******
trigger OnAccountTrigger on Account (before update, before insert) {
// Create the map to store mappings
//Map<String, String> objParentAccountMapping = new Map<String, String>();
// Create set to store ids
Set<Id> arParentAccountIds = new Set<Id>();
// get all the Ids for the accounts that are inserted in that execution context.
for(Account objAccount : Trigger.New) {
// Check if we have a person
if ((objAccount.Person_Account_Parent__c != null) && (objAccount.IsPersonAccount)) {
// Retrieve the related contact
// Retrieve the parent account from objAccount.Person_Account_Parent__c
// Update the local contact from the objAccount.PersonContactId of that retrieved parent
arParentAccountIds.add(objAccount.Person_Account_Parent__c);
}
}
// Store the map
Map<Id, Account> objAccountChildContact = new Map<Id, Account>([select Id, PersonContactId from Account WHERE Id IN : arParentAccountIds]);
System.debug('Map');
System.debug(objAccountChildContact);
// get all the Ids for the accounts that are inserted in that execution context.
for(Account objAccount : Trigger.New) {
// Check if we have a person
if ((objAccount.Person_Account_Parent__c != null)) {
// Retrieve the related contact
// Retrieve the parent account from objAccount.Person_Account_Parent__c
// Update the local contact from the objAccount.PersonContactId of that retrieved parent
// Set the contact
/*below in red is line 32*/
objAccount.Parent__pc = objAccountChildContact.get(objAccount.Person_Account_Parent__c).PersonContactId;
System.debug('Updating contact parent for [' + objAccount.Id + '] to [' + objAccount.Parent__pc +']');
}
}
}
Error Message:
Apex script unhandled trigger exception by user/organization:
OnAccountTrigger: execution of BeforeUpdate
caused by: System.NullPointerException: Attempt to de-reference a null object
Trigger.OnAccountTrigger: line 32, column 1
***** Code End *****
Thanks in advance !
I made few changes to handle null exceptions. I hope it will help
All Answers
I made few changes to handle null exceptions. I hope it will help
Thanks so much for your help mate, and yes, your suggestion had worked very well 👍
Greatly appreciate your help. Thanks again and best regards