You need to sign in to do that
Don't have an account?
SFDC12
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.
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.
Change the test class as below to get 100%.
If this solution helps, Please mark it as best answer.
Thanks
All Answers
Change the test class as below to get 100%.
If this solution helps, Please mark it as best answer.
Thanks
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,