You need to sign in to do that
Don't have an account?
Validation rule conflicts with after insert trigger : FIELD_CUSTOM_VALIDATION_EXCEPTION error
Hi Guys, i have a after insert trigger on the Case object to auto-update some fields.
Besides that, i have one validation rule to make sure a Date/Time field value not earlier than the record created Date/Time.
According to salesforce rule of "Triggers and Order of Execution", the user-defined validation rules run before the 'after insert' trigger. However, they seems conflict with each other now for my trigger and validation rule.
My after insert trigger Code:
trigger triggerOnCase on Case(after insert){
//Declarations
set<id> caseIds = new set<id>();
set<id> userIds = new set<id>();
Long BHPerDay = 24;
BusinessHours stdBH = [select id from businesshours where isDefault = true];
if(trigger.isAfter&&trigger.isInsert){
for(integer x=0; x<trigger.newMap.size();x++){
caseIds.add(trigger.new[x].id);
userIds.add(trigger.new[x].ownerId);
}
map<id, Case> caseMap = new map<id, Case>([select id,
ownerId,//Case Owner Role
owner.type,
Case_Owner_Role__c,
Priority, //Expected Closed Date
CreatedDate,
Expected_Closed_Date__c
from case where id in: caseIds]);
map<id, User> userMap = new map<id, User>([select u.id, u.UserRole.Name from User u where u.Id in :userIds]);
for(id i:caseIds){
Case newCase = caseMap.get(i);
//store case owner role
newCase.Case_Owner_Role__c = userMap.get(newCase.ownerId).UserRole.Name;
//Assign Expected Closed Date
if(newCase.Priority == 'Low'){
newCase.Expected_Closed_Date__c = BusinessHours.add(stdBH.id, newCase.CreatedDate, 3*BHPerDay*3600*1000L);}
if(newCase.Priority == 'High'){
newCase.Expected_Closed_Date__c = BusinessHours.add(stdBH.id, newCase.CreatedDate, 7*BHPerDay*3600*1000L);}
if(newCase.Priority == 'Special'){
newCase.Expected_Closed_Date__c = BusinessHours.add(stdBH.id, newCase.CreatedDate, 7*BHPerDay*3600*1000L);}
}
update caseMap.values();
}
}
My validation rule is as follows:
OR(( Call_Back_Date_Time__c < CreatedDate )&&NOT(ISCHANGED( Call_Back_Date_Time__c )), (Call_Back_Date_Time__c < NOW())&&ISCHANGED(Call_Back_Date_Time__c))
when I inserted a new record, i key in a date/time earlier than current time, it should display the error message like "Please ensure that Call Back Date/Time is later than the current Date ."
Instead, I get the follow one:
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger triggerOnCase caused an unexpected exception, contact your administrator: triggerOnCase: execution of AfterInsert caused by: System.DmlException: Update failed. First exception on row 0 with id 50090000002ure0AAA; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Please ensure that Call Back Date/Time is later than the current Date & Time: [Call_Back_Date_Time__c]: Trigger.triggerOnCase: line 144, column 5
I don't know how to add try catch operation on the map. I tried the following to replace the " update caseMap.values();"
try{update caseMap.values();}
catch(System.DmlException e){for (Integer i = 0; i < e.getNumDml(); i++) {
eMessage = e.getDmlMessage(i);
Case errorCase = [select id from Case where id =:e.getDmlId(i)];
errorCase.addError(eMessage);
}}
while it threw me with another error message " System.FinalException: SObject row does not allow errors".
Anyone can help?
What behaviour you are expecting for the validation rule in case of after events..???
Validation rule might be getting executed after "before triggers" and before "after triggers" but error you would see after execution of after triggers.Since your validation is failed and record gets commited in the final step, salesforce is throwing you error.
Hi,
Yes you are right the sequences are as follows:
Standard Validation then
Before Trigger then
Custom Validation then
After Trigger
Hence firstly I would suggest you that disable you trigger for a while and check wither your validation is working fine or not.
Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved