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
BrandiTBrandiT 

Need help w Apex Class for Email Service (set date field on new record)

I have an email service that creates a new record in a custom object called Equests.  I need to somehow assign a value for a date field on that newly created custom record.  I was hoping to have the sender would add the due date at the very beginning of the email body and then have the apex class for the email service pull that first part of the email body to populate the due date field.

 

Here is the code I have so far:

 

global class myEquestsHandler_BrandiT implements Messaging.InboundEmailHandler {
      global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {
          Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
         
          equests__c q = new Equests__c();
          q.name = email.subject;
          q.Email_Body__c = email.plaintextbody;
          q.sent_email_address__c = envelope.fromaddress;
          q.assigned_to__c = 'Brandi Tanyaviriya';
          q.due_date__c = email.plaintextbody.substring(0,11);
          insert q;
         
          System.debug('====> Created equest '+equests__c.Id);
         
          if (email.binaryAttachments != null && email.binaryAttachments.size() > 0) {
      for (integer i = 0 ; i < email.binaryAttachments.size() ; i++) {
        Attachment attachment = new Attachment();
        attachment.ParentId = q.id;
        attachment.Name = email.binaryAttachments[i].filename;
        attachment.Body = email.binaryAttachments[i].body;
        attachment.ContentType = 'image/equest';
        insert attachment;
      }
          }
               return result;
      }
  }

 

 

Here is the message I'm getting:

 

Error: Compile Error: Illegal assignment from String to Date at line 10 column 11 

 

 

I know I'm getting the message because I need to use the ValueOf method instead of IndexOf, but I'm not sure how to get it to work.  I've never had to use these methods before.

 

Please help!!

 

Thank you!

Ritesh AswaneyRitesh Aswaney

Its quite straight-forward really, given how far you've gotten :)

 

q.due_date__c = Date.valueof(email.plaintextbody.substring(0,11));

 

It might be wise to a not null check before trying to make a date out of your string.

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_date.htm

 

if due_date__c is a datetime field, then use Datetime.valueOf(....

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_datetime.htm

BrandiTBrandiT

Thank you!

 

The code actually saved with no errors, but my date field did not populate  :(

 

Here is my code:

 

 

global class myEquestsHandler_BrandiT implements Messaging.InboundEmailHandler {
      global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {
          Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
          
          equests__c q = new Equests__c();
          q.name = email.subject;
          q.Email_Body__c = email.plaintextbody;
          q.sent_email_address__c = envelope.fromaddress;
          q.assigned_to__c = 'Brandi Tanyaviriya';
          q.due_date__c = Date.valueof(email.plaintextbody.substring(0,11));
          insert q;
          
          System.debug('====> Created equest '+equests__c.Id);
          
          if (email.binaryAttachments != null && email.binaryAttachments.size() > 0) {
      for (integer i = 0 ; i < email.binaryAttachments.size() ; i++) {
        Attachment attachment = new Attachment();
        attachment.ParentId = q.id;
        attachment.Name = email.binaryAttachments[i].filename;
        attachment.Body = email.binaryAttachments[i].body;
        attachment.ContentType = 'image/equest';
        insert attachment;
      }
          }
               return result;
      }
  }

 

 

 

In my email I put this date in the very first line of the email body:

2011-06-01

 

but it didn't work.

 

Should I try the date in a different format or is there something else I missed?

 

Thanks again!