+ Start a Discussion
Irish@accIrish@acc 

Test Class for Trigger

H,

how do i write test class for the below trigger..I have nevr written test class..plz help

 

trigger CloneTemplateTasks on Project__c(after Insert) {

List<Project__c> lstPrj = new List<Project__c>();//create an empty list of Project Set<String> setType = new Set<String>();//Set for Storing Project type Set<Id> setPrjId = new Set<Id>(); // Set for Storing Projectids List<Task__c> insertlst = new list<Task__c>(); // List of Task to insert

 

for(Project__c proj : Trigger.new){

setType.add(proj.Project_Type__c);

}

System.debug('setType----->'+setType);

List<Project_Template__c> lstTemplate = [SELECT Template__c FROM Project_Template__c where Template__c !=null and Project_Type__c in:setType];

System.debug('lstTemplate----->'+lstTemplate);

for(Project_Template__c tempObj : lstTemplate){

setPrjId.add(tempObj.Template__c);

}

System.debug('setPrjId----->'+setPrjId);

List<Task__c> lsttask = [select id,Name,Project__c,Actual_completion_date__c,TQL_Phase__c,Key_TQL_Milestone__c,Comments__c,Owner__c,Target_date__c from Task__c where Project__c in :setPrjId ];

System.debug('lsttask ----->'+lsttask);

for(Project__c proj : Trigger.new) {

for(Task__c t : lsttask) { Task__c tsk = new Task__c(); tsk = t.clone(); tsk.Project__c = proj.id; insertlst.add(tsk); }

}

System.debug('insertlst----->'+insertlst);

insert insertlst;

 

 

 

}

Best Answer chosen by Admin (Salesforce Developers) 
Irish@accIrish@acc
How do i insert the child class...I tried the same logic as Project__c but getting error...

for(Task__c t : lsttask) { Task__c tsk = new Task__c(); tsk = t.clone(); tsk.Project__c = proj.id; insertlst.add(tsk); }//need to cover this in test class

All Answers

Vinit_KumarVinit_Kumar

Hi Irish,

 

Please try below :-

 

@IsTest(SeeAllData=true)

public class CloneTemplateTasks_test{

 

    static testmethod void MyUnitTest(){

 

     Project__c pr = new Project__c(Name='Test',-------------) ;//put the values for all the required fields to insert a record  

     insert pr;

 

 

 

}

 

 

 

}

Irish@accIrish@acc
How do i insert the child class...I tried the same logic as Project__c but getting error...

for(Task__c t : lsttask) { Task__c tsk = new Task__c(); tsk = t.clone(); tsk.Project__c = proj.id; insertlst.add(tsk); }//need to cover this in test class
This was selected as the best answer
Vinit_KumarVinit_Kumar

Irish,

 

You need to check if you have the populated the list lsttask or not.If you have som value in lsttask i.e if lsttask.size()>0 ,then you can cover these lines.

Irish@accIrish@acc
these have been populated in the lsttask