Hello there,

This use case comes from a question in the success community where the admin wants to send out emails to the contacts with an inline form that they can fill and submit within the email.

Assumptions:
I have only tested on gmail email client and it works flawlessly. Further testing and code modification will be required for other mail clients.

The Plan:
1. First, we will create an HTML email using Salesforce setup and embed our form in it
2. We will then create a REST webservice that will receive the form data
3. Finally we will add the remoteRequest class to our public Site

Here are the screenshots and code required. Its fairly straight forward.

1

Below is the full code to email template.
The important thing to note is that the form submission URL will be:
<public site URL> + services/apexrest/ + <URLmapping in our class>

Hi there,

Here my my form. Fill it up now! 

<!--unfortunately the ajax doesnt work on gmail client, so you can ignore the script here --></pre>
<form id="myFormId" action="https://ima-developer-edition.ap1.force.com//services/apexrest/form/v1/1" method="POST">
		<label for="first_name">First Name</label><input id="first_name" maxlength="40" name="first_name" size="20" type="text" />

		<label for="last_name">Last Name</label><input id="last_name" maxlength="80" name="last_name" size="20" type="text" />

		<label for="email">Email</label><input id="email" maxlength="80" name="email" size="20" type="text" />

		<input name="submit" type="submit" />
	</form>


Our RestResource class goes like this

@RestResource(urlMapping='/form/v1/*')
global with sharing class form1
{

    @HttpPost
    global static Void getFormData(){ //String first_name,String last_name, String email
        RestRequest req = RestContext.request;
        //RestResponse res = Restcontext.response;
        String first_name = req.params.get('first_name');
        String last_name = req.params.get('last_name');
        String email = req.params.get('email');       

   }
}

Finally, add to the site for public access:
Go to Setup->Sites->Open sites/Create new site->Public access Settings->Apex class access->Edit
Now add the class in the enabled apex classes list as below
My class is called “form1”:

2

All is done!
Now use the email template that we created and send emails to your contacts!

For a working demo on web, visit this link:

DEMO LINK !!

Thanks for reading.

Advertisements