Friday, May 28, 2010

Salesforce Web-to-Lead (W2L) implementation

A web-to-Lead forms is an essential component of marketing and sales automation. Its purpose is to capture data submitted by website visitors, such as contact information and product interest, and store it as a “Lead” record in a CRM product, in this case Salesforce.com.

Here in this article I will more talk about coding part of web to lead and not on how to setup your account in salesforce. This help you can get on salesforce site only.

Over here I am assuming that, you or your company already has your personal organization ID and you have also done setup in salesforce for web to lead. Setup will include
creating lead template form too. Once you are having your lead form ready you can use that generated html to post contacts to Salesforce CRM.

But over here the concern is how you can do that programmatically. Let’s check this out step by step.

1) You will be having the salesforce auto generated lead form template, copy html part of it and paste it in your applications lead.aspx page.

2) Style it well and remove org. id and return URL from the html and keep it to config file. Also keep method post of from but remove the action URL and kept that salesforce URL to config too.

3) Add validation as per need.

4) Now once you click submit button your lead.aspx.cs will get executed and in that have a method for form post. i.e.
private void FormPost(NameValueCollection collection)
{
NameValueCollection form = new NameValueCollection();
form.Add("oid", appConfig["OID"]);
for (int count = 0; count < collection.Count; count++)
{
if (collection.Keys[count].ToLower() == "returl")
{
// Update "Return URL" dynamically with 'http://' & servername
form.Add(collection.Keys[count], Request.Url.ToString().Substring(0, Request.Url.ToString().IndexOf(Request.Url.PathAndQuery)) + collection["retURL"]);
}
else
{
form.Add(collection.Keys[count], collection[count]);
}

}
WebClient wc = new WebClient();
// Posts form collection to Salesforce and get the response
byte[] res = wc.UploadValues(appConfig["SFURL"], "POST", form);
// Convert response from byte array to string
string result = System.Text.ASCIIEncoding.UTF8.GetString(res);
Response.Write(result);
}

Call this method on page load and pass Request.Form as param. That’s all you are done. Using this code, if you want to load different type of leads form dynamically then also you can do that, as the method post the form dynamically no hard coded ids are there.

No comments:

Post a Comment