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
Sunny SSunny 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 !


 
Best Answer chosen by Sunny S
Abdul KhatriAbdul Khatri
Hi Sunny

I made few changes to handle null exceptions. I hope it will help
 
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);
        }
    }
    
    if(arParentAccountIds.isEmpty()) return;
   
    // Store the map   
    Map<Id, Account> objAccountChildContact = new Map<Id, Account>([select Id, PersonContactId from Account WHERE Id IN : arParentAccountIds]);
   
    if(objAccountChildContact.isEmpty()) return;
    
    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*/
            if(objAccountChildContact.get(objAccount.Person_Account_Parent__c) != null) {
            	objAccount.Parent__pc = objAccountChildContact.get(objAccount.Person_Account_Parent__c).PersonContactId; 
            	System.debug('Updating contact parent for [' + objAccount.Id + '] to [' + objAccount.Parent__pc +']');
            }
           
        }
    }
}

 

All Answers

Abdul KhatriAbdul Khatri
Hi Sunny

I made few changes to handle null exceptions. I hope it will help
 
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);
        }
    }
    
    if(arParentAccountIds.isEmpty()) return;
   
    // Store the map   
    Map<Id, Account> objAccountChildContact = new Map<Id, Account>([select Id, PersonContactId from Account WHERE Id IN : arParentAccountIds]);
   
    if(objAccountChildContact.isEmpty()) return;
    
    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*/
            if(objAccountChildContact.get(objAccount.Person_Account_Parent__c) != null) {
            	objAccount.Parent__pc = objAccountChildContact.get(objAccount.Person_Account_Parent__c).PersonContactId; 
            	System.debug('Updating contact parent for [' + objAccount.Id + '] to [' + objAccount.Parent__pc +']');
            }
           
        }
    }
}

 
This was selected as the best answer
Sunny SSunny S
Hi Abdul, 
Thanks so much for your help mate, and yes, your suggestion had worked very well 👍
Greatly appreciate your help. Thanks again and best regards