You need to sign in to do that
Don't have an account?
Kareem 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.
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.
All Answers
Separating the code to align with the error message.
insertTraining Trigger:
insertTraining Test Class: