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
Krish NKrish N 

Unable to create cases through Apex Email services

Hello all, 
I'm trying to create cases through incoming email by using Apex Inbound Emailhandler. It worked very well for a sample class which pulls the contact id based on incoming email address and creates case. However, when I try to create case based on our requirement, it wouldn't create. Please have a look at the below code. We receive emails in a particular template, we have to parse the emailbody to assign certain information to certain fields. We don't want the email body to be dumped in description field. Parsing is working fine, as I tested for the sample class. However, I'm unable to pull the location id (object of a managed package) for the incoming email. Can any one help me? There seems to be an issue with the query I think.  

Every incoming email has a location name, we have to use it to pull the location id and create a case. 
SVMXC__Site__c is the API name for the custom object Location from which I have to pull the location name and create case. 
 
global Class EtoCase implements Messaging.InboundEmailHandler {

// Instantiate variables that we will need for handling this email
public String location;
public String notes;
public Integer locationIdx;
public Integer notesIdx;
public String locationFinal;
public String notesFinal;

global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope env){  
Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
          
if (email.fromaddress =='example@test.com') {

location = 'Location: ';   
notes = 'Notes ';      

    
locationIdx = email.plainTextBody.indexOf(location);
notesIdx = email.plainTextBody.indexOf(notes);

locationFinal = email.plainTextBody.substring(
                              locationIdx + location.length(), email.plainTextBody.indexOf('\n', locationIdx + location.length()));
          String[] locNme = locationFinal.split(' ', 3);
notesFinal = email.plainTextBody.substring(
                              notesIdx + notes.length(), email.plainTextBody.indexOf('\n', notesIdx + notes.length()));
         
    
     try{      
               
case c= new case();
c.subject= 'PRODUCT SERVICE REQUEST';
c.Case_Type__c= 'Service Request';
c.Priority='High';
c.Origin='Email';
c.Status='new';
string location = locationFinal;   
SVMXC__Site__c[] locArray = [SELECT Id, Name, NTT_Location_Name__c, SVMXC__Account__r.Id FROM SVMXC__Site__c where Name = 'locationFinal'  limit 1];
c.SVMXC__Site__r.Id = locArray[0].Id;           
c.AccountId = locArray[0].SVMXC__Account__r.Id;
c.recordtypeId = '012E0000000oRWX';         
c.Description= notesFinal;
 insert c;
     } catch (System.Dmlexception e)
                     {System.debug('Error: Not able to create new Case: ' + e);
        

            // If no email address is gathered from the email body, write to the debug log, and no actions
            // will be performed.
}
}
return result;

      } 
}

 
Best Answer chosen by Krish N
Alex SelwynAlex Selwyn
I believe its the binding, right now the SOQL is query for a site name 'locationFinal'. bind the value in soql 'Name =: locationFinal'

SVMXC__Site__c[] locArray = [SELECT Id, Name, NTT_Location_Name__c, SVMXC__Account__r.Id FROM SVMXC__Site__c where Name = 'locationFinal'  limit 1];

SVMXC__Site__c[] locArray = [SELECT Id, Name, NTT_Location_Name__c, SVMXC__Account__r.Id FROM SVMXC__Site__c where Name =: locationFinal limit 1];

 

All Answers

Krish NKrish N
Any help guys?
Alex SelwynAlex Selwyn

1) To check if there is any exception, Setup debug log for the 'Context User'. Every email service address should have a context user. The context user should also get error email if there is exception (Except DMLException, since thats handled).

2) Try to execute the query in console to see data exists.
Krish NKrish N
Hello Alex, I've checked the debug logs, the executed soql didn't yield any data. 15:24:16:467 SOQL_EXECUTE_END [39]|Rows:0
I've executed the query in dev console and it's working fine.  
Alex SelwynAlex Selwyn
I believe its the binding, right now the SOQL is query for a site name 'locationFinal'. bind the value in soql 'Name =: locationFinal'

SVMXC__Site__c[] locArray = [SELECT Id, Name, NTT_Location_Name__c, SVMXC__Account__r.Id FROM SVMXC__Site__c where Name = 'locationFinal'  limit 1];

SVMXC__Site__c[] locArray = [SELECT Id, Name, NTT_Location_Name__c, SVMXC__Account__r.Id FROM SVMXC__Site__c where Name =: locationFinal limit 1];

 
This was selected as the best answer
Krish NKrish N
Yes Alex. I figured it out after I posted the question. I rectified the error but it still wouldn't create cases. 
Alex SelwynAlex Selwyn
can you post the debug log?
Krish NKrish N
Hey Alex, Sorry for the delayed reply. My issue was resolved though. Previously, I had a debug log error of "List out of bounds". I got rid of the limit 1 from the query and it's working fine with no errors.