You need to sign in to do that
Don't have an account?
Dilip 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.
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.
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
@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;
}}
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;
}}
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.
Hi Dilip,
Did you try the above code for test class?
How much coverage you got?
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;
}}
I got 100% code coverage. Thank you very much.
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');
}
}
}