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
Luca Benedettini 7Luca Benedettini 7 

Apex Class for email service

Hi,
I wrote an apex class for one Salesforce email service. I need that when an email arrive, case is created and email's attachemnts attach to the case created. When I test it, doesn't trhow errors or expetions, but also doesn't create case and attachment. Someone could please check my code and give me some tips please?

Here is my code
global class casewithattachments implements Messaging.InboundEmailHandler {
    
global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email, Messaging.InboundEnvelope env){ 
// Create an inboundEmailResult object for returning the result of the Force.com Email Service 
Messaging.InboundEmailResult result = new Messaging.InboundEmailResult(); 
//List of email's Attachments
List <Attachment> attch = new List<Attachment>(); 
// Initialize the Contact ID, so if no Contact is found, a case is still created
Id vconID;     
//Case objects to be created 
Case newCase = new Case();   
// Try to lookup any contacts based on the email from address 
// If there is more than 1 contact with the same email address 
// an exception will be thrown and the catch statement will be called 
try { 
    Contact vCon = [Select Id, Name, Email From Contact Where Email = :email.fromAddress Limit 1]; 
// Add a new Case to the contact record we just found above - if not Contact found, value remains null 
    vconID = vCon.ID; 
// Insert the new Case and it will be created and appended to the contact record 
//newCase.Description = myPlainText;  
    newCase.Description = email.htmlBody; 
    newCase.Priority = 'Medium';
    newCase.Status = 'New';
    newCase.Subject = email.subject;
    newCase.Origin = 'Email'; 
    newCase.ContactId = vconID;
    insert newCase;
    System.debug('New Case Object: ' + newCase );     

// If there is an exception with the query looking up and the exception will be written to the Apex Debug logs 
catch (System.QueryException e) { 
    System.debug('Query Issue: ' + e);


    try{     
        for (Messaging.Inboundemail.TextAttachment tAttachment : email.textAttachments) {
        attch.add(new Attachment(Name = tAttachment.fileName,Body = Blob.valueOf(tAttachment.body),ParentId = newCase.Id));} 

    
        for (Messaging.Inboundemail.BinaryAttachment bAttachment : email.binaryAttachments) {
          attch.add(new Attachment(Name = bAttachment.fileName,Body = bAttachment.body,ParentId = newCase.Id));  } 
        System.debug('Attachemnt attached' );
    }
    catch (System.NullPointerException e) { 
            System.debug('Attachment Issue: ' + e);
}    
    
insert attch;
// 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 Force.com Email Service 
return result; } 
}
Best Answer chosen by Luca Benedettini 7
Jagjeet Singh 13Jagjeet Singh 13
Hi Luca Benedettini 7,
 Your code is absolutely fine. I guess you might not have set up the Email Service and the Email address. Please go through https://developer.salesforce.com/page/An_Introduction_To_Email_Services_on_Force.com documentation and set up the Email Service.
Also make sure that there must be a single contact in your org with the same email address otherwise the code segment:
Contact vCon = [Select Id, Name, Email From Contact Where Email = :email.fromAddress Limit 1];
 will give error. Let me know if you still face any problems.
Thanks

 

All Answers

Jagjeet Singh 13Jagjeet Singh 13
Hi Luca Benedettini 7,
 Your code is absolutely fine. I guess you might not have set up the Email Service and the Email address. Please go through https://developer.salesforce.com/page/An_Introduction_To_Email_Services_on_Force.com documentation and set up the Email Service.
Also make sure that there must be a single contact in your org with the same email address otherwise the code segment:
Contact vCon = [Select Id, Name, Email From Contact Where Email = :email.fromAddress Limit 1];
 will give error. Let me know if you still face any problems.
Thanks

 
This was selected as the best answer
Luca Benedettini 7Luca Benedettini 7
Thank you so much Singh, I think the email service is good, look the image and tell me what you think. Maybe I do one mistake but I can't see it.User-added image
P.S. In my org in this moment there aren't Contacts , that may be the problem?
Jagjeet Singh 13Jagjeet Singh 13
Email service setup looks good. I am assuming that you are sending the email at the salesforce generating address displayed in the bottom of the image. Please check in your logs, you must be getting an error "list has no rows for assignment". If that's the case please create a contact and the email should be same as sender's email.
Luca Benedettini 7Luca Benedettini 7
Thank you very very much for all the tips you gave me. I create the Contact and it works, but the Attachment wasn't attached to the case. 
Jagjeet Singh 13Jagjeet Singh 13
Debug a lil more and you should be through with attachments as well. Please mark the best answer. Thanks!