You need to sign in to do that
Don't have an account?
TBouscal
Apex data validation
Attempting to create a validation rule that insures the end user closing a Case has already entered a Comment on that Case but I can't seem to get the map to work correctly. Here's what I have in the trigger;
What am I overlooking?
trigger Cases on Case (<etc>){ if(Trigger.isBefore && Trigger.isUpdate){ CloseValidation.CheckValidation(Trigger.new); } }and here's the class;
public class CloseValidation { public static void CheckValidation(Case[] cs) { if(UserInfo.getProfileId()!='00e30000000d2ft'){ // bypass for Salesforce Administrator profile // map<Id, CaseComment> CaseCommentMap = new map<Id, CaseComment>(); Id thisUser = UserInfo.getUserId(); for (Case c:cs){ if(c.Status=='Closed' && c.RecordTypeId=='01270000000Q9E1'){ CaseCommentMap.Put(c.id,null); } } CaseCommentMap.remove(null); CaseCommentMap.putAll([Select Id FROM CaseComment Where ParentId in : CaseCommentMap.keyset() AND CreatedById =:thisUser]); // if CaseCommentMap has a null for the CaseComment then error for(Case c:cs){ if(!CaseCommentMap.containsKey(c.id) && c.Status=='Closed'){ // no key - fail c.addError('You must have commented to close the case (CaseValidation.apx) ' + c.id ); } else { // has key - pass } } } } }
What am I overlooking?
You are doing wrong CaseCommentMap.putAll([Select Id FROM CaseComment Where ParentId in : CaseCommentMap.keyset() AND CreatedById =:thisUser]); line . In this case Map will contain CaseComment Id as Key and CaseComment as a value and then this will never pass if(!CaseCommentMap.containsKey(c.id) && c.Status=='Closed') and you will always get Error.
I have made some tweaked and below are the code for both trigger and Class.
Trigger. Class.
Let me know the outcomes.
Thanks,
Amit Singh.
All Answers
You are doing wrong CaseCommentMap.putAll([Select Id FROM CaseComment Where ParentId in : CaseCommentMap.keyset() AND CreatedById =:thisUser]); line . In this case Map will contain CaseComment Id as Key and CaseComment as a value and then this will never pass if(!CaseCommentMap.containsKey(c.id) && c.Status=='Closed') and you will always get Error.
I have made some tweaked and below are the code for both trigger and Class.
Trigger. Class.
Let me know the outcomes.
Thanks,
Amit Singh.