+ Start a Discussion
Luke Higgins 22Luke Higgins 22 

Help writing a test class for a trigger

Hello I need help writing a tet class for a trigger. 

Trigger-
 

trigger addPTMplc on jstcl__PlacementTeamMember__c (after insert) {

    List<ID> listIds = new List<ID>();
    
    for(jstcl__PlacementTeamMember__c ptm : Trigger.new){
        listIds.add(ptm.jstcl__Placement__c);
    }
    List<String> lstNames = new List<String>();
    List<ts2__Placement__c> plcList = [SELECT Id, ts2__Filled_By__c FROM ts2__Placement__c WHERE Id IN :listIds];
    for(jstcl__PlacementTeamMember__c ptm : [SELECT jstcl__User__r.name , jstcl__SplitPercent__c From jstcl__PlacementTeamMember__c WHERE jstcl__Placement__c IN :listIds AND jstcl__CommissionPlan__r.name LIKE '%Recruiter%' ORDER BY jstcl__SplitPercent__c DESC NULLS LAST LIMIT 1]){
        
        if(lstNames.contains(ptm.jstcl__User__r.Name)){

        }
        else{
            lstNames.add(ptm.jstcl__User__c);
        }
    for(integer i=0;i<plcList.size();i++){
        String names = string.join(lstNames, ', ');
        plcList[i].ts2__Filled_By__c =names;
        }
    }
    update plcList;
    
    
    List<ID> listIds2 = new List<ID>();
    
    for(jstcl__PlacementTeamMember__c ptm2 : Trigger.new){
        listIds2.add(ptm2.jstcl__Placement__c);
    }
    List<String> lstNames2 = new List<String>();
    List<ts2__Placement__c> plcList2 = [SELECT Id, ts2__Sales_Rep__c FROM ts2__Placement__c WHERE Id IN :listIds2];
    for(jstcl__PlacementTeamMember__c ptm2 : [SELECT jstcl__User__r.name From jstcl__PlacementTeamMember__c WHERE jstcl__Placement__c IN :listIds2 AND jstcl__CommissionPlan__r.name LIKE '%Account Executive%' ORDER BY jstcl__SplitPercent__c DESC NULLS LAST LIMIT 1]){
        
        if(lstNames2.contains(ptm2.jstcl__User__r.Name)){

        }
        else{
            lstNames2.add(ptm2.jstcl__User__c);
        }
    for(integer i=0;i<plcList2.size();i++){
        String names2 = string.join(lstNames2, ', ');
        plcList2[i].ts2__Sales_Rep__c =names2;
        }
    }
    update plcList2;
}

What I have so far for the test class-
@isTest(SeeAllData=true)
public class addPTMplcTest {
    private static testMethod void testPTM(){
 ADP_Department__c adp = new ADP_Department__c();
        adp.Name = 'Georgia';
        insert adp;
         Contact con=new Contact(
            FirstName='fname',
            LastName = 'lname',
            Email = 'email@gmail.com',
            Phone = '9743800309'); 
        insert con; 
       
        ts2__Job__c job = new ts2__Job__c();
        job.Name = 'Software Engineer';
        insert job;
        Account acc = new Account(Name = 'Test Company');
        insert acc;
        ts2__HolidayCalendar__c hcal = new ts2__HolidayCalendar__c();
        hcal.Name= 'Holiday Calendar';
        insert hcal;
        Labor_Category__c lcat = new Labor_Category__c(Name='Software Engineer 1');  
        insert lcat;
         ts2__Placement__c plc = new ts2__Placement__c();
        plc.ts2__Job__c = job.Id;
        plc.Contractor_Type__c = 'Test';
        plc.Onboarding_Method__c= 'Staff Augmentation';
        plc.ts2__Start_Date__c= Date.newInstance(2019, 1, 7);
        plc.ts2__End_Date__c= Date.newInstance(2019, 2, 15);
        plc.jstcl__HireType__c= 'W2';
        plc.ADP_Department__c= adp.Id;
        plc.ts2__Bill_Rate__c= 60.00;
        plc.ts2__Pay_Rate__c= 30.00;
        plc.jstcl__Invoice_Batch__c= 'Placement';
        plc.ts2__Client__c= acc.Id;
        plc.ts2__Hiring_Manager__c= con.Id;
        plc.jstcl__TimecardApprover__c= con.Id;
        plc.jstcl__HolidayCalendar__c= hcal.Id;
		plc.State_Worked_In_Non_TFI_Placements__c= 'GA - Georgia';
        plc.jstcl__Overtime_Bill_Rate__c= 60;
        plc.jstcl__Burden__c= 0.20;
        plc.jstcl__Accounts_Receivable_U__c= '00537000005d7F2AAI';
        plc.jstcl__Terms__c= 'Net 60';
        plc.ts2__Employee__c= con.Id;
        plc.Labor_Category__c= lcat.Id;
        plc.Placement_Team_Member_List__c= 'Test User';
        insert plc;
        User u = new User(
 	    ProfileId = [SELECT Id FROM Profile WHERE Name = 'National Sales'].Id,
	     LastName = 'last',
    	 Email = 'abc@123.com',
	     Username = 'abc@123.com',
    	 CompanyName = 'TEST',
   	 	 Title = 'title',
   	 	 Alias = 'alias',
   		 TimeZoneSidKey = 'America/Los_Angeles',
     	 EmailEncodingKey = 'UTF-8',
    	 LanguageLocaleKey = 'en_US',
    	 LocaleSidKey = 'en_US'
		);
         
        jstcl__TG_Commission_Plan__c cp = new jstcl__TG_Commission_Plan__c(name = 'National Vertical Manager (NVM or NAM)' );
        insert cp;
        
        
        Test.startTest();
        System.debug('>>>>>>>>> Starting Test');
        jstcl__PlacementTeamMember__c ptm = new jstcl__PlacementTeamMember__c(jstcl__Placement__c = plc.Id,
                                                                              jstcl__User__c = u.Id,
                                                                              jstcl__CommissionPlan__c = cp.Id,
                                                                              jstcl__SplitPercent__c = 100);
        insert ptm;
        System.assertEquals(ptm.jstcl__User__c, plc.Placement_Team_Member_List__c);
        System.debug('>>>>>>>>> Ending Test');
        Test.stopTest();

    }
}

​​​​​​​
DimondDimond
What problem are you running into?
Luke Higgins 22Luke Higgins 22
I made a few minor changes but I am stuck at 58% coverage. It's saying that lines 12-20 and 35-43 are not covered.

Trigger:
trigger addPTMplc on jstcl__PlacementTeamMember__c (after insert) {

    List<ID> listIds = new List<ID>();
    
    for(jstcl__PlacementTeamMember__c ptm : Trigger.new){
        listIds.add(ptm.jstcl__Placement__c);
    }
    List<String> lstNames = new List<String>();
    List<ts2__Placement__c> plcList = [SELECT Id, ts2__Filled_By__c FROM ts2__Placement__c WHERE Id IN :listIds];
    for(jstcl__PlacementTeamMember__c ptm : [SELECT jstcl__User__r.name , jstcl__SplitPercent__c From jstcl__PlacementTeamMember__c WHERE jstcl__Placement__c IN :listIds AND jstcl__CommissionPlan__r.name LIKE '%Recruiter%' ORDER BY jstcl__SplitPercent__c DESC NULLS LAST LIMIT 1]){
        
        if(lstNames.contains(ptm.jstcl__User__r.Name)){

        }
        else{
            lstNames.add(ptm.jstcl__User__c);
        }
    for(integer i=0;i<plcList.size();i++){
        String names = string.join(lstNames, ', ');
        plcList[i].ts2__Filled_By__c =names;
        }
    }
    update plcList;
    
    
    List<ID> listIds2 = new List<ID>();
    
    for(jstcl__PlacementTeamMember__c ptm2 : Trigger.new){
        listIds2.add(ptm2.jstcl__Placement__c);
    }
    List<String> lstNames2 = new List<String>();
    List<ts2__Placement__c> plcList2 = [SELECT Id, ts2__Sales_Rep__c FROM ts2__Placement__c WHERE Id IN :listIds2];
    for(jstcl__PlacementTeamMember__c ptm2 : [SELECT jstcl__User__r.name From jstcl__PlacementTeamMember__c WHERE jstcl__Placement__c IN :listIds2 AND jstcl__CommissionPlan__r.name LIKE '%Account Executive%' ORDER BY jstcl__SplitPercent__c DESC NULLS LAST LIMIT 1]){
        
        if(lstNames2.contains(ptm2.jstcl__User__r.Name)){

        }
        else{
            lstNames2.add(ptm2.jstcl__User__c);
        }
    for(integer i=0;i<plcList2.size();i++){
        String names2 = string.join(lstNames2, ', ');
        plcList2[i].ts2__Sales_Rep__c =names2;
        }
    }
    update plcList2;
}

Test class:
@isTest(SeeAllData=true)
public class addPTMplcTest {
    private static testMethod void testPTM(){
 ADP_Department__c adp = new ADP_Department__c();
        adp.Name = 'Georgia';
        insert adp;
         Contact con=new Contact(
            FirstName='fname',
            LastName = 'lname',
            Email = 'email@gmail.com',
            Phone = '9743800309'); 
        insert con; 
       
        ts2__Job__c job = new ts2__Job__c();
        job.Name = 'Software Engineer';
        insert job;
        Account acc = new Account(Name = 'Time Warner Inc.');
        insert acc;
        ts2__HolidayCalendar__c hcal = new ts2__HolidayCalendar__c();
        hcal.Name= 'Holiday Calendar';
        insert hcal;
        Labor_Category__c lcat = new Labor_Category__c(Name='Software Engineer 1');  
        insert lcat;
         ts2__Placement__c plc = new ts2__Placement__c();
        plc.ts2__Job__c = job.Id;
        plc.Contractor_Type__c = 'Technical Youth';
        plc.Onboarding_Method__c= 'Staff Augmentation';
        plc.ts2__Start_Date__c= Date.newInstance(2019, 1, 7);
        plc.ts2__End_Date__c= Date.newInstance(2019, 2, 15);
        plc.jstcl__HireType__c= 'W2';
        plc.ADP_Department__c= adp.Id;
        plc.ts2__Bill_Rate__c= 60.00;
        plc.ts2__Pay_Rate__c= 30.00;
        plc.jstcl__Invoice_Batch__c= 'Placement';
        plc.ts2__Client__c= acc.Id;
        plc.ts2__Hiring_Manager__c= con.Id;
        plc.jstcl__TimecardApprover__c= con.Id;
        plc.jstcl__HolidayCalendar__c= hcal.Id;
		plc.State_Worked_In_Non_TFI_Placements__c= 'GA - Georgia';
        plc.jstcl__Overtime_Bill_Rate__c= 60;
        plc.jstcl__Burden__c= 0.20;
        plc.jstcl__Accounts_Receivable_U__c= '00537000005d7F2AAI';
        plc.jstcl__Terms__c= 'Net 60';
        plc.ts2__Employee__c= con.Id;
        plc.Labor_Category__c= lcat.Id;
        insert plc;
        User u = new User(
 	    ProfileId = [SELECT Id FROM Profile WHERE Name = 'JS Recruiter - Brooksource'].Id,
	     LastName = 'last',
    	 Email = 'anoetzelman@brooksource.com.invalid',
	     Username = 'anoetzelman@brooksource.com.devfull',
    	 CompanyName = 'TEST',
   	 	 Title = 'title',
   	 	 Alias = 'alias',
   		 TimeZoneSidKey = 'America/Los_Angeles',
     	 EmailEncodingKey = 'UTF-8',
    	 LanguageLocaleKey = 'en_US',
    	 LocaleSidKey = 'en_US'
		);
               
        Test.startTest();
        System.debug('>>>>>>>>> Starting Test');
        jstcl__PlacementTeamMember__c ptm = new jstcl__PlacementTeamMember__c(jstcl__Placement__c = plc.Id,
                                                                              jstcl__User__c = u.Id,
                                                                              jstcl__SplitPercent__c = 100);
        insert ptm;
        System.assertEquals(ptm.jstcl__User__c, plc.Placement_Team_Member_List__c);
        System.debug('>>>>>>>>> Ending Test');
        Test.stopTest();

    }
}

Thanks!
DimondDimond
looks like you'll just have to create more test data and insert more jstcl_placementteammember__c that would satisfy your trigger. You might want to consider refactoring and use a helper class. so