+ Start a Discussion
Gabe RothmanGabe Rothman 

Null pointer exception on Task Trigger (banging my head against the wall - please help)

Summary:

My trigger is supposed to update a custom field on the related Account called Owner_s_Last_Activity_Date__c with the CreatedDate of the Task any time a new task created on the Account is assigned to the Account owner.  The trigger works great in the positive case (i.e. the Related Account lookup is populated), but I'm getting a null pointer exception any time the related Account lookup is null.  I know I need to filter tasks with no related Account out of my trigger logic, but I just can't seem to figure out how to do it.  Any help would be greatly appreciated.  Thanks!

trigger UpdateAccountFromActivity on Task (after insert) {

  Map<ID, Account> parentAccts = new Map<ID, Account>();
  List<Id> listIds = new List<Id>();

  for (Task task : Trigger.new){
    if(task.AccountId!=null)
      listIds.add(task.AccountId);
  }
   
  parentAccts = new Map<Id, Account>([SELECT ID, Owner_s_Last_Activity__c, OwnerID, (SELECT ID, CreatedDate, OwnerId, AccountID FROM Tasks WHERE AccountID!=null ) FROM Account WHERE ID IN :listIds]);

  for (Task task: Trigger.new){
      Account myParentAcc = parentAccts.get(task.AccountId);
      if(myParentAcc.OwnerId == task.OwnerId && task.AccountID != null){
       myParentAcc.Owner_s_Last_Activity__c = task.CreatedDate;
        }     
      }  
   update parentAccts.values();
}

Best Answer chosen by Gabe Rothman
Vinit_KumarVinit_Kumar
Try below code :- 
trigger UpdateAccountFromActivity on Task (after insert) {

  Map<ID, Account> parentAccts = new Map<ID, Account>();
  List<Id> listIds = new List<Id>();

  for (Task task : Trigger.new){
    if(task.AccountId!=null )
      listIds.add(task.AccountId);
  }
   
  parentAccts = new Map<Id, Account>([SELECT ID, Owner_s_Last_Activity__c, OwnerID FROM Account WHERE ID IN :listIds]);

  for (Task task: Trigger.new){
	 if(task.AccountID != null && !parentAccts.IsEmpty())
	 {
      Account myParentAcc = parentAccts.get(task.AccountId);
      if(myParentAcc.OwnerId == task.OwnerId)
	    {
          myParentAcc.Owner_s_Last_Activity__c = task.CreatedDate;
        }     
      } 
}
   if(!parentAccts.IsEmpty())	  
   {
      update parentAccts.values();
   }  
}
If this helps,please mark this as best answer to help others :)

All Answers

varun_vatsavarun_vatsa

please replace the code section with the below given code.

for (Task task: Trigger.new){
      if(task.AccountId != null){
          Account myParentAcc = parentAccts.get(task.AccountId);
              if(myParentAcc.OwnerId == task.OwnerId && task.AccountID != null){
                  myParentAcc.Owner_s_Last_Activity__c = task.CreatedDate;
                 }   
           } 
      } 
   
Vinit_KumarVinit_Kumar
Try below code :- 
trigger UpdateAccountFromActivity on Task (after insert) {

  Map<ID, Account> parentAccts = new Map<ID, Account>();
  List<Id> listIds = new List<Id>();

  for (Task task : Trigger.new){
    if(task.AccountId!=null )
      listIds.add(task.AccountId);
  }
   
  parentAccts = new Map<Id, Account>([SELECT ID, Owner_s_Last_Activity__c, OwnerID FROM Account WHERE ID IN :listIds]);

  for (Task task: Trigger.new){
	 if(task.AccountID != null && !parentAccts.IsEmpty())
	 {
      Account myParentAcc = parentAccts.get(task.AccountId);
      if(myParentAcc.OwnerId == task.OwnerId)
	    {
          myParentAcc.Owner_s_Last_Activity__c = task.CreatedDate;
        }     
      } 
}
   if(!parentAccts.IsEmpty())	  
   {
      update parentAccts.values();
   }  
}
If this helps,please mark this as best answer to help others :)
This was selected as the best answer
Gabe RothmanGabe Rothman
Thanks for you help, guys! You've solved my problem