Jawad Ahmed
Jawad Ahmed

Reputation: 1

Attempt to dereference a Null object Salesforce

I have made a trigger to update the Contact ID in person account and its working for person accounts it is populating the Contact ID field on person account.

The problem is it is giving an error on business and employer account record type i have also inserted a check for record type. But still its giving an error

on Error: Invalid Data.

Review all error messages below to correct your data.

Apex trigger UpdateContactID caused an unexpected exception, contact your administrator: UpdateContactID: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.UpdateContactID: line 41, column 1

The trigger is below:

trigger UpdateContactID on Account (after insert, after update) 
{

 //Identify applicable record type
    RecordType PersonAccount = [SELECT Id FROM RecordType WHERE SobjectType='Account' AND Name = 'Career Champion Account' limit 1];

 List<Account> toUpdate = new List<Account>();

if(trigger.isInsert) 
{



 List<String> newAccountIDList = new List<String>();


 // Taking all account IDs in collection
 for(Account acct: Trigger.new)
 {  
  newAccountIDList.add(acct.ID); 
 }

 // Fetching contacts against the account IDs
 List<Contact> contactList = [SELECT Id, Account.Id FROM Contact WHERE Account.ID in :newAccountIDList];      


 // Adding contacts in a map with relation to Account ID
 Map<String, Contact> mapContact = new Map<String, Contact>();
 for(Contact cont : contactList)
 {
  mapContact.put(cont.Account.Id, cont) ;
 }


 // Updating Contact_ID__c from Map to new Account list to update
 List<Account> newAccounts = [select Id, Contact_ID__c from Account where Id in :newAccountIDList];

 for(Account acct: newAccounts)
 {  
 **LINE 41**  
toUpdate.add(new Account(
  id = acct.Id,
  Contact_ID__c = mapContact.get(acct.Id).Id
 ));
}

update toUpdate;

} // if
else if(trigger.isUpdate && trigger.new[0].Contact_ID__c == null && trigger.old[0].Contact_ID__c == null) 
{



 List<String> newAccountIDList = new List<String>();


 // Taking all account IDs in collection
 for(Account acct: Trigger.new)
 {  
  newAccountIDList.add(acct.ID); 
 }

 // Fetching contacts against the account IDs
 List<Contact> contactList = [SELECT Id, Account.Id FROM Contact WHERE Account.ID in :newAccountIDList];      


 // Adding contacts in a map with relation to Account ID
 Map<String, Contact> mapContact = new Map<String, Contact>();
 for(Contact cont : contactList)
 {
  mapContact.put(cont.Account.Id, cont) ;
  }


 // Updating Contact_ID__c from Map to new Account list to update
 List<Account> newAccounts = [select Id, Contact_ID__c from Account where Id in :newAccountIDList];
// List<Account> toUpdate = new List<Account>();
 for(Account acct: newAccounts)
 {  

  toUpdate.add(new Account(
   id = acct.Id,
   Contact_ID__c = mapContact.get(acct.Id).Id
  ));
 }

 update toUpdate;

 } // else if

} // trigger

Upvotes: 0

Views: 2478

Answers (1)

Daniel Ballinger
Daniel Ballinger

Reputation: 13537

Most likely mapContact does not contain a value for the key acct.Id. The mapContact.get(acct.Id) call will return null and the following .Id will give the null reference exception.

You should add a guard statement before that line. E.g.

if(mapContact.containsKey(acct.Id)) {
    toUpdate.add(new Account(
      id = acct.Id,
      Contact_ID__c = mapContact.get(acct.Id).Id
     ));
}

Upvotes: 0

Related Questions