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
Siddharth LakhotiaSiddharth Lakhotia 

Please help in writting a test class for this trigger? I am new to developement

trigger TaskTrigger on Task (before delete) 
{
    if(RecursionControl.TaskrunOnce())
    {
        TaskTriggerHandler.execute(Trigger.old, Trigger.OldMap, Trigger.IsDelete, Trigger.IsBefore);
        //The logic is contained in the execute() method of TaskTriggerHandler Class
    }


Task Trigger Handler Class

public class TaskTriggerHandler 
{
    
    public static void execute(List<Task> oldList, Map<Id, Task> mapOld, Boolean IsDelete, Boolean IsBefore)
    {
        
        /* Activate/Deactivate Trigger by Custom Setting "disableRules__c" */
        disableRules__c theCustomSetting = disableRules__c.getInstance(UserInfo.getUserId());
        
        if(!(String.IsNotBlank(theCustomSetting.Trigger_Objects__c) && theCustomSetting.Trigger_Objects__c.containsIgnoreCase('Task') && theCustomSetting.Disable_Triggers__c))
        {
            for(Task oldObj : oldList)
            {
                oldObj.addError(Label.ActivityDelError);
            }
        }
    }
}

}
Best Answer chosen by Siddharth Lakhotia
Dinesh MultaniDinesh Multani
Here is the code 
@isTest(SeeAllData = false)
public class TaskTriggerHandlertest
{
    static testmethod void TaskTriggerHandlertest()
    {
        Account testAccount=new Account();
       //Create data for all the required fields in account
      //Example (See Below)-
        testAccount.name='Test';
        insert testAccount;
        
        Task testTask=new Task();        


//Create data for all the required fields in Task


      //Example (See Below)-

        testTask.Priority='low';
        testTask.Status='Completed';
        insert testTask;
        
        disableRules__c testDisableRule=new disableRules__c(); 
      //Create data for all the required fields in Disbale Rules
      //Example (See Below)-
        testDisableRule.Disable_Triggers__c=false;
        testDisableRule.Trigger_Objects__c='Task';
        insert testDisableRule;
        
        delete testTask;
    }
}

 

All Answers

Dinesh MultaniDinesh Multani
Here is the code 
@isTest(SeeAllData = false)
public class TaskTriggerHandlertest
{
    static testmethod void TaskTriggerHandlertest()
    {
        Account testAccount=new Account();
       //Create data for all the required fields in account
      //Example (See Below)-
        testAccount.name='Test';
        insert testAccount;
        
        Task testTask=new Task();        


//Create data for all the required fields in Task


      //Example (See Below)-

        testTask.Priority='low';
        testTask.Status='Completed';
        insert testTask;
        
        disableRules__c testDisableRule=new disableRules__c(); 
      //Create data for all the required fields in Disbale Rules
      //Example (See Below)-
        testDisableRule.Disable_Triggers__c=false;
        testDisableRule.Trigger_Objects__c='Task';
        insert testDisableRule;
        
        delete testTask;
    }
}

 
This was selected as the best answer
Siddharth LakhotiaSiddharth Lakhotia
Hi Dinesh,

Thanks a lot for the code sharing.. Can you also tell how do I check code coverage for it
Siddharth LakhotiaSiddharth Lakhotia
Please help me with test class for

​trigger preventTaskDeletion on Task (before delete) {
    for(Task tsk: trigger.old){
        if(tsk.Status == 'Completed'){
            tsk.adderror('Closed Task can not be deleted');
        }
    }
}
Dinesh MultaniDinesh Multani
Is this code related to same thread? If not Please start new thread and Mark this thread as Solved and select Best answer which you liked.
Dinesh MultaniDinesh Multani
Hi Siddharth,
 
You need to run the test class first and then you can check the code coverage by simply going back to your helper class and trigger and at the top of those respective class and trigger you can see their respective code coverage or another method is once you have run the test class simply click on "Developer Console button" and at the bottom right-hand corner you will see the list of all the classes and triggers with their code coverage besides them.
 
Refer this link for more information- https://help.salesforce.com/articleView?id=000199478&type=1  (https://help.salesforce.com/articleView?id=000199478&type=1 )
Siddharth LakhotiaSiddharth Lakhotia
Hi Dinesh,

In Tasks..  Owner is a mandatory field.

But I am getting this error.  Field is not writeable: Task.Owner 

How do I get it corrected
Dinesh MultaniDinesh Multani
You need not to set the Owner field. This will be auto populated.