0

I have created a InboundEmail service class for creating a contact. By using this class i can able to get only last name of contact and inserting record successfully with last name.

Class which i created:

global class createContact implements Messaging.InboundEmailHandler {

    global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {

        Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();

        List<contact> memb = new List<contact>();

        try {
            contact mem = new contact();
            mem.Lastname = email.PlainTextBody;
            memb.add(mem);
            insert memb;
            System.debug('New member: ' + memb);
        }
        catch (Exception e) {
            System.debug('Error is: ' + e);
        }
        result.success = true;
        return result;
    }
}

But i want to get more fields like first name, email, phone and some more.

To get these fields i have to sent an email with body contains like last name, first name, Email and phone like the following image shows.

enter image description here

For Example:

If i send an email to salesforce which contains body like

Firstname: starone
Lastname : startwo
Phone: 123456789
Email : t.ravi@gmail.com
Description : salesforce

then a contact has to create with first name is starone, last name is startwo, email is t.ravi@gmail.com and phone is 123456789 and Description is salesforce

How do i get these fileds in InboundEmail apex class(createContact)?

Thanks in advance.

1

Here's one way I see to do it:

  • First, split your email body by lines using: String[] bodySplitted = email.PlainTextBody.split('\n');
  • After that you will have an array filled with each line of your email body
  • Then, if we assume that you will always have the values in the same order and this separator between your labels and your values: ': ' like Firstname: starone
  • for each line of your new array, you will have to substring the right side after the ': ' separator like this: String firstName = bodySplitted[0].substringAfter(': ');
    • Then instanciate your contact fields with these variables like this: contact.FirstName = firstName;
  • Finally, do this for all of your fields by changing the index of the array bodySplitted

Please note that this solution is a bit tricky. It will not work if there is no line breaks between the fields of your email, if there is no ': ' separator between the labels and the values and if the order of the fields is changed.

  • could you please check this? String[] bodySplitted = email.PlainTextBody.split('\n');String firstName = bodySplitted[0].substringAfter(': '); String LastName = bodySplitted[1].substringAfter(': '); String conEmail = bodySplitted[2].substringAfter(': '); String phone = bodySplitted[3].substringAfter(': '); String Description = bodySplitted[4].substringAfter(': '); mem.FirstName = firstName; mem.LastName = LastName; mem.email = conEmail; mem.phone = phone; mem.Description = Description; – user37550 Jan 23 '17 at 13:37
  • This should do the trick. If the String[] bodySplitted = email.PlainTextBody.split('\n'); line doesn't work. You should try: String[] bodySplitted = email.PlainTextBody.split('\r\n'); Sometimes line breaks use these characters. – Martin Lezer Jan 23 '17 at 13:41
  • I am unable to get this even i tried with both \n and \r\n. could please provide a piece of code? – user37550 Jan 23 '17 at 13:56
  • @MartinLezer can we change into plain text body as Htmlbody of Email, if we change like that we can split easily That way is correct – SFDC Jan 23 '17 at 13:58
  • 1
    @user36188 Yes you're right we could it with html too. But I think it will be also laborious. – Martin Lezer Jan 23 '17 at 14:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy