Tuesday, 25 March 2014

How to write validation Rule for casecomment?

Question


 
I want to write validation Rule for case comments. SO if any users wants to delete/update  the case comments then it wont allowed deletion or modification. But i saw that there is no field like comments in the case fields. Then how could i acheive this ?
User-added image 
 
 

Answer 
 
trigger trgr_Case_CaseCommentLock on CaseComment (before Update, before Delete) {
    
    List<Profile> currentUserProfile = new List<Profile>();
    
    currentUserProfile = 
        [SELECT Name FROM Profile WHERE Id = :UserInfo.getProfileId() LIMIT 1];
    
    if(
        !currentUserProfile.isEmpty() &&
        currentUserProfile[0].Name != 'System Administrator'
    )
        if(trigger.isDelete)
            for(CaseComment cm : trigger.old)
                cm.addError('You cannot Delete a Case Comment!');
        else
            for(CaseComment cm : trigger.new)
                cm.addError('You cannot Edit a Case Comment!');        
}

No comments:

Post a Comment