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
BrendBrend 

How do I go about writting test class for this

This code works for my requirement but I need test class please.


trigger NoDeleteonTask on Task (before delete)
{
   String ProfileId = UserInfo.getProfileId(); 
   List<Profile> profiles=[select id from Profile where name='Profile 1' or name='Profile 2'];
 
   if (2!=profiles.size())
   {
      // unable to get the profiles - handle error
   }
   else
   {
       for (Task a : Trigger.old)     
       {           
          if ( (profileId==profiles[0].id) || (profileId==profiles[1].id) )
          {
             a.addError('You can't delete this record');
          }
       }           
   }
}
Best Answer chosen by Brend
Steven NsubugaSteven Nsubuga
I added an escape character for the ' in the trigger error message.
trigger NoDeleteonTask on Task (before delete)
{
   String ProfileId = UserInfo.getProfileId(); 
   List<Profile> profiles=[select id from Profile where name='Profile 1' or name='Profile 2'];
 
   if (2!=profiles.size())
   {
      // unable to get the profiles - handle error
   }
   else
   {
       for (Task a : Trigger.old)     
       {           
          if ( (profileId==profiles[0].id) || (profileId==profiles[1].id) )
          {
             a.addError('You can\'t delete this record');
          }
       }           
   }
}

Test
@isTest
private class NoDeleteonTaskTest {
    
    @isTest static void test() {

        Profile pf= [Select Id from profile where Name='Profile 1']; 
        String orgId=UserInfo.getOrganizationId(); 
        User uu=new User(firstname = 'ABC', 
                         lastName = 'XYZ', 
                         email =  'uniqueName@test' + orgId + '.org', 
                         Username = 'uniqueName@test' + orgId + '.org', 
                         EmailEncodingKey = 'ISO-8859-1', 
                         Alias = 'ABC', 
                         TimeZoneSidKey = 'America/Los_Angeles', 
                         LocaleSidKey = 'en_US', 
                         LanguageLocaleKey = 'en_US', 
                         ProfileId = pf.Id
                        ); 
        
        insert uu;
        
        system.runas(uu){
            Account acct = new Account(Name = 'Test Account');
            insert acct;
            Contact ct = new Contact(AccountId = acct.Id, LastName = 'TestCT');
            insert ct;
            
            Task newTask = new Task(Description = 'Site Survey Email',
                                    Priority = 'Normal',
                                    Status = 'Completed',
                                    Subject = 'Site Survey',
                                    Type = 'Email',
                                    WhoId = ct.Id);
            
            
            insert newTask;
            
            try {
                delete newTask;
            } catch (Exception e) {
                System.assert(e.getMessage().contains('You can\'t delete this record'));
            }
            
        }
    }
}

 

All Answers

Santosh Bompally 8Santosh Bompally 8
Hi Uzee, 

Use this, Some tweaks to this should cover your code. 
https://salesforce.stackexchange.com/questions/135454/how-to-write-a-test-class-for-task

PS: Make sure you're wrapping test.startTest(); and test.stopTest(); around Delete Task; 
Steven NsubugaSteven Nsubuga
I added an escape character for the ' in the trigger error message.
trigger NoDeleteonTask on Task (before delete)
{
   String ProfileId = UserInfo.getProfileId(); 
   List<Profile> profiles=[select id from Profile where name='Profile 1' or name='Profile 2'];
 
   if (2!=profiles.size())
   {
      // unable to get the profiles - handle error
   }
   else
   {
       for (Task a : Trigger.old)     
       {           
          if ( (profileId==profiles[0].id) || (profileId==profiles[1].id) )
          {
             a.addError('You can\'t delete this record');
          }
       }           
   }
}

Test
@isTest
private class NoDeleteonTaskTest {
    
    @isTest static void test() {

        Profile pf= [Select Id from profile where Name='Profile 1']; 
        String orgId=UserInfo.getOrganizationId(); 
        User uu=new User(firstname = 'ABC', 
                         lastName = 'XYZ', 
                         email =  'uniqueName@test' + orgId + '.org', 
                         Username = 'uniqueName@test' + orgId + '.org', 
                         EmailEncodingKey = 'ISO-8859-1', 
                         Alias = 'ABC', 
                         TimeZoneSidKey = 'America/Los_Angeles', 
                         LocaleSidKey = 'en_US', 
                         LanguageLocaleKey = 'en_US', 
                         ProfileId = pf.Id
                        ); 
        
        insert uu;
        
        system.runas(uu){
            Account acct = new Account(Name = 'Test Account');
            insert acct;
            Contact ct = new Contact(AccountId = acct.Id, LastName = 'TestCT');
            insert ct;
            
            Task newTask = new Task(Description = 'Site Survey Email',
                                    Priority = 'Normal',
                                    Status = 'Completed',
                                    Subject = 'Site Survey',
                                    Type = 'Email',
                                    WhoId = ct.Id);
            
            
            insert newTask;
            
            try {
                delete newTask;
            } catch (Exception e) {
                System.assert(e.getMessage().contains('You can\'t delete this record'));
            }
            
        }
    }
}

 
This was selected as the best answer
BrendBrend
@Santosh, @Steven, I appreciate your responses. They were both helpful.
 However,  on line 41: System.assert(e.getMessage().contains('You can\'t delete this record'));
I got the following error while trying to deploy to production
System.AssertException: Assertion Failed
Stack Trace: Class.TaskdeletionTest.testinsertuser: line 41, column 1
BrendBrend
Thank you so much Steven!!! I got everything working and deployed sucessfully.