• Pavel Zaverach 11
  • NEWBIE
  • 0 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
Hi I have written a trigger that will update a field on Account when the child record is created or the first child records (on Account) will get updated. However, my test class is failing on the de-refence a null object and I'm getting 91% coverage. how? 

Error Message:

System.DmlException: Upsert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, NewJobTimeStampOnAccount: execution of AfterInsert

caused by: System.NullPointerException: Attempt to de-reference a null object

Trigger.NewJobTimeStampOnAccount: line 22, column 1: []

Trigger:
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
trigger NewJobTimeStampOnAccount on Job__c (after insert, after update) {

  Map<ID, Account> parentAcct = new Map<ID, Account>(); //Making it a map instead of list for easier lookup
  List<Id> listIds = new List<Id>();
 
if(trigger.isInsert || trigger.isUpdate){
  for (Job__c childObj : Trigger.new){
    listIds.add(childObj.Account__c);
  }
}
  //Populate the map. Also make sure you select the field you want to update, amount
  //The child relationship is more likely called jobs__r (not job__r) but check
  //You only need to select the child jobs if you are going to do something for example checking whether the job in the trigger is the latest
  parentAcct = new Map<Id, Account>([SELECT id, New_Job_Start_Date__c, (SELECT ID, Date_Time_Taken__c FROM Jobs__r) FROM Account WHERE ID IN :listIds]);
 
  List<Job__c> i = [select id from Job__c where Account__c in :listIds order by Date_Time_Taken__c ASC limit 5];

  for (Job__c job: Trigger.new){
     if(i[0].id == job.id)
     {
        Account myParentAcct = parentAcct.get(job.Account__c);
        myParentAcct.New_Job_Start_Date__c = job.Date_Time_Taken__c;
       
     }
  }
  update parentAcct.values();

}

Test class:

@isTest
private class NewJobTimeStampOnAccount_Test {

    static testmethod void test_trigger(){
     
        Job__c job = new Job__c(Job_Name__c = 'Roy,Semira tes', Date_Time_Taken__c = datetime.now(), contact__c = '003S000000m4qD5', Account__c = '001S000000gq5Dp', Project_manager__c = '00570000002rgiK', name = '6##-##-02206', Stage__c = 'Qualification', Status__c = 'Opportunity', Office__c = 'Chicago', Lead_Source__c = 'Agent', Job_class__c = 'Apartment', County__c = 'Orange', City_of_LA__c = 'No');
      
        upsert job;
      
    }
}