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];

    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.
            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');

                    //New Case should be created now!       
  • 1
    You are introducing the error with the addError method. Looks like it is working as coded – Eric May 1 '17 at 13:00
  • Also note that your code isn't properly written to support the bulk ccase, as you only consider the first record. – Adrian Larson May 1 '17 at 13:46
  • @AdrianLarson, your completely right Im not so familiar with bulkfying yet but I will be looking into It, thanks for the remark. – user42169 May 3 '17 at 8:10

You are directly causing this error (if I recall, you were looking to create some unit tests for this trigger). As noted in the comments, the text of the error message matches the text that you're sending to addError() in your code.

When you add an error message to a record in an appropriate trigger context variable using addError() in a before trigger, that will cause the execution of your trigger to stop. This is noted in the documentation on SObject methods for addError().

From that page:


Marks a record with a custom error message and prevents any DML operation from occurring.


When used on Trigger.new in before insert and before update triggers, and on Trigger.old in before delete triggers, the error message is displayed in the application interface.

When used in Visualforce controllers, the generated message is added to the collection of errors for the page. For more information, see Validation Rules and Standard Controllers in the Visualforce Developer's Guide.

The class of error that you get when this happens is FIELD_CUSTOM_VALIDATION_EXCEPTION, which is a bit misleading (as it has nothing to do with validation rules. If anything, it's indicative of when, in the order of execution, this happens), but that's the way Salesforce does things.

  • I wouldn't say it's misleading. You are throwing a validation exception by calling addError. It's code meant specifically for validation purposes. – Adrian Larson May 1 '17 at 13:45
  • @AdrianLarson Perhaps, though I do think that using FIELD_CUSTOM_VALIDATION_EXCEPTION for both validation rules and .addError() is, at the very least, not great for clarity (also, is the FIELD part of the message appropriate given that the error isn't being added to a specific field?). Given OP's code, it was easy to see in this case, but I myself have seen this type of error, and immediately went looking for validation rules instead of trigger code. – Derek F May 1 '17 at 13:57
  • Fair enough on the inclusion of FIELD_. – Adrian Larson May 1 '17 at 13:58
  • @DerekF When i test this in my sandbox it is working fine when I deploy it to production it reproduces this error. How can I break the loop without using AddError??? – user42169 May 2 '17 at 8:04
  • @user42169 addError() is a perfectly fine way of doing things. If your tests are passing in your developer/QA sandbox, but not in prod, then you should try deploying your test class along with your trigger here. If that doesn't work, then your tests need more work. You seem to have not read anything I wrote in the comments on your 36% coverage question. If you need more help on your unit tests (and there should be more than one), start by looking at this question: salesforce.stackexchange.com/questions/84025/… – Derek F May 2 '17 at 12:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.