On case creation from an email2case process, the case description gets populated with a text version of the email.

In the scenario of when the inbound email email is large, I was expecting the text to be truncated to 32000 characters to fit in the case description field.

What I am observing instead is the case description length is larger than 32000 by a small amount (ie. btw 1-2000 over limit). This means when an agent tries to make changes to the case and hits save, it shows them an error message "Error: Invalid Data", forcing them to truncate the description field themselves.

I have tried truncating the description field in a before insert/update trigger

    for (Case c : (Case[]) Trigger.new) {
        String d = c.description;
        if (!string.isBlank(d) && d.length() >= 32000) {
            c.description = d.substring(0, 32000);

but the if condition never appears to be true, even if the description data is longer than 32000 characters.

What options do I have to fix this?

  • I believe the issue has to do with line breaks. When the user edits the saved E2C case, SFDC converts the database data into HTML and upon save, the HTML is converted back to line breaks but extra characters are added (I think \r but I may be wrong). Hence, the save fails – cropredy Aug 8 '19 at 6:00

As per the Order of Execution, before insert trigger would never be fired if there is a field length exception i.e. it will throw the error at field length check itself.

Excerpt from the above documentation

  1. Loads the original record from the database or initializes the record for an upsert statement.

  2. Loads the new record field values from the request and overwrites the old values.

    If the request came from a standard UI edit page, Salesforce runs system validation to check the record for:

    Compliance with layout-specific rules Required values at the layout level and field-definition level

    Valid field formats

    Maximum field length

    When the request comes from other sources, such as an Apex application or a SOAP API call, Salesforce validates only the foreign keys. Before executing a trigger, Salesforce verifies that any custom foreign keys do not refer to the object itself.

    Salesforce runs user-defined validation rules if multiline items were created, such as quote line items and opportunity line items.

  3. Executes all before triggers.

There is an idea posted for similar issue i.e. Allow field length validation in before insert trigger, you can up-vote it and wait for Salesforce to take this up in future releases. Untill then, I don't think we have any workarounds for this.

Email-to-case will create the case successfully but it will truncate the inbound emails, so it won't contain the entire email if the email exceeds the length limit.

Here is an help article from salesforce Truncated inbound emails when you use Email-to-Case which you can refer and check the size of the description and body of the email message.

  • The before insert trigger is firing for cases created by email2case. Other parts of the trigger is working fine, ditto with process builder. Since the email2case process is creating the case successfully, it isn't throwing a STRING_TOO_LONG exception – user2610036 Aug 7 '19 at 3:55
  • Check the size of the htmlbody and textbody of email message and see if any of them has exceeded 32000 limit. Updated the answer. – Vijay Ganji Aug 7 '19 at 4:22

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.