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
Luke Higgins 23Luke 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:
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 VakatiRaj Vakati
Remove Hardcoded Id and use this code
 
@isTest
public class addNAM_VAMtest {
	
    static testmethod void test1(){
       	
		   
        Profile pf= [Select Id from profile where Name='System Administrator']; 
        
        String orgId=UserInfo.getOrganizationId(); 
        String dateString=String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','') ;
        Integer RandomId=Integer.valueOf(Math.rint(Math.random()*1000000)); 
        String uniqueName=orgId+dateString+RandomId; 
        User uu=new User(firstname = 'ABC', 
                         lastName = 'XYZ', 
                         email = uniqueName + '@test' + orgId + '.org', 
                         Username = uniqueName + '@test' + orgId + '.org', 
                         EmailEncodingKey = 'ISO-8859-1', 
                         Alias = uniqueName.substring(18, 23), 
                         TimeZoneSidKey = 'America/Los_Angeles', 
                         LocaleSidKey = 'en_US', 
                         LanguageLocaleKey = 'en_US', 
                         ProfileId = pf.Id
                        ); 
        
        
        insert uu;
        
		
		
		account acct = new account(VAM_or_NAM_Member__c = uu.id, Name = 'Northwestern Medicine'); 
		
	   ts2__Placement__c place = new ts2__Placement__c() ;
	   place.Name ='Test';
	   place.ts2__Client__c = acct.Id ; 
	   insert place ; 
	   
	   jstcl__CommissionPlan__c comm= new jstcl__CommissionPlan__c() ; 
	   comm.Name = 'Test';
	   insert comm ; 
	   
	    jstcl__PlacementTeamMember__c ptm = new jstcl__PlacementTeamMember__c(jstcl__Placement__c = place.Id,
		jstcl__User__c = uu.Id,
            jstcl__CommissionPlan__c = comm.id, jstcl__SplitPercent__c = 100);
		
		
    }
}

 
Luke Higgins 23Luke Higgins 23

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: []"

Raj VakatiRaj Vakati
Use this code
 
@isTest
public class addNAM_VAMtest {
	
    static testmethod void test1(){
       	
		   
        Profile pf= [Select Id from profile where Name='System Administrator']; 
        
        String orgId=UserInfo.getOrganizationId(); 
        String dateString=String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','') ;
        Integer RandomId=Integer.valueOf(Math.rint(Math.random()*1000000)); 
        String uniqueName=orgId+dateString+RandomId; 
        User uu=new User(firstname = 'ABC', 
                         lastName = 'XYZ', 
                         email = uniqueName + '@test' + orgId + '.org', 
                         Username = uniqueName + '@test' + orgId + '.org', 
                         EmailEncodingKey = 'ISO-8859-1', 
                         Alias = uniqueName.substring(18, 23), 
                         TimeZoneSidKey = 'America/Los_Angeles', 
                         LocaleSidKey = 'en_US', 
                         LanguageLocaleKey = 'en_US', 
                         ProfileId = pf.Id
                        ); 
        
        
        insert uu;
        
		
		
		account acct = new account(VAM_or_NAM_Member__c = uu.id, Name = 'Northwestern Medicine'); 
		insert acct ;
		
	   ts2__Placement__c place = new ts2__Placement__c() ;
	   place.Name ='Test';
	   place.ts2__Client__c = acct.Id ; 
	   insert place ; 
	   
	   jstcl__CommissionPlan__c comm= new jstcl__CommissionPlan__c() ; 
	   comm.Name = 'Test';
	   insert comm ; 
	   
	    jstcl__PlacementTeamMember__c ptm = new jstcl__PlacementTeamMember__c(jstcl__Placement__c = place.Id,
		jstcl__User__c = uu.Id,
            jstcl__CommissionPlan__c = comm.id, jstcl__SplitPercent__c = 100);
			insert ptm ;
		
		
    }
}

 
Raj VakatiRaj Vakati
Is its working ?
Luke Higgins 23Luke Higgins 23
It unfortunately is not. It still returns this error- 
"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: []"
Leo BarnoskiLeo Barnoski
Is it working for buy Valium online (https://pixelsmedstore.com/product-category/buy-valium/) website?