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
SFDC12SFDC12 

scenario for trigger

Hi evryone tried  below code and testclass able to cover 80% how to cover error message.
Trigger:
//write a trigger only system administrator should delete the tasks(if system administartor means delete the recrd if not show error message)
trigger userdeletetasks on Task (before delete) {
id profileid=userinfo.getProfileId();
    profile profilname=[select Name from Profile where id=:profileid];
    for(task t:trigger.old){
        if(profilname.Name!='System Administrator'){
            t.adderror('no access for user');
        }
    }
  
}

Testclass:
@isTest
public class Testuserdeletetasks {
@isTest
    static void call(){
        
      task t=new task();
        t.Subject='subtask';
        insert t;
         Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator'];
       
            try{
                delete t;
            }catch(Exception ex){
                system.debug('Exception');
               
                system.assert(ex.getMessage().contains('no access for user'),'no access for user');
            }    
        
        
    }
}

Thanks in Advance.
Best Answer chosen by SFDC12
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Anshi,

Change the test class as below to get 100%.
 
@isTest
public class Testuserdeletetasks {
@isTest
    static void call(){
        User u = new User(
     ProfileId = [SELECT Id FROM Profile WHERE Name = 'Standard User'].Id,
     LastName = 'last',
     Email = 'puser000@amamama.com',
     Username = 'puser000@amamama.com' + System.currentTimeMillis(),
     CompanyName = 'TEST',
     Title = 'title',
     Alias = 'alias',
     TimeZoneSidKey = 'America/Los_Angeles',
     EmailEncodingKey = 'UTF-8',
     LanguageLocaleKey = 'en_US',
     LocaleSidKey = 'en_US'
);
        system.runAs(u){
      task t=new task();
        t.Subject='subtask';
        insert t;
        
       
            try{
                delete t;
            }catch(Exception ex){
                system.debug('Exception');
               
                system.assert(ex.getMessage().contains('no access for user'),'no access for user');
            }    
        
        }
    }
}

If this solution helps, Please mark it as best answer.

Thanks

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Anshi,

Change the test class as below to get 100%.
 
@isTest
public class Testuserdeletetasks {
@isTest
    static void call(){
        User u = new User(
     ProfileId = [SELECT Id FROM Profile WHERE Name = 'Standard User'].Id,
     LastName = 'last',
     Email = 'puser000@amamama.com',
     Username = 'puser000@amamama.com' + System.currentTimeMillis(),
     CompanyName = 'TEST',
     Title = 'title',
     Alias = 'alias',
     TimeZoneSidKey = 'America/Los_Angeles',
     EmailEncodingKey = 'UTF-8',
     LanguageLocaleKey = 'en_US',
     LocaleSidKey = 'en_US'
);
        system.runAs(u){
      task t=new task();
        t.Subject='subtask';
        insert t;
        
       
            try{
                delete t;
            }catch(Exception ex){
                system.debug('Exception');
               
                system.assert(ex.getMessage().contains('no access for user'),'no access for user');
            }    
        
        }
    }
}

If this solution helps, Please mark it as best answer.

Thanks
This was selected as the best answer
SFDC12SFDC12
Thanks for your reply can you please expain about system.assert
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Anshi,

System.assert () means the it will check if the expected is true or false. Here the case is if the user is not system admin he should be getting an error. So we have run the delete with other than system admin and we got that erroe and that is equal to what we have in original class. So this test class got passes.

Hope this answers your question.

Thanks,