function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Appy123Appy123 

Before Delete Trigger on CaseComment showing Null values

Hi,

I have created the following trigger on caseComment.

if(Trigger.isBefore && Trigger.isDelete)
{
cls_Case_Comment.CheckAssignedPermissionSet(trigger.old);
}
}

Below is the Helper Class:
Public with sharing class cls_Case_Comment {

Public Static Void CheckAssignedPermissionSet(List<CaseComment>newList)
{
/* This Method is used as Helper Method for trigger trg_Case_Comment 

Set<String>str_set= new Set<String>();
String profile_Name =[SELECT Name from Profile where Id=:UserInfo.getProfileId()].Name;   

List<PermissionSet>lst_PermissionSet = [SELECT Id, Name from PermissionSet where ID in ( SELECT PermissionSetId FROM PermissionSetAssignment WHERE AssigneeId =:Userinfo.getUserId())];

for(PermissionSet ps:lst_PermissionSet){
str_set.add(ps.Name);
}

for(CaseComment cs:newList)
{
         if(profile_Name!='System Administrator'){
         if(!str_set.contains('Delete_cases'))
           {
                cs.addError(system.label.trg_cls_Case_Comment);
           }
         }

}

}

}

Everything is Working Correctly except on Deleting the CaseComment, I am getting a null value.

Error message is displaying correctly
User-added image
ra811.3921220580267847E12ra811.3921220580267847E12
Hi,

Look into debug logs so that you will identify where problem occurs exactly in class.
Himanshu ParasharHimanshu Parashar
Hi Aproova,

I think you are missing modify all permission for that Case comment object in permission set because Salesforce docs says that:

In the Salesforce user interface, comments are generally entered by a User working on a Case. All users have access to create and view CaseComment in the Salesforce user interface and when using the API. In the API, CaseComment records can't be modified after insertion unless the user has the “Modify All” object-level permission for Cases or the “Modify All Data” permission. If not, users can only update the IsPublished field, and can't delete CaseComment.

Thanks,
Himanshu
Salesforce Certified Developer | Administrator | Service Cloud Consultant

P.S. If my answer helps you to solve your problem please mark it as best answer and upvote it.
Appy123Appy123
Hi  d
Thanks for your response.
I have checked the debug logs also but no luck. Same thing is working for other objects.
Also, I have checked that Profile is having "Modify All" permission and casecomment will have the same.
Himanshu ParasharHimanshu Parashar
Hi Apoorva, 
Can you try once using sys admin permission whether you are getting same issue from that profile as well or not ?
it will narrow down the issue.
Appy123Appy123
Hi Himanshu,
I have modify my trigger to allow  profile other than system admin.
trigger casecommentdelete on CaseComment (before delete) {

Set<String>str_set= new Set<String>();
String profile_Name =[SELECT Name from Profile where Id=:UserInfo.getProfileId()].Name;   

//List<PermissionSet>lst_PermissionSet = [SELECT Id, Name from PermissionSet where ID in ( SELECT PermissionSetId FROM PermissionSetAssignment WHERE AssigneeId =:Userinfo.getUserId())];



for(CaseComment cs:trigger.old)
{
         if(profile_Name.contains('System Administrator')){
         //if(!str_set.contains('Delete_cases'))
           {
                cs.addError('you can not delete case');
           }
         }

}

}

But If I login as a System Admin and try to delete the record, it is still showing the Null Value.
Trigger is working fine, my error mesage is getting captured ie 'you can not delete case' . But I wonder why it is showing  deletion problem null.
You can also try the same code in your DEV org. For other objects it is not showing but for CaseComment.