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
Kareem Miller 6Kareem Miller 6 

Code coverage and test class issue

I have create a trigger that creates a custom object record (Training__c ) whenever the formula field Training Id (on Tasks) equals 1, and the task is completed. The code seems to be working fine in my sandbox however when I go to implement the test class I run into two issues: 

Issue number 1: When I run my test class I don't get any code coverage on the trigger after running it.
Issue number 2: When I try to deploy the trigger and class I get the following error:
       
System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: [] 
Stack Trace: Class.insertTraining.insertTraining: line 26, column 1.

I'm not quite sure what the Invalid_Cross_Refeference_Key  id is but I'm guessing it has to do with the task that I am creating. The code for both the class and trigger are below. Any assistance is greatly a ppreciated. 
 
Insert Training Trigger: 


trigger insertTraining on Task (after update) {
               
    for (Task task : Trigger.new) {
       
        if(task.Training_Id__c == 1 && task.Status == 'Completed' ){
            
            Training__c train = new Training__c();
 
 
 train.Name = task.Subject;
 train.Type__c = task.Activity_Type__c;
 train.Missed__c= task.Missed__c;
 train.Onsite__c= task.Onsite__c;
 train.Comments__c = task.Description;
 train.Related_To__c = Trigger.new[0].WhatId;
 train.Due_Date__c = task.ActivityDate;
 train.Start_Time__c = task.Start_Time__c;
 train.End_Time__c = task.End_Time__c;
 train.Concerns__c = task.Concerns__c;
 train.Next_Steps__c = task.Next_Steps__c;

 insert train;
             
        }
    }
}



insertTraining Test Class: 

@istest

public class insertTraining {
    static testMethod void insertTraining (){
    
    string parentId = '001p000000ObV3q';
      
    //Insert a new activity where Training ID is 1
    
    Task task = new Task();
    
    task.Subject= 'Task is 1';
        
    task.No_Connect__c =  True;
    task.Connect__c = False;
    task.Notes_and_Comments__c = 'This should be the record that is taken to create a training' ;
    task.ActivityDate = date.newInstance(2017, 10, 15);
    task.Start_Time__c = dateTime.newInstance(2017, 10, 15, 12, 30, 0); 
    task.End_Time__c = dateTime.newInstance(2017, 10, 15, 12, 45, 0) ;
    task.Activity_Type__c = 'Basic Training';
    task.WhatId = parentId;
    task.Status = 'Completed';
    
   
    
    insert task;
  
 
      
      
       // insert new Training record
    
    Training__c train = new Training__c();
    
    train.Name = task.Subject;
    train.Type__c  = task.Activity_Type__c;
    train.missed__c = task.missed__c;  
    train.onsite__c = task.onsite__c;
    train.Comments__c = task.Notes_and_Comments__c;
    train.Related_To__c = task.WhatId;
    train.Due_Date__c = task.ActivityDate;
    train.Start_Time__c = task.Start_Time__c;
    train.End_Time__c = task.End_Time__c;
    
    
    insert train;
        
     //find the number of all of the trainings that that have been created
     
    List<Training__c> train1 =[Select Id,Name, onsite__c, missed__c, Related_To__c, Start_Time__c, End_Time__c, Due_Date__c, Comments__c, Type__c FROM Training__c WHERE Id = :task.Whatid];
     
                    
     System.assertEquals(0,train1.size());
     
     
     
     
     
     
         
         
         
         }
     
     
  }
I'm still relatively knew to triggers and classes so any assistance or clarification is greatly appreciated. 
Best Answer chosen by Kareem Miller 6
Paul S.Paul S.
Kareem - you're not getting any code coverage because that DML error is preventing the insert of the task (which is the condition under which the trigger fires).  You've hard-coded an account Id.  Is that one from production or your sandbox?

All Answers

Kareem Miller 6Kareem Miller 6

Separating the code to align with the error message. 

insertTraining Trigger:

trigger insertTraining on Task (after update) {
               
    for (Task task : Trigger.new) {
       
        if(task.Training_Id__c == 1 && task.Status == 'Completed' ){
            
            Training__c train = new Training__c();
 
 
 train.Name = task.Subject;
 train.Type__c = task.Activity_Type__c;
 train.Missed__c= task.Missed__c;
 train.Onsite__c= task.Onsite__c;
 train.Comments__c = task.Description;
 train.Related_To__c = Trigger.new[0].WhatId;
 train.Due_Date__c = task.ActivityDate;
 train.Start_Time__c = task.Start_Time__c;
 train.End_Time__c = task.End_Time__c;
 train.Concerns__c = task.Concerns__c;
 train.Next_Steps__c = task.Next_Steps__c;

 insert train;
             
        }
    }
}

insertTraining Test Class:

 

@istest

public class insertTraining {
    static testMethod void insertTraining (){
    
    string parentId = '001p000000ObV3q';
      
    //Insert a new activity where Training ID is 1
    
    Task task = new Task();
    
    task.Subject= 'Task is 1';
        
    task.No_Connect__c =  True;
    task.Connect__c = False;
    task.Notes_and_Comments__c = 'This should be the record that is taken to create a training' ;
    task.ActivityDate = date.newInstance(2017, 10, 15);
    task.Start_Time__c = dateTime.newInstance(2017, 10, 15, 12, 30, 0); 
    task.End_Time__c = dateTime.newInstance(2017, 10, 15, 12, 45, 0) ;
    task.Activity_Type__c = 'Basic Training';
    task.WhatId = parentId;
    task.Status = 'Completed';
    
    
   
    
    insert task;
  
 
      
      
       // insert new Training record
    
    Training__c train = new Training__c();
    
    train.Name = task.Subject;
    train.Type__c  = task.Activity_Type__c;
    train.missed__c = task.missed__c;  
    train.onsite__c = task.onsite__c;
    train.Comments__c = task.Notes_and_Comments__c;
    train.Related_To__c = task.WhatId;
    train.Due_Date__c = task.ActivityDate;
    train.Start_Time__c = task.Start_Time__c;
    train.End_Time__c = task.End_Time__c;
    
    
    insert train;
        
     //find the number of all of the trainings that that have been created
     
    List<Training__c> train1 =[Select Id,Name, onsite__c, missed__c, Related_To__c, Start_Time__c, End_Time__c, Due_Date__c, Comments__c, Type__c FROM Training__c WHERE Id = :task.Whatid];
     
                    
     System.assertEquals(0,train1.size());
     
     
     
     
     
     
         
         
         
         }
     
     
  }
Paul S.Paul S.
Kareem - you're not getting any code coverage because that DML error is preventing the insert of the task (which is the condition under which the trigger fires).  You've hard-coded an account Id.  Is that one from production or your sandbox?
This was selected as the best answer
Kareem Miller 6Kareem Miller 6
This indeed turned out to be the issue. I instead re-created the test class and inserted an account, whose Id I then used to reference when inserting the training record. This was able to give me full code coverage and I am now just bulkifying the code to ensure I get out as many test cases as possible. Also, in case anyone else references this; the issue I was having with obtaining code coverage was the result of having to run a full test on my testing environment of all of the classes currently in my test org. Once I ran a full test on all test classes I was able to get code coverage associated with the trigger that was created. Thanks for your feedback on this Paul. Best Regards.