You need to sign in to do that
Don't have an account?
Robert Wambold 10
Test Class for Case Comment Delete - Code Coverage Help PLEASE
Hello All,
I have a simple trigger, PreventCCDeletion that should only allow Users with Profile='System Administrator-2012' or with Permission Set='Super User' to delete Case Comments. The trigger works well...unfortunately I cannot reach the proper Code Coverage and cannot get over 66%.
Thanks in advance for your help.
Kned regards,
Robert
Here is my trigger:
trigger PreventCCDeletion on CaseComment (before delete) { Boolean YouCanDelete; YouCanDelete=FALSE; for(CaseComment cc : trigger.old) { // Check for 'System Administrator-2012' Profile List<Profile> PROFILE = [SELECT Id, Name FROM Profile WHERE Id=:userinfo.getProfileId() LIMIT 1]; String MyProfileName = PROFILE[0].Name; If (MyProfileName=='System Administrator-2012'){ YouCanDelete=TRUE; } // Check for 'Super User' ( 0PS0y000000D0dvGAC ) Permission Set If (YouCanDelete=FALSE){ List<PermissionSetAssignment> SUPERUSER = [SELECT PermissionSet.Name FROM PermissionSetAssignment WHERE AssigneeId= :UserInfo.getUserId() AND PermissionSet.Name = 'Super_User' LIMIT 1]; String MyPermissionName = SUPERUSER[0].PermissionSet.Name; If (MyPermissionName=='Super_User'){ YouCanDelete=TRUE; } } If (YouCanDelete=FALSE) { cc.adderror('Case Comments cannot be deleted'); } } }
Here is my test class:
@isTest public class PreventCCDeletion_Test{ static Case tCase; static CaseComment tComment; static Profile tProfile; static User tUser; // Start of Test 1 static void createTestData1(){ tCase = new Case(); tCase.Status = 'Open'; tCase.Description = 'Test Description-1 '; tCase.Origin = 'Annuity External'; tCase.Type = 'Feature Request'; tCase.Priority = 'Low'; INSERT tCase; tComment = new CaseComment(); tComment.ParentId = tCase.Id; tComment.CommentBody = 'Some Comment-1'; tComment.IsPublished = TRUE; INSERT tComment; tProfile = [ SELECT Id FROM Profile WHERE Name = 'System Administrator-2012' ]; tUser = new User( Alias = 'standt', Email='standarduser@testorg.com', EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', LocaleSidKey='en_US', ProfileId = tProfile.Id, TimeZoneSidKey='America/Los_Angeles', UserName='testuser@sometestorg.com', Non_Use_Send_Deactivation_Alert__c=FALSE, DefaultCurrencyIsoCode='USD', CurrencyIsoCode='USD', ReceivesAdminInfoEmails=FALSE, ByPass_New_Hire_Welcome__c=TRUE ); INSERT tUser; } testMethod static void execCaseCommentDelete1(){ test.startTest(); createTestData1(); try{ System.runAs(tUser) { DELETE tComment; } } catch(Exception e){ } test.stopTest(); } // End of Test 1 // Start of Test 2 static void createTestData2(){ tCase = new Case(); tCase.Status = 'Open'; tCase.Description = 'Test Description-2 '; tCase.Origin = 'Annuity External'; tCase.Type = 'Feature Request'; tCase.Priority = 'Low'; INSERT tCase; tComment = new CaseComment(); tComment.ParentId = tCase.Id; tComment.CommentBody = 'Some Comment-2'; tComment.IsPublished = TRUE; INSERT tComment; tProfile = [ SELECT Id FROM Profile WHERE Name = 'Prod IT Support User' ]; tUser = new User( Alias = 'standt2', Email='standarduser2@testorg.com', EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', LocaleSidKey='en_US', ProfileId = tProfile.Id, TimeZoneSidKey='America/Los_Angeles', UserName='testuser2@sometestorg.com', Non_Use_Send_Deactivation_Alert__c=FALSE, DefaultCurrencyIsoCode='USD', CurrencyIsoCode='USD', ReceivesAdminInfoEmails=FALSE, ByPass_New_Hire_Welcome__c=TRUE ); INSERT tUser; } testMethod static void execCaseCommentDelete2(){ test.startTest(); createTestData2(); // Query your permission set name from Organization that your want to test. PermissionSet PS2 = [SELECT Id FROM PermissionSet WHERE Name = 'Super_User']; PermissionSetAssignment PSA2=new PermissionSetAssignment (AssigneeId = tUser.Id,PermissionSetId = PS2.Id); try{ System.runAs(tUser) { insert PSA2; System.debug('****testPS2....'+PS2); DELETE tComment; } } catch(Exception e){ } test.stopTest(); } // End of Test 2 }
You need to change
If (YouCanDelete=FALSE){
to
If (YouCanDelete==FALSE){
All Answers
What happens if you just run the one method, check the coverage. Then later run the other method and check the coverage. Does the test coverage shift to different areas of your code?
Hi Tad,
When I execute both methods or just the first method I acheive 68% code coverage. When only the second method is executed I acheive 0%.
I believe my required PermissionSetAssignment for the second methed ius the problem?
Thanks for your help.
Robert
You need to change
If (YouCanDelete=FALSE){
to
If (YouCanDelete==FALSE){