• Ailedroc
  • 5 Points
  • Member since 2009

  • Chatter
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
  • 1

I am writing some Apex code to implement my own version of Email-to-Case.


Using the API Developer's Guide information about the fields in the EmailMessage standard object, I was able to include the following code for creating a new EmailMessage in my handleInboundEmail method.  However, the API documentation does not specify any attachment fields aside from the boolean HasAttachment.  Is there a way I can add email.binaryAttachments and email.textAttachments to the new EmailMessage object?



// new EmailMessage object to be created
EmailMessage[] newEmail = new EmailMessage[0];

// Lookup case based on the case number
Case vCase = [Select Id, CaseNumber, ContactId, OwnerId
From Case Where CaseNumber = :myCaseNumber Limit 1];

String ownerIdStr = vCase.OwnerId;
System.debug('Owner ID as a string = ' + OwnerIdStr);

newEmail.add(new EmailMessage(
FromAddress = email.fromAddress,
FromName = email.fromName,
Incoming = true,
MessageDate = System.now(),
ParentId = vCase.Id,
Status = '0',
Subject = email.subject,
TextBody = email.plainTextBody,
ToAddress = email.toAddresses[0]));

// Insert the new EmailMessage into the case
insert newEmail;



Message Edited by Ailedroc on 07-22-2009 08:25 AM

I need to create a "Reply" button for a Task Detail Page that allows a user to reply to a case task when its status is Inbound Email.  I am trying to pass parameters to the EmailAuthor.jsp URL, as suggested in the Quick Email Button Salesforce blog, so that clicking on the "Reply" button brings a user to the Send Email page with the Task's Comments already in the email body.  I tried creating an email template with {!Task.Description} in the body, but email templates cannot access Task fields.  I am currently trying to change the body of the email through the parameters, like so:




However, {!Task.Description} is too long to fit into the URL, and I get the following error message: "A problem with the OnClick JavaScript for this button or link was encountered: unterminated string literal."  Does anyone have any suggestions for how I can get around this?

I am trying to implement my own email-to-case class and I have the following code, which is working in my sandbox, to create an EmailMessage on a case using email services:
EmailMessage[] newEmail = new EmailMessage[0];
newEmail.add(new EmailMessage(FromAddress = email.fromAddress,
FromName = email.fromName,
ToAddress = email.toAddresses[0],
Subject = email.subject,
TextBody = email.plainTextBody,
HtmlBody = email.htmlBody,
ParentId = newCase[0].Id, 
ActivityId = newTask[0].Id));   // (newCase and newTask are the newly created case and task from earlier code)
insert newEmail;
I have several questions.  Is it possible to set the email message status to "New"?  If I attempt to add "Status = 'New'" in the .add() method I get an error: Message: Insert failed. First exception on row 0; first error: INVALID_OR_NULL_FOR_RESTRICTED_PICKLIST, Status: bad value for restricted picklist field: New: [Status] null.  If I don't attempt to set the status, it defaults to "Sent".
Also, does anyone have any sample code to share that adds headers and attachments from an inbound email to an Email Message object?  I'm struggling with that one.
Another minor issue that is bugging me is that in the Email Section of the Case page in my sandbox, the column labelled "Email Address" shows the 'to address' and my production version using the the standard SFDC email to case application will have the 'from address' (contact's address) displayed.  Does anyone know what field in the Email Message object this data comes from?
Thanks for the help!