You need to sign in to do that
Don't have an account?
Luke Higgins 23
Writing a test class for a trigger
Hi,
I am trying to get a test class written for this trigger that adds a child record after the creation of the parent record. I am getting the following error -
"System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, jstcl.PlacementTeamMember: execution of BeforeInsert
caused by: System.NullPointerException: Attempt to de-reference a null object
Class.jstcl.PlacementTeamMemberTriggerHandler.validateTeamMembers: line 14, column 1
Trigger.jstcl.PlacementTeamMember: line 2, column 1: []"
trigger:
test:
I am trying to get a test class written for this trigger that adds a child record after the creation of the parent record. I am getting the following error -
"System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, jstcl.PlacementTeamMember: execution of BeforeInsert
caused by: System.NullPointerException: Attempt to de-reference a null object
Class.jstcl.PlacementTeamMemberTriggerHandler.validateTeamMembers: line 14, column 1
Trigger.jstcl.PlacementTeamMember: line 2, column 1: []"
trigger:
trigger autoAddNAM_VAM on ts2__Placement__c (after insert) { List<jstcl__PlacementTeamMember__c> teamMems = new List<jstcl__PlacementTeamMember__c>(); Set<Id> tsClientSet = new Set<id>(); Map<ID, Account> tsClientmap = new Map<ID, Account> (); // Check the API Name of ts2__Client__c object for(ts2__Placement__c newTeamMem : Trigger.New) { if(newTeamMem.ts2__Client__c != null) { tsClientSet.add(newTeamMem.ts2__Client__c); } } if(tsClientSet.size() > 0) { tsClientmap = new Map<ID, Account>([SELECT Id, VAM_or_NAM_Member__c from Account where Id IN: tsClientSet]); } for(ts2__Placement__c newTeamMem : Trigger.New){ if(tsClientmap.Containskey(newTeamMem.ts2__Client__C)) { try{ if (tsClientmap.get(newTeamMem.ts2__Client__C).VAM_or_NAM_Member__c != null ){ teamMems.add(new jstcl__PlacementTeamMember__c( jstcl__Placement__c = newTeamMem.Id, jstcl__User__c = tsClientmap.get(newTeamMem.ts2__Client__C).VAM_or_NAM_Member__c, jstcl__CommissionPlan__c = [Select jstcl__Commission_Plan__c FROM jstcl__TG_Commission_Plan_Assignment__c WHERE jstcl__Commission_Plan__r.Name LIKE '%Plan%' AND jstcl__User__c = :tsClientmap.get(newTeamMem.ts2__Client__C).VAM_or_NAM_Member__c].jstcl__Commission_Plan__c, jstcl__SplitPercent__c = 100)); } }catch(exception e){ System.debug('The following exception has occurred: ' + e.getMessage()); } } insert teamMems; } }
test:
@isTest public class addNAM_VAMtest { static testmethod void test1(){ ts2__Placement__c plc = new ts2__Placement__c(ts2__Client__c = '00137000008cDfrAAE'); account acct = new account(VAM_or_NAM_Member__c = '00537000003tZkqAAE', Name = 'Northwestern Medicine'); jstcl__PlacementTeamMember__c ptm = new jstcl__PlacementTeamMember__c(jstcl__Placement__c = 'a211G000001NrqRQAS', jstcl__User__c = '00537000003tZkqAAE', jstcl__CommissionPlan__c = 'a4g370000008PGJAA2', jstcl__SplitPercent__c = 100); insert acct; insert ptm; } }
Raj,
Thanks for your response. However on line 33, you try to write to the Name field of the ts2__Placement__c but it is an auto-number field. When I try to just remove the line from the code, I am left with this error on the test -
"System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, ts2.Placement: execution of BeforeInsert
caused by: System.NullPointerException: Attempt to de-reference a null object
Trigger.ts2.Placement: line 14, column 1: []"
"System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, ts2.Placement: execution of BeforeInsert
caused by: System.NullPointerException: Attempt to de-reference a null object
Trigger.ts2.Placement: line 17, column 1: []"