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
Admin 123Admin 123 

trigger UpdateSA on Task (before insert,before update) { for(Task t: Trigger.new) { If (t.Date_Time_Denied__c == null || t.Declined_By__c == null) { t.SA_Denied__c = true; } else { t.SA_Denied__c = false; }}

trigger UpdateSA on Task (before insert,before update)
 { for(Task t: Trigger.new)
    {  If (t.Date_Time_Denied__c == null ||    t.Declined_By__c == null)
        {
           t.SA_Denied__c = true;
        } else
            {            
               t.SA_Denied__c = false;  
      }  
    }
    Test class for this?
Best Answer chosen by Admin 123
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_Denied__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; 
}}        

 

All Answers

Shivdeep KumarShivdeep Kumar
Hi Shruti,
It's very simple, Create a method and insert a task record in which your "Date_Time_Denied__c" field will be null and and in second method your "Date_Time_Denied__c" will not be null.
Create one another method which ll check the null pointer exception case for Salesforce best practice of test classes.
Please check the below code..
@isTest
public class UpdateSA_Test{
static testMethod void data1(){
     	task t = new task();
// your all mandatory fields of task and no need to insert Date_Time_Denied__c field
        t.OwnerId = UserInfo.getUserId();
        t.Subject = 'Call';
        t.Priority = 'Normal';
        t.Status = 'Completed';
        insert t;
    }

static testMethod void data2(){
     	task t = new task();
// your all mandatory fields of task and insert Date_Time_Denied__c field
        t.OwnerId = UserInfo.getUserId();
        t.Subject = 'Call';
        t.Priority = 'Normal';
        t.Status = 'Completed';
t.Date_Time_Denied__c = ' ' ;
        insert t;
    }
}

Please let me know if this help !

Thanks
Shivdeep 
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_Denied__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; 
}}        

 

This was selected as the best answer
Admin 123Admin 123
This works, Thankyou so much.!