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
DeafForceDeafForce 

create tasks from inbound email

Hello Gurus,

 

I hope you are well.

 

I got a request to attach inbound emails to certain SF records if the email has been sent from SF. Similiar to the email2salesforce functionality but for custom objects.

 

What I am trying to achieve is that I want to look for the record ID in the textbody of the email. The user sends out an email using a template containing the record ID and if someone respondes (which will be sent to a routing address)  the class should look for the record ID and attach it appropriately. I hope that made sense ?!

 

What I found is the following and it does work fine for contacts:

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

 

I modified the following part to:

-------------------------------------

try {
      Contact vCustom = [SELECT Id, Name, Email
        FROM CustomObject__c
        WHERE Id = :email.TextBody
        LIMIT 1];
      
      // Add a new Task to the contact record we just found above.
      newTask.add(new Task(Description =  myPlainText,
           Priority = 'Normal',
           Status = 'Inbound Email',
           Subject = email.subject,
           IsReminderSet = true,
           ReminderDateTime = System.now()+1,
           WhatId =  vCustom.Id));
      insert newTask;    
-----------------------------------     

 

However, the error I am getting is "Variable does not exist: TextBody". According to the WSDL the field does exist though.

 

Can you please let me know what I do wrong? If you have an easier solution on how to attach incoming emails (to custom objects) , then please let me know!

 

Any questions please let me know!

 

Thanks in advance and have a great day,

Christian

 

Best Answer chosen by Admin (Salesforce Developers) 
DeafForceDeafForce

Actually got it working but had to use the subject line of the email and include a unqiue ID which is an auto-number field using a particular format such as "AI=0000"

 

 

try {

    String customID;
    
    Pattern p = Pattern.compile('^(.*)(ai\\=\\d+)(.*)$');
    Matcher pm = p.matcher( email.subject);
    if( pm.matches() ){
         customID = pm.group(2);
    }
    
    System.debug('customID _' + customID + '_');
    
      Opportunity vCustom = [SELECT Id, Name, Custom_Unique_Field__c 
        FROM CustomObject__c
        WHERE Custom_Unique_Field__c =: customID 
        LIMIT 1];
    
    

      // Add a new Task to the contact record we just found above.
      newTask.add(new Task(Description =  myPlainText,
           Priority = 'Normal',
           Status = 'Inbound Email',
           subject = email.subject,
           IsReminderSet = true,
           ReminderDateTime = System.now()+1,
           whatId = vCustom.Id
           ));

All Answers

DeafForceDeafForce

Actually got it working but had to use the subject line of the email and include a unqiue ID which is an auto-number field using a particular format such as "AI=0000"

 

 

try {

    String customID;
    
    Pattern p = Pattern.compile('^(.*)(ai\\=\\d+)(.*)$');
    Matcher pm = p.matcher( email.subject);
    if( pm.matches() ){
         customID = pm.group(2);
    }
    
    System.debug('customID _' + customID + '_');
    
      Opportunity vCustom = [SELECT Id, Name, Custom_Unique_Field__c 
        FROM CustomObject__c
        WHERE Custom_Unique_Field__c =: customID 
        LIMIT 1];
    
    

      // Add a new Task to the contact record we just found above.
      newTask.add(new Task(Description =  myPlainText,
           Priority = 'Normal',
           Status = 'Inbound Email',
           subject = email.subject,
           IsReminderSet = true,
           ReminderDateTime = System.now()+1,
           whatId = vCustom.Id
           ));

This was selected as the best answer
Rom_Rom_

Exactly what I needed!

But can't get mine to work...

Getting Error: Compile Error: Illegal assignment from LIST<Test_Email__c> to SOBJECT:Opportunity at line 30 column 7

 

global class CreateTaskEmailExample 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= '';
    
    // 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 unique identifier in subject of email to relate activity to
    try {
    String customID;
    
    Pattern p = Pattern.compile('^(.*)(ai\\=\\d+)(.*)$');
    Matcher pm = p.matcher( email.subject);
    if( pm.matches() ){
         customID = pm.group(2);
    }
    
    System.debug('customID _' + customID + '_');
    
      Opportunity vCustom = [SELECT Id, Name, Email_ID__c 
        FROM Test_Email__c
        WHERE Email_ID__c =: customID 
        LIMIT 1];
    
    

      // Add a new Task to the contact record we just found above.
      newTask.add(new Task(Description = myPlainText,
           Priority = 'Normal',
           Status = 'Inbound Email',
           subject = email.subject,
           IsReminderSet = true,
           ReminderDateTime = System.now()+1,
           whatId = vCustom.Id
           ));
            }  
            catch(DmlException e) {
                System.debug('The following exception has occurred: ' + e.getMessage());
         }
    }
}

 Any help would be GREATLY appreciated :)

DeafForceDeafForce

well your list must query a standard or custom object.

so the "FROM" should query the object where you created field "email_ID__c". Replace "Test_Email__c" with "Opprtunity" if you try get it work for opps

 

    
 
      Opportunity vCustom = [SELECT Id, Name, Email_ID__c 
        FROM Test_Email__c
        WHERE Email_ID__c =: customID 
        LIMIT 1];