Creating Leads or Cases in Salesforce from Sitecore

Its a very common use case where we have to create leads or cases in SFDC (or any other CRM) whenever a customer submits a form in our website. The form can either be a simple MVC form, or a WFFM form, or a Sitecore 9 form. In case of WFFM or Sitecore 9 forms, we just need to create our custom Submit action or Save action and write our mapping logic there.
In this post, I will walk through the easiest way to do it which can be completed in under 1 hour.

Before we start, lets understand what a lead or case is. If you already know this, skip this section.
A lead is a marketing object in SFDC. Whenever a customer submits a form or interacts with our website, our marketing team want to keep a record of that customer for further processing. In technical terms it looks like a record in a table in SFDC with various details.

A case is similar to a lead in technical terms. It also looks like a record in a table in SFDC. Only difference is functional. Unlike a lead which is used for marketing, a case is used for further processing. For example when a customer requests change in address, registering your product, warranty etc. Their entry is stored as a case in salesforce and assigned to respective people for further processing.

There are three ways to do it.

  1. Using one of the many connectors provided by Sitecore or third party vendors.
    This is most cumbersome method, and I’ll suggest it only if there is an extensive connection between your Sitecore and SFDC instances. That is when your forms fields are highly Dynamic/changing very often. This brings a lot of technical overhead in your Sitecore solution. If this is not the case, explore below methods.

  2. Using SFDC enterprise WSDL.
    Salesforce provides WSDL for many simple tasks such as creating leads, cases, account management etc. All you need is an admin access to SFDC, so you can download “SFDC Enterprise WSDL” from that instance.
    Remember, this WSDL is unique for every SFDC instance, so you will likely have different WSDLs for every environment.
    – Next you have to import this WSDL as web service in your solution.
    – This WSDL has many methods in it, you just need the one to create leads or cases.
    – Rest is all .Net code. Just fetch values from you front end form fields and post data from your code to those methods.
  3. Using web to lead or web to case forms
    This is the easiest and fastest option. And if your form fields don’t change extremely often, this is the recommended one.
    – A web-to-lead form is a simple HTML markup which can be downloaded from SFDC instance. You will get this from SFDC team. Here is how to do it.
    – This is also unique for every salesforce instance. If you look at the markup, it looks like below. It has a form tag, a submit url and various field identified with their IDs.
<!----------------------------------------------------------------  -->
<!--NOTE: Please add the following <META> element to your page <HEAD>. -->
<!--If necessary, please modify the charset parameter to specify the-->

<!--------------------------------------------------------------  -->

<META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=UTF-8">
<!--------------------------------------------------------------  -->
<!-NOTE: Please add the following <FORM> element to your page-->
<!---------------------------------------------------------------  -->
<form action="https://webto.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8" method="POST">
<input type=hidden name="oid" value="00D1t000000sTJD">
<input type=hidden name="retURL" value="http://www.mysitecoresite.com/form-sucess">
<!------------------------------------------------------------------>
<!--NOTE:These fields are optional debugging elements. Please uncomment-->
<!--  these lines if you wish to test in debug mode.-->
<!--  <input type="hidden" name="debug" value=1>-->
<!--  <input type="hidden" name="debugEmail"-->
<!--  value="shukla.shashwat@hotmail.com">-->
<!--------------------------------------------------------------->
<label for="first_name">First Name</label><input  id="first_name" maxlength="40" name="first_name" size="20" type="text" /><br>
<label for="last_name">Last Name</label><input  id="last_name"  name="last_name" size="20" type="text" /><br>
<label for="email">Email</label><input  id="email" name="email" size="20" type="text" /><br>
<label for="company">Company</label><input  id="company"  name="company" size="20" type="text" /><br>
<label for="city">City</label><input  id="city" name="city" size="20" type="text" /><br>
<label for="state">State/Province</label><input  id="state" maxlength="20" name="state" size="20" type="text" /><br> 
<input type="submit" name="submit">
</form>

Next we just need to map these fields from our forms and post to the url given in Form Action.

We used WFFM forms, so I created an Custom Submit action for this-

And in the code I just wrote mapping logic-

using Sitecore.Data;
using Sitecore.WFFM.Abstractions.Actions;
using System.Collections.Specialized;
using System.Net;
namespace Mysite.Foundation.CRM.SFDCConnector
{
		public class SalesforceRegisterSaveAction : ISaveAction
		{
				private string FieldName { get; set; }
				public ID ActionID { get; set; }
				public string UniqueKey { get; set; }
				public ActionType ActionType { get; }
				private string GetWFFMFieldValueByIndex(AdaptedResultList fields, int index)
				{
						string value = string.Empty;
						int counter = 1;
						foreach (AdaptedControlResult field in fields)
						{
								if (index == counter)
								{
										value = field.Value;
										break;
								}
								counter++;
						}
						return value;
				}

				public void Execute(ID formId, AdaptedResultList fields, ActionCallContext actionCallContext = null, params object[] data)
				{
						using (WebClient wb = new WebClient())
						{
								NameValueCollection formData = new NameValueCollection
								{
										{ "orgid", "00DU0000000######" },// internal SFDC field for form type
										{ "retURL", "https://www.mysitecoresite.com/form-success" },// internal SFDC field for form type
										{ "first_name", fields.GetEntryByName("Name").Value }, //Name
										{ "last_name", fields.GetEntryByName("Surname").Value }, //Surname
										{ "email", fields.GetEntryByName("E-mail Address").Value },
										{ "company", fields.GetEntryByName("Province").Value },
										{ "city", fields.GetEntryByName("City").Value }, //City
										{ "state", fields.GetEntryByName("Province").Value }, //Province
								};

								byte[] response = wb.UploadValues("https://webto.salesforce.com/servlet/servlet.WebToCase?encoding=UTF-8", "POST", formData);
						}
				}

				public ActionState QueryState(ActionQueryContext queryContext)
				{
						return ActionState.Enabled;
				}
		}
}

Just attach this submit action to your WFFM form. Or modify it to be used with Sitecore 9 form or simple MVC form.

Open your form page on website, fill values and submit. New leads or cases will be created in SFDC with all the mapped fields.

Lead in Salesforce

That’s all Folks !!!

Please feel free to mail me or comment in case you have doubts and need help.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create your website at WordPress.com
Get started
%d bloggers like this: