A Simple Email Integration Between Gravity Forms on WordPress and Salesforce

Gravity Forms is a well know form builder that runs on WordPress.  It is simple to spin up a form to collect data for whatever you need.  But what if that data needs to get to Salesforce?

You could try to interact with the Salesforce API.  Salesforce has great APIs for interacting with its data, but doing that on the WordPress site can be pretty technical.  Let’s try to keep this simple.

Let’s just have Gravity Forms send an email to Salesforce with the results of what was entered on the form.  This is how Gravity Forms works already for most people.  The efficiency problem though is that the usually email ends up in somebody’s inbox and then the data needs to be copy/pasted into another system.

But Salesforce has a way to process this email for you.  It is called an Email Service.  Here is how this can be done.

1) Create an Apex Class that implements Messaging.InboundEmailHandler
Here is an example piece of code that will accept a Gravity Form email and then parse it into a list that will contain the questions and answers.  It would need to be extended for your specific questions and then what action to take.  Maybe you want to convert a lead or create an opportunity.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
global class inboundSalesEmail implements Messaging.InboundEmailHandler {
    //Email for this service: <span data-eeencemail_edrftoishi="1"><a href="mailto:NameOfEmailService@longlistofCharacters.otherChars.na1.apex.salesforce.com">NameOfEmailService@longlistofCharacters.otherChars.na1.apex.salesforce.com</a></span><script type="text/javascript">/*<![CDATA[*/var out ='',el = document.getElementsByTagName('span'),l = ['>','a','/','<',' 109',' 111',' 99',' 46',' 101',' 99',' 114',' 111',' 102',' 115',' 101',' 108',' 97',' 115',' 46',' 120',' 101',' 112',' 97',' 46',' 49',' 97',' 110',' 46',' 115',' 114',' 97',' 104',' 67',' 114',' 101',' 104',' 116',' 111',' 46',' 115',' 114',' 101',' 116',' 99',' 97',' 114',' 97',' 104',' 67',' 102',' 111',' 116',' 115',' 105',' 108',' 103',' 110',' 111',' 108',' 64',' 101',' 99',' 105',' 118',' 114',' 101',' 83',' 108',' 105',' 97',' 109',' 69',' 102',' 79',' 101',' 109',' 97',' 78','>','\"',' 109',' 111',' 99',' 46',' 101',' 99',' 114',' 111',' 102',' 115',' 101',' 108',' 97',' 115',' 46',' 120',' 101',' 112',' 97',' 46',' 49',' 97',' 110',' 46',' 115',' 114',' 97',' 104',' 67',' 114',' 101',' 104',' 116',' 111',' 46',' 115',' 114',' 101',' 116',' 99',' 97',' 114',' 97',' 104',' 67',' 102',' 111',' 116',' 115',' 105',' 108',' 103',' 110',' 111',' 108',' 64',' 101',' 99',' 105',' 118',' 114',' 101',' 83',' 108',' 105',' 97',' 109',' 69',' 102',' 79',' 101',' 109',' 97',' 78',':','o','t','l','i','a','m','\"','=','f','e','r','h','a ','<'],i = l.length,j = el.length;while (--i >= 0){out += unescape(l[i].replace(/^\s\s*/, '&#'));}while (--j >= 0){/**/if (el[j].getAttribute('data-eeEncEmail_EdrFtOIshi')){el[j].innerHTML = out;}}/*]]>*/</script>
    global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email, Messaging.InboundEnvelope env){   
        Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
        
        system.debug('Subject: ' + email.Subject);
        //system.debug('PlainText Body: ' + email.plainTextBody);
        //system.debug('HTML Body: ' + email.htmlBody);
        
        //The email comes in broken by line feed
        list<string> splitBody = new list<string>();
        splitBody = email.plainTextBody.split('\n');
        //Now that we have a list of the lines spin through them
        // and remove the blank rows
        list<string> splitItems = new list<string>();
        for(string s : splitBody){
            if(string.isBlank(s) == false){
                splitItems.add(s);
            }
        }
        
        //Here is where you would do your logic. 
        //Both the questions and the answers from the Gravity Form
        // are in this list of strings.
        for(string s : splitItems){
            system.debug('Incoming Item: ' + s);
        }
        
        result.success = true;
        return result;
    }
        
}
</string></string></string></string>

This code is making use of the plainTextBody of the email.  You could get the htmlBody too from the email parameter for more detailed parsing, but that is going to contain all of the table tags like

and

and all of the formatting.  I really just wanted to get right at the data for this example.

2) Create an Email Service
a) Go to Setup, Build, Develop, Email Service
b) Click on New Email Service
c) Fill out the form and specify the Apex class that you created above
d) After the Email Service is saved click on the ‘New Email Address’ button to generate an email for this Email Service.
e) This last step is the piece that ties is all together.  It is saying that when Salesforce receives this email, that it will route the email to the Apex class that was created above.

That is really all there is to it!  Your Gravity Form and Salesforce are now linked!

You will have to be careful about how Gravity Forms sends over tables and other more complicated structures.  As you go through the loop you may need to build a set of questions so you know where they start and stop.
I wrote a blog post last week about creating a task with an email service too if you want to look into how to write a test class for your email service.

An email integration is just as powerful as a REST/SOAP integration.  It gets the data where it needs to go and it stops the dreaded time-killer of copy/paste work.

Let me know if Sundog can help you with your next integration!

This entry was posted in Technology and tagged , , , . Bookmark the permalink.

One Response to A Simple Email Integration Between Gravity Forms on WordPress and Salesforce

  1. david says:

    Hi I’ve been struggling for the last few days to understand how to get the content of a gravity form into Salesforce using the Salesforce plugin and web-to-lead but so far without any success, so this looks like an interesting alternative method.
    So when you say:
    Here is an example piece of code that will accept a Gravity Form email
    I’m not an experienced coder and would appreciate a more detailed explanation of the code, or a pointer to a real live example/tutorial – in particular I don’t see where the form field names.
    Thanks for any help
    David
    “paddling upstream searching for the source”

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>