Hey there this is my first Apex code and I'm having trouble finding my debug logs. Heres my Trigger and Test Class First, then below I'll try and explain all that I've tried so far.
trigger Task_Delete_Restriction on Task (before delete) {
String currentUserId = UserInfo.getUserId();
Profile userProfile = [SELECT Id, Name FROM Profile WHERE Id=:userinfo.getProfileId() LIMIT 1];
for (Task t : Trigger.old) {
if(t.CreatedById <> currentUserId &&
userProfile.name <> 'System Administrator' &&
userProfile.name <> 'Sales' &&
userProfile.name <> 'Case Team Managers and Team Leads' &&
userProfile.name <> 'Case Managers' &&
userProfile.name <> 'Account Managers' &&
userProfile.name <> 'Power User Claims' ) {
t.addError('You do not have permission to Delete this Task because you did not create it: Apex Trigger - "Task_Delete_Restriction"');
}
}
}
@isTest
Private class Task_Delete_RestrictionTest{
@testSetup static void setup(){
String ProfileId = [SELECT id FROM Profile WHERE Name='Standard User'].Id;
String SalesUser = [SELECT id FROM Profile WHERE Name='Sales'].Id;
//Create 3 x user records
User user1 = createUser('userOne@One.net', 'userOne','userOne@none.net','userOne',profileId);
Insert user1;
User user2 = createUser('user@Two.net', 'userTwo','test@none.net','userTwo',profileId);
Insert user2;
User user3 = createUser('user@Three.net', 'user3','userThree@none.net','user3',salesUser);
Insert user3;
Task testTask = new Task();
testTask.OwnerId = user1.Id;
testTask.CreatedById = user1.Id;
testTask.Subject='Delete Me';
testTask.Status='Not Started';
testTask.Priority='claims';
insert testTask;
}
@isTest
static void taskDelete_SysAdmin(){
//test that a system admin can delete the record
//usually, the person running the test code is an Admin, so there is no need to set a "runnng user" for the system admin test - but you could always create another user and assign the system admin profile and do a Runas
//get the task we created above
Task testTask = [SELECT Id FROM Task WHERE Subject = 'Delete Me' LIMIT 1];
System.assertNotEquals(null,testTask.Id); //sanity check to see we got a record
Test.startTest();
try{
delete testTask;
}catch(DMLexception e){
//these should not actually run
Boolean containsError = e.getMessage().contains('You do not have permission to Delete this Task because you did not create it: Apex Trigger - "Task_Delete_Restriction"');
System.debug('Error: ' + e.getMessage());
System.assert(containsError);
}
Test.stopTest();
Task deletedTask = [SELECT Id, IsDeleted FROM Task WHERE Subject = 'Delete Me' ALL ROWS];
System.assertEquals(true,deletedTask.IsDeleted);
}
@isTest static void taskDelete_AsCreator(){
//test that the Creator can delete the record
//get the task we created above
List<Task> testTask = [SELECT Id FROM Task WHERE Subject = 'Delete Me' LIMIT 1];
System.assertEquals(1,testTask.size()); //sanity check to see we got a record
User Creator = [SELECT Id FROM User WHERE Alias = 'userOne'];
Test.startTest();
System.runas(Creator){
try{
delete testTask;
} catch(DMLexception e){
//these should not actually run
Boolean containsError = e.getMessage().contains('You do not have permission to Delete this Task because you did not create it: Apex Trigger - "Task_Delete_Restriction"');
System.debug('Error: ' + e.getMessage());
System.assert(containsError);
}
}
Test.stopTest();
Task deletedTask = [SELECT Id, IsDeleted FROM Task WHERE Subject = 'Delete Me' ALL ROWS];
System.assertEquals(true,deletedTask.IsDeleted);
}
@isTest static void taskDelete_NotAsCreator(){
//test that the Non owner cannot delete the record
//get the task we created above
List<Task> testTask = [SELECT Id FROM Task WHERE Subject = 'Delete Me' LIMIT 1];
System.assertEquals(1,testTask.size()); //sanity check to see we got a record
User other = [SELECT Id FROM User WHERE Alias = 'userTwo'];
Test.startTest();
System.runas(other){
try{
delete testTask;
} catch(DMLexception e) {
Boolean containsError = e.getMessage().contains('You do not have permission to Delete this Task');
System.debug('Error: ' + e.getMessage());
System.assert(containsError);
}
}
Test.stopTest();
}
@isTest static void taskDelete_SalesUser(){
//test that the Non owner cannot delete the record
//get the task we created above
List<Task> testTask = [SELECT Id FROM Task WHERE Subject = 'Delete Me' Limit 1];
System.assertEquals(1,testTask.size()); //sanity check to see we got a record
User other = [SELECT Id FROM User WHERE Alias = 'user3'];
Test.startTest();
System.runas(other){
try{
delete testTask;
} catch(DMLexception e) {
Boolean containsError = e.getMessage().contains('You do not have permission to Delete this Task');
System.debug('Error: ' + e.getMessage());
System.assert(containsError);
}
}
Test.stopTest();
Task deletedTask = [SELECT Id, IsDeleted FROM Task WHERE Subject = 'Delete Me' ALL ROWS];
System.assertEquals(true,deletedTask.IsDeleted);
}
//create a method to quickly create the user if you don't have a utility class
public static User createUser(String uName, String lName, String eAdd, String alias, String profileId) {
User user = new User();
user.Username = uName;
user.LastName = lName;
user.Email = eAdd;
user.Alias = alias;
user.TimeZoneSidKey = 'America/New_York';
user.LocaleSidKey = 'en_US';
user.EmailEncodingKey = 'ISO-8859-1';
user.ProfileId = profileId;
user.LanguageLocaleKey = 'en_US';
return user;
}
}
So, I'm failing on taskDelete_NotAsCreator()
and taskDelete_SalesUser()
. My goal is to make a trigger that will prevent certain users from deleting tasks unless they created them. On the two failing tests the developer console says Assertion Failed. Which I think means that the System.assert(containsError)
line is returning false. To try and trouble shoot this I put in the System.debug(e.getMessage())
to try and learn what the DMLexception is returning. However where I am stuck is I can't find the debug logs.
To try and find the debug logs I have gone into salesforce->setup->Apex Test Execution and ran a test. From there I returned back to the developer console to find the ApexTestHandler log. However when I filter for "Debug Only" nothing comes up. If someone could help me find my debug files it would be greatly appreciated.
System.assert()
(or the 3-arg versions ofSystem.assertEquals()
andSystem.assertNotEquals()
). That extra argument is a custom string that gets appended to the message on a failed assertion, which would be visible in the test results (meaning you wouldn't need to try to find it in the debug log).System.debug()
wouldn't be logged as the default log level used (when you use the 1-arg version ofSystem.debug()
) isDebug
.System.assert()
how would I go about using that? I'm really new to apex and coding in general. But yes my end goal is more to get this test class to function however being able to access the debug files easily will be really helpful for future triggers and test classes.