+ Start a Discussion
DivyaReddyDivyaReddy 

Regarding test case

Hi

This is my trigger and can any one help me with the test case

trigger trgDeleteTaskDisabled on Task (before Delete) {
String ProfileName = [SELECT Profile.Name FROM User where Id = :Trigger.old[0].OwnerId].Profile.Name;
If(ProfileName != 'System Administrator')
    Trigger.old[0].addError('Task once created cannot be deleted');
}

plz help me with the test case to get 100 percent this is the test case i have written
@isTest
private class trgDeleteEventDisabled_TESTS
{
static testMethod void mytestclass()
{
User u = new User();
u.LastName = 'test';
u.Alias = 't';
u.Email = 'r@gmail.com';
u.Username = 'name';
u.CommunityNickname = 'mane';
u.UserRoleid = 'CRO';
u.Profileid = 'Accounts';
u.EmailEncodingKey = 'Unicode (UTF-8)';
u.Profile.Name = 'tset';
insert u ;


Task t = new Task();
t.ownerid = u.id;
t.Subject = 'test';
t.Task_Type__c = 'Cold Call';
t.Status = 'In Progress';
t.Priority = 'High';
insert t;

delete t ;
}
}

 

 

pankaj.raijadepankaj.raijade

The issue in your test class could be because the user you created may not have profile as "system administrator"

 

so first query system adming profile assigne it to new user you are creating and then assign user to record.

 

rest code is correct.

TejTej
@isTest
private class trgDeleteEventDisabled_TESTS{
	static testMethod void mytestclass(){
		User u = new User();
		u.LastName = 'test';
		u.Alias = 't';
		u.Email = 'r@gmail.com';
		u.Username = 'name';
		u.CommunityNickname = 'mane';
		u.UserRoleid = 'CRO';
		u.Profileid = 'Accounts';
		u.EmailEncodingKey = 'Unicode (UTF-8)';
		u.Profile.Name = 'tset';
		insert u ;
		
		System.runAs( u ) {
			Task t = new Task();
			t.ownerid = u.id;
			t.Subject = 'test';
			t.Task_Type__c = 'Cold Call';
			t.Status = 'In Progress';
			t.Priority = 'High';
			insert t;
			
			Task tk = [select Id from Task where id = :t.Id];
			
			try{
				delete tk ;
			} catch (exception e ){
				//exception			
			
			}
		}
	}
}

 Try this one n see if it works