+ Start a Discussion
Roopa S 1Roopa S 1 

plz help me to write test class for this

trigger deleteUserTrigger1 on Task (before delete) {
    Id profileid=Userinfo.getProfileId();  
    profile profilname=[select Name from Profile where id=:profileid];
    for(Task accountDuplicate:Trigger.old){
        if(profilname.Name!='System Administrator'){
            accountDuplicate.addError('No Access for Deletion');                                    
        }    
    }
}*/
Best Answer chosen by Roopa S 1
AnkaiahAnkaiah (Salesforce Developers) 
Hi Roopa,

Please try with below code getting 100%
@isTest
public class deleteUserTrigger1_Test {

    @testSetup static void setupTestData() {

		Id stdProfId = [Select id from Profile WHERE Name=:'Standard User'].Id;
        User stdUser = new User();
        stdUser.FirstName = 'Admin';
        stdUser.LastName = 'testUser';
        stdUser.Email = 'stdtestUserak@test.com';
        stdUser.Username = 'stdtestUserak@test.com';
        stdUser.ProfileId = stdProfId;
        stdUser.IsActive = true;
        stdUser.LanguageLocaleKey = 'en_US';
        stdUser.LocaleSidKey = 'en_US';
        stdUser.Alias = stdUser.FirstName.substring(0,1) + stdUser.LastName.substring(0,4);
        stdUser.EmailEncodingKey = 'UTF-8';
        stdUser.TimeZoneSidKey = 'GMT';        
        insert stdUser;
       
        Task taskData = new Task(Priority = 'Normal',Subject = 'Test',Status = 'Scheduled', OwnerId = stdUser.Id);
        insert taskData;
        
    }  
    
    static testMethod void test_user_must_not_be_able_to_delete_task(){
     	User stdUser = [SELECT Id FROM User WHERE Username = 'stdtestUserak@test.com'];
        Task taskData = [SELECT Id FROM Task Where Subject = 'Test' LIMIT 1];
        
        System.runAs(stdUser)
        {   
            try {
            	delete taskData;
            }catch(Exception ex){
                system.debug('ex : ' + ex.getMessage());
            }
        }       
    }     
}

If this helps, please mark it as best answer.

Regards,
Ankaiah

All Answers

CharuDuttCharuDutt
Hii Roopa
Try Below Test Class
@isTest
public class deleteUserTrigger1Test {
    @isTest 
    Public Static Void Unittest(){
        
        Account Acc = new Account();
        Acc.name = ' Test Accocunt';
        Insert Acc;
        Task taskRecord = new Task();
        taskRecord.Subject = 'Task subject';
        //Add your field mappings
        taskRecord.whatid = acc.id;
        Insert taskRecord;
        Delete taskRecord;
        
    }
}
Please Mark It As Best Answer If It Helps
Thank YOU!
AnkaiahAnkaiah (Salesforce Developers) 
Hi Roopa,

Please try with below code getting 100%
@isTest
public class deleteUserTrigger1_Test {

    @testSetup static void setupTestData() {

		Id stdProfId = [Select id from Profile WHERE Name=:'Standard User'].Id;
        User stdUser = new User();
        stdUser.FirstName = 'Admin';
        stdUser.LastName = 'testUser';
        stdUser.Email = 'stdtestUserak@test.com';
        stdUser.Username = 'stdtestUserak@test.com';
        stdUser.ProfileId = stdProfId;
        stdUser.IsActive = true;
        stdUser.LanguageLocaleKey = 'en_US';
        stdUser.LocaleSidKey = 'en_US';
        stdUser.Alias = stdUser.FirstName.substring(0,1) + stdUser.LastName.substring(0,4);
        stdUser.EmailEncodingKey = 'UTF-8';
        stdUser.TimeZoneSidKey = 'GMT';        
        insert stdUser;
       
        Task taskData = new Task(Priority = 'Normal',Subject = 'Test',Status = 'Scheduled', OwnerId = stdUser.Id);
        insert taskData;
        
    }  
    
    static testMethod void test_user_must_not_be_able_to_delete_task(){
     	User stdUser = [SELECT Id FROM User WHERE Username = 'stdtestUserak@test.com'];
        Task taskData = [SELECT Id FROM Task Where Subject = 'Test' LIMIT 1];
        
        System.runAs(stdUser)
        {   
            try {
            	delete taskData;
            }catch(Exception ex){
                system.debug('ex : ' + ex.getMessage());
            }
        }       
    }     
}

If this helps, please mark it as best answer.

Regards,
Ankaiah
This was selected as the best answer
mukesh guptamukesh gupta
Hi Roopa,

Please use below code with 100% code coverage
 
@isTest
private class deleteTastTrigger_Test {
	private static testmethod void taskTriggersTest(){
	Profile prof = [select id from profile where name = 'Standard User'];
	User user = new User();
	user.firstName = 'test1';
	user.lastName = 'test2';
	user.profileId = prof.id,username = 'test@test.com';
	user.email = 'test@test.com';
	insert user;
	system.runAs(user){
	
	  Test.startTest();  
		Account acc = new Account(name='Test Company Name');
		insert acc;
	
	    Task tsk = new Task();
		tsk.Type = 'Email';
		tsk.Description = 'Test Task'; //string
		tsk.OwnerId = user.Id; //user id
		tsk.WhatId = acc.Id; //record id
		insert tsk;
		
		delete tsk;
	
		Test.stopTest();  
    }
}
}

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh
Suraj Tripathi 47Suraj Tripathi 47
Hi Roopa,
Hope you are doing good,
I have written a Test class for your requirement, please follow the below code for your reference.
@isTest
public class deleteUserTrigger1_Test {
    @isTest 
    public static void TriggerTest(){
        
        Profile profileList = new Profile();
        profileList  = [select id from profile where name = 'Standard User'];
        User userObj = new User();
        userObj.IsActive = true;
        userObj.LanguageLocaleKey = 'en_US';
        userObj.LocaleSidKey = 'en_US';
        userObj.EmailEncodingKey = 'UTF-8';
        userObj.TimeZoneSidKey = 'GMT';    
        userObj.firstName = 'abxcfhc';
        userObj.lastName = 'xydz';
        userObj.profileId = profileList.id;
        userObj.Username = 'xyzZZ123@test.com';
        userObj.email = 'xyzZZZZ@testz.com';
        userObj.Alias = userObj.FirstName.substring(0,1) + 
        userObj.LastName.substring(0,4);
        insert userObj;
        
        Account acc = new Account();
        acc.Name = 'Test';
        insert acc;
        
        Task taskObj = new Task();
        taskObj.Type = 'Email';
        taskObj.Description = 'Test Task'; 
        taskObj.OwnerId = userObj.Id; 
        taskObj.WhatId = acc.Id;
        insert taskObj;
        delete taskObj; 
    }
}

I hope you find the above solution helpful. If it does, please mark it as Best Answer to help others too.

Thanks and regards
Suraj Tripathi