+ Start a Discussion
Ellsa JamesEllsa James 

How do I check against email subject in Apex class?

I have set up an email service and Apex class that handles inbound emails and creates a new task with the details from the email. The task is then related to a custom object based on the subject line of the email matching a custom field on the custom object record. This is working fine at the moment but I need to tweek the criteria the check if the subject of the email contains the value of the custom field. At the moment it is checking if the subject = the field. I need to check if it contains rather than =.

Custom object = Request
Custom field to check against email subject = Test

Here is the Apex class
global class CreateTaskEmailExample1 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 any Request based on the email subject
    
    try {
      Request__c vCon = [SELECT Id, Name, Number__c, Test__c
        FROM Request__c 
        WHERE Test__c =:email.subject
        LIMIT 1];
      
      // Add a new Task to the Request 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 =  vCon.Id));
     
     // Insert the new Task 
     insert newTask;    
     
     System.debug('New Task Object: ' + newTask );   
    }
    // If an exception occurs when the query accesses 
    // the Request 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;
  }
}



 
Shashikant SharmaShashikant Sharma
You need to use Like instead of = in your SOQL
 
Request__c vCon = [SELECT Id, Name, Number__c, Test__c
        FROM Request__c
        WHERE Test__c Like '%' + email.subject + '%'
        LIMIT 1];

Read this to know more about Like operator : http://www.salesforcegeneral.com/introduction-to-soql/
Ellsa JamesEllsa James
Thanks for the help Shashikant but I am getting a snytax error.

Error: Compile Error: unexpected token: '+' at line 28 column 37

It doesn't like the +
Ellsa JamesEllsa James
Corection - The error is for line 23
Frédéric TrébuchetFrédéric Trébuchet
Hi,

In such a case, you have to construct your query before to use it using dynamic SOQL.
Here is the idea:
// declare a list for the corresponding requests
List<Request__c> requests;
// build the SELECT string
soql = 'SELECT Id, Name, Number__c, Test__c FROM Request__c WHERE Test__c
           LIKE \''+String.escapeSingleQuotes(email.subject)+'%\'';
// execute the query and return the requests
requests = Database.query(soql);

Here is a link to a complete example http://blog.jeffdouglas.com/2010/07/13/building-a-dynamic-search-page-in-visualforce/

Hope this helps and happy new year!
Fred
Frédéric TrébuchetFrédéric Trébuchet
Hi,

If these answers helped you solve your problem, please, mark the question as Solved and kindly select the best one ;)

Thanks,
Fred