+ Start a Discussion
Dilip KulkarniDilip Kulkarni 

trigger(on task object) test class

Hi Experts,
What will be the test class for following trigger?

trigger UpdateSADeclined on Task (before insert,before update) {

for(Task x: Trigger.new)

If (x.Declined_by__c!=null || x.Date_Time_Declined__c!=null || x.Declined_Reason__c!=null)
 { 
  
  x.SA_Declined__c=true;

else
 { x.SA_Declined__c=false;
 
 }
 }
 }


Please help.
Best Answer chosen by Dilip Kulkarni
Deepak Maheshwari 7Deepak Maheshwari 7

Please try below test class and let me know the code coverage:

Please confirm Date_Time_Declined__c is date time field

Declined_by__c is lookup of user

Declined_Reason__c is text type field.


@isTest
private class testMyTaskTrigger {
  
    public static testMethod void testTTrigger1(){
        
        
        Task t = new Task();
        t.OwnerId = UserInfo.getUserId();
        t.Subject='Testing';
        t.Status='Not Started';
        t.Priority='Normal';
        t.Date_Time_Declined__c= System.Now();
        t.Declined_by__c= UserInfo.getUserId();
        t.Declined_Reason__c='Nothing';
        insert t;   
}        
         public static testMethod void testTTrigger2(){
        
        
        Task t1 = new Task();
        t1.OwnerId = UserInfo.getUserId();
        t1.Subject='t=Testing';
        t1.Status='Not Started';
        t1.Priority='Normal';
        

        insert t1; 
}}        

All Answers

Deepak Maheshwari 7Deepak Maheshwari 7
Please try below test class and let me know the code coverage:
@isTest
private class testMyTaskTrigger {
  
    public static testMethod void testTTrigger1(){
        
        
        Task t = new Task();
        t.OwnerId = UserInfo.getUserId();
        t.Subject='Testing';
        t.Status='Not Started';
        t.Priority='Normal';
        t.Date_Time_Declined__c= System.Now();

        insert t;   
}        
         public static testMethod void testTTrigger2(){
        
        
        Task t1 = new Task();
        t1.OwnerId = UserInfo.getUserId();
        t1.Subject='t=Testing';
        t1.Status='Not Started';
        t1.Priority='Normal';
        

        insert t1; 
}}        
Deepak Maheshwari 7Deepak Maheshwari 7

Please try below test class and let me know the code coverage:

Please confirm Date_Time_Declined__c is date time field

Declined_by__c is lookup of user

Declined_Reason__c is text type field.


@isTest
private class testMyTaskTrigger {
  
    public static testMethod void testTTrigger1(){
        
        
        Task t = new Task();
        t.OwnerId = UserInfo.getUserId();
        t.Subject='Testing';
        t.Status='Not Started';
        t.Priority='Normal';
        t.Date_Time_Declined__c= System.Now();
        t.Declined_by__c= UserInfo.getUserId();
        t.Declined_Reason__c='Nothing';
        insert t;   
}        
         public static testMethod void testTTrigger2(){
        
        
        Task t1 = new Task();
        t1.OwnerId = UserInfo.getUserId();
        t1.Subject='t=Testing';
        t1.Status='Not Started';
        t1.Priority='Normal';
        

        insert t1; 
}}        

This was selected as the best answer
Dilip KulkarniDilip Kulkarni
Hi Deepak,
Thank for the reply.
All three fields (  Date_Time_Declined__c,Declined_by__c and Declined_Reason__c)  are date/time fields.
I will check and revert.
Deepak Maheshwari 7Deepak Maheshwari 7

Hi Dilip,

 

Did you try the above code for test class?

How much coverage you got?

Dilip KulkarniDilip Kulkarni
Not yet. I will try and let you know asap.
Deepak Maheshwari 7Deepak Maheshwari 7

If all three field are date/time field then use below test class:

 

@isTest
private class testMyTaskTrigger {
  
    public static testMethod void testTTrigger1(){
        
        
        Task t = new Task();
        t.OwnerId = UserInfo.getUserId();
        t.Subject='Testing';
        t.Status='Not Started';
        t.Priority='Normal';
        t.Date_Time_Declined__c= System.Now();
        t.Declined_by__c= System.Now();
        t.Declined_Reason__c=System.Now();
        insert t;   
}        
         public static testMethod void testTTrigger2(){
        
        
        Task t1 = new Task();
        t1.OwnerId = UserInfo.getUserId();
        t1.Subject='t=Testing';
        t1.Status='Not Started';
        t1.Priority='Normal';
        

        insert t1; 
}}        

Dilip KulkarniDilip Kulkarni
OK.
Dilip KulkarniDilip Kulkarni
Hi Deepak,
I got 100% code coverage. Thank you very much.
Dilip KulkarniDilip Kulkarni
Yes.
Deepak Maheshwari 7Deepak Maheshwari 7
Thanks
sanjay pegasanjay pega
Hello everyone I am beginner in developing and trying to compleate one task on checkboxes. The task is about to get all the student reports(child object) when check the checkboxes of student name(parent object). Here is my code please correct it.
VFpage:

<apex:page controller="SelCheckBox_cntrl">
 <apex:form >
 <apex:pageBlock title="Select Student Name">
 <apex:SelectCheckboxes value="{!coolbool}">
 <apex:SelectOptions value="{!StudentNames}"/>
 <apex:actionSupport event="onclick" reRender="messagebox"/>
 </apex:SelectCheckboxes>
 </apex:pageBlock>
 <apex:pageBlock title="Related Selected Reports">
 <apex:pageBlockTable value="{!srep}" var="SR">
 <apex:column value="{!SR.Name}"/>
 </apex:pageBlockTable>
 </apex:pageBlock>
 <apex:commandButton value="Get Selected Reports" action="{!selctedReports}"/>
 </apex:form>
</apex:page>

APEX code:

public class SelCheckBox_cntrl {
public String StudentDetails{set;get;}
public List<Student_Reports__c> srep{set;get;}
public Boolean coolbool{set;get;}

public SelCheckBox_cntrl(){
srep = new List<Student_Reports__c>();
 coolbool = false;
}

public List<SelectOption> getStudentNames(){
List<SelectOption> Sel = new List<SelectOption>();
for(Student_Details__c SD : [Select id,Name from Student_Details__c]){
Sel.add(new SelectOption(SD.id,SD.Name));
}
return Sel;
}

public void selctedReports(){
  if(coolbool==true){
   srep = [Select id,Name,Student_Name__c,Test1__c,Test2__c,Test3__c,Certification__c,Result__c from Student_Reports__c where Student_Name__c=:StudentDetails];
    }
 else{
 system.debug('Select any Name');
  }
 }
}