I get the following error:
FIELD_CUSTOM_VALIDATION_EXCEPTION : Automatic email loop has been terminated
I have made a trigger on cases to kill email loops. When I google the error I get that it could be a missing field, where I am left in the dark where i have no clue which field should be missing and there are no validation rules on cases. Here is my code, please some explanation:
trigger beforeInsertOnCaseLoopKiller on Case (before insert) {
// Will not process new email if there are at least 4 emails from
// the same email address with the same subject and the previous email
// was less than 5 minutes old.
case[] c = trigger.new;
case[] check = [select ID, CreatedDate, subject from Case where SuppliedEmail = :c[0].SuppliedEmail and subject = :c[0].subject and isclosed = false order by CreatedDate desc];
system.debug(c[0].SuppliedEmail);
system.debug(c[0].subject);
if(c[0].Subject != null) {
//We have a subject, proceed.
if(c[0].subject.contains('[ ref:')){
//No Errors. Email should be attached to the case.
}else{
if(check.size() > 3){
if((check[0].createddate.addMinutes(5) > System.now()) && check[0].subject.contains(c[0].subject)){
//Loop Was Killed.
Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
message.toAddresses = new String[] {[SELECT Loopkiller_Email_support__c FROM Loopkiller__c LIMIT 1][0].Loopkiller_Email_support__c};
message.optOutPolicy = 'FILTER';
message.subject = 'Email loop caused';
message.plainTextBody = 'An email loop has been caused by case '+c[0].subject + '\n' + 'This loop was generated by email adres '+ c[0].SuppliedEmail + '.';
Messaging.SingleEmailMessage[] messages =
new List<Messaging.SingleEmailMessage> {message};
Messaging.SendEmailResult[] results = Messaging.sendEmail(messages);
if (results[0].success) {
System.debug('The email was sent successfully.');
} else {
System.debug('The email failed to send: '
+ results[0].errors[0].message);
}
c[0].addError('Automatic email loop has been terminated');
}else{
//New Case should be created now!
}
}
}
}
}