function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SatrianodeeSatrianodee 

email service failing with STRING_TOO_LONG

I created an email service to attach emails as taks to a custom object. It works great unless the email content is too large, then the email bounces and I recevie an error email "System.DmlException: Insert failed. First exception on row 0; first error: STRING_TOO_LONG, Description: data value too large: max length=32000" Is there anyway to code around this and have the "myplaintext" content truncated before the email is attached or to strip out the salutation? I tried playing around with the myplaintextistruncated command but that didn't help.
ShashForceShashForce
Hi,

You can probably use the substring method to return only the first 32000 characters. Something like this:

string myplaintext = '............';
return myplaintext.substring(0,32000);

This will return only 32000 characters.

If this answers your question, please mark this as the Best Answer for this post, so that others can benefit from this post.

Thanks,
Shashank
SatrianodeeSatrianodee
Thanks for the quick reply. I couldn’t get the substring to work. Below is my code. I replaced “String myplainText = '';” with your code and received the following compile error. Error: Compile Error: Return value must be of type: Messaging.InboundEmailResult at line 11 column 3 global class CreateSlabReturnEmail implements Messaging.InboundEmailHandler { global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email, Messaging.InboundEnvelope env){ // Create an InboundEmailResult object for returning the result of the // Apex Email Service. Messaging.InboundEmailResult result = new Messaging.InboundEmailResult(); String myplainText = ''; String sbj = email.subject; String slabRefID =''; Integer SRLocate = sbj.indexOf(': RA#') + 5; slabRefID = sbj.subString(SRLocate, SRLocate + 9); // Add the email plain text into the local variable myplainText = email.plaintextBody; // New Task object to be created Task[] newTask = new Task[0]; // Try to look up any slab returns based on the email subject try { Return_Authorization__c vslr = [SELECT Id, Name, Title__c FROM Return_Authorization__c WHERE name = :slabRefID]; // Add a new Task to the slab return record we just found above. newTask.add(new Task(Description = myplainText, Priority = 'Normal', Status = 'Completed', Category__c = 'status', Subject = email.subject, WhatId = vslr.ID)); // Insert the new Task insert newTask; // Save attachments, if any if (email.binaryAttachments != null && email.binaryAttachments.size() > 0) { for (integer i = 0 ; i < email.binaryAttachments.size() ; i++) { Attachment a = new Attachment(ParentId = newTask[0].ID, Name = email.binaryAttachments[i].filename, Body = email.binaryAttachments[i].body); insert a; } } System.debug('New Task Object: ' + newTask ); } // If an exception occurs when the query accesses // the contact record, a QueryException is called. // The exception is written to the Apex debug log. catch (QueryException e) { System.debug('Query Issue: ' + e); } // Set the result to true. No need to send an email back to the user // with an error message result.success = true; // Return the result for the Apex Email Service return result; } }
ShashForceShashForce
Hi,

Substring can only take string as an argument. Please convert your value into a string first and then pass it into the substring method. Please try this and check. Also, it would help if you can provide your code in a more readable format :)

Thanks,
Shashank