+ Start a Discussion
thomasarnold81thomasarnold81 

Case to EMail Issue

Hi Guys,

 

I am trying to figure if it's possible to strip/parse an email body and take one of the lines to then use to attach a case to an account?

 

At the moment I have castoemail working just fine with lots of lines (strings) going into simple text fields. But it doesn't seem to want to take.....

 

Account: Test County Council

 

... form the body of the email and insert that into the Account Name on the case most likely because the field is a look up. Can I convert this somehow in APEX so that the lookup will take the data from the email body?

 

Or do I need to write a trigger so that I pull in the string into a simple Account Name text field and copy that over to the look up on insert?

 

Any help would be appreciated.

 

Currently I can see that you may be able to take the email address and use that to attach the case to a contact so clearly there are ways to attach cases to other objects when using emailtocase

 

Thanks in advance.

mtbclimbermtbclimber

You should be able to set the AccountId on the case by parsing the account name and doing a query to get the respective ID.

 

I'm assuming your entry point is an email service and you are invoking insert on case in some apex that is initiated from receipt of an email.

thomasarnold81thomasarnold81

Thanks so much for the reply.

 

Heres my APEX code;

 

Any idea how or where I would add what you are suggesting? Are there any examples of what that query would look like? I am assuming that I can amend "vcon contact" element below bit I don't understand what the vcon part is. 

 

global class email_to_cases 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();

//String myPlainText = '';

String[] emailBody = email.plainTextBody.split('\n', 0);
String Phone_String = emailBody[0].substring(6);
String Firstname_String = emailBody[1].substring(10);
String Surname_String = emailBody[2].substring(8);
String Latitude_String = emailBody[3].substring(9);
String Longitude_String = emailBody[4].substring(10);
String App_ID_String = emailBody[5].substring(7);
String Phone_ID_String = emailBody[6].substring(9);
String Problem_String = emailBody[7].substring(8);
String Notes1_String = emailBody[8].substring(6);
// + emailBody[9].substring(0) + emailBody[10].substring(0);
//String[] addr = new String[]{'a@b.com','c@d.com','e@f.com'};
//String Notes2_String = emailBody[8].substring(0);
// Initialize the Contact ID, so if no Contact is found, a case is still created
ID vconID;

// Add the email plain text into the local variable



// new Case object to be created

Case[] newCase = new Case[0];

    // 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 Name
       From Contact  
       Where Name = :email.subject
       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
    
System.debug('New Case Object: ' + newCase );
}
   // If there is an exception with the query looking up
   // the contact this QueryException will be called.
   // and the exception will be written to the Apex Debug logs

   
   catch (System.QueryException e) {
   System.debug('Query Issue: ' + e);
}

 newCase.add(new Case(
//     Description = email.plainTextBody,
//     Status = 'New',
     Subject = Problem_String,
     SuppliedEmail = email.fromAddress,
     SuppliedPhone = Phone_String,
     Case_Firstname__c = Firstname_String,
     Case_Surname__c = Surname_String,
     Longitude__c = Longitude_String,
     Latitude__c = Latitude_String,
     App_ID__C = App_ID_String,
     Phone_ID__C = Phone_String,
     Description = Notes1_String));



// Normal from here on in        
          
 insert newCase;
 
 //Attachments
if (email.binaryAttachments!=null && email.binaryAttachments.size() > 0) {
            for (integer i = 0 ; i < email.binaryAttachments.size() ; i++) {

System.debug('Binary Attachments - filename: ' + email.binaryAttachments[i].filename);
System.debug('Binary Attachments - size: ' +   email.binaryAttachments[i].mimeTypeSubType);
            
            Attachment a = new Attachment(ParentId = newCase[0].Id, 
                                          Name = email.binaryAttachments[i].filename, 
                                          Body = email.binaryAttachments[i].body);
            insert a;
            }
    }

// 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;
}


}

mtbclimbermtbclimber

Yes. You need to get the account name into a variable like you've done with the other items, i.e. Phone_String, etc and then use that variable in an account query similar to what is going on with the query to contact in your sample.  You'd need to do this before you instantiate the newcase sobject recod (which is the point you need the account id).

 

Then, after you figure out exactly which one of the accounts is the one you want assign its ID to the accountId standard field on case where the newCase is instantiated though I don't know why but it doesn't appear you are adding the contact IDto the case. But there are other issues with your code too like the attachment insert statement that sits within a for loop.

 

You may also want to have logic in there for handling the case where no accounts come back, i.e. it's a new account (so you might want to create it on the fly).  Also, you'll get an error if there are multiple contacts with the same name in your org, probably don't want to do the same with account.

 

Hope that helps.

 

thomasarnold81thomasarnold81

Ace Thanks I'll see what I can do.

 

Cheers for coming back so quickly.