Using ColdFusion to create a Lead in SalesForce using SOAP
At my day job, Secure Computing, I needed to figure out how to create a new Lead in SalesForce from a partner deal registration web application. I already had experience using Web-to-lead (WTL), but this would not work for the type of Lead I was trying to create. In this case, the Lead had fields for a Contact and Account (both lookup fields), which WTL doesn't support.
I did some google searching for ColdFusion web services and SalesForce and found myself at the Adobe forums. After reading all the posts and grabbing some sample code, I started to figure it out. After a few days of coding and testing using SOAP web services, I had my solution. I posted my findings back to the Adobe forum to give back to the community, and my submission is found here on page 3 (posted by "tccfug"). The only drawback is I wasn't able to associate the Lead to a Campaign. However, it wasn't a show stopper and my solution is now in production.
Here's the code, hope it will help somebody else down the road... Make sure you change the first lines of code with your respective username and password to login to SalesForce. This is required to first begin a new session, then create the lead with the sessionid.
<cfset UserName = "myemail@mydomain.com">
<cfset Pwd = "mypassword">
<cfset loginurl = "https://www.salesforce.com/services/Soap/u/9.0">
<!---
LOGIN TO SALESFORCE TO START A NEW SESSION
--->
<cfoutput>
<cfsavecontent variable = "LoginSOAP"><?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SOAP-ENV:Header>
<m:LoginScopeHeader xmlns:m="urn:partner.soap.sforce.com"> <m:organizationId></m:organizationId>
</m:LoginScopeHeader>
<m:CallOptions xmlns:m="urn:partner.soap.sforce.com">
<m:client></m:client>
</m:CallOptions>
</SOAP-ENV:Header>
<SOAP-ENV:Body><!---Body of Soap--->
<m:login xmlns:m="urn:partner.soap.sforce.com">
<m:username>#UserName#</m:username><!---User Name for login--->
<m:password>#Pwd#</m:password><!---Password for Login--->
</m:login>
</SOAP-ENV:Body><!---end of the body--->
</SOAP-ENV:Envelope></cfsavecontent><!---End of the Login SOAP--->
</cfoutput>
<cfhttp url="#loginurl#" method="post"><!---Variables to be posted at this URL --->
<cfheader name="Content-Type" value="text/xml; charset=utf-8" />
<cfhttpparam type="xml" value="#LoginSOAP#" />
<cfhttpparam type="header" name="SOAPAction" value="login" /><!---method of the web service--->
</cfhttp>
<!--- the following parses out the ServerURL and the SessionID which will be used in the 2nd SOAP request to generate a Lead --->
<cfset serurl_start = Find("<serverUrl>",cfhttp.FileContent)>
<cfset serurl_end = Find("</serverUrl>",cfhttp.FileContent)>
<cfset serurl_final = Trim(Mid(cfhttp.FileContent,serurl_start+11,serurl_end-(serurl_start+11)))>
<cfset sesid_start = Find("<sessionId>",cfhttp.FileContent)>
<cfset sesid_end = Find("</sessionId>",cfhttp.FileContent)>
<cfset sesid_final = Trim(Mid(cfhttp.FileContent,sesid_start+11,sesid_end-(sesid_start+11)))>
<!---
LEAD SUBMISSION WHICH NEEDS TO USE SESSION FROM LOGIN ABOVE
--->
<cfoutput>
<cfsavecontent variable="LeadSoap"><?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soapenv:Header>
<ns1:SessionHeader soapenv:mustUnderstand="0" xmlns:ns1="urn:enterprise.soap.sforce.com">
<ns2:sessionId xmlns:ns2="urn:enterprise.soap.sforce.com">#sesid_final#</ns2:sessionId>
</ns1:SessionHeader>
</soapenv:Header>
<soapenv:Body>
<create xmlns="urn:enterprise.soap.sforce.com">
<sObjects xsi:type="ns3:Lead" xmlns:ns3="urn:sobject.enterprise.soap.sforce.com">
<ns3:RecordTypeId>01250000000DKxf</ns3:RecordTypeId> <!--- hardcoded for Prospects --->
<ns3:FirstName>Rich</ns3:FirstName>
<ns3:LastName>Dude</ns3:LastName>
<ns3:Title>CEO</ns3:Title>
<ns3:Email>rich_dude@prospect.com</ns3:Email>
<ns3:Company>TEST</ns3:Company>
<ns3:Street>123 Money Lane</ns3:Street>
<ns3:City>Las Vegas</ns3:City>
<ns3:State>NV</ns3:State>
<ns3:Country>US</ns3:Country>
<ns3:PostalCode>89109</ns3:PostalCode>
<ns3:Phone>888-PHONE</ns3:Phone>
<ns3:Fax>777-FAX</ns3:Fax>
<ns3:LeadSource>Other</ns3:LeadSource>
<ns3:OwnerId>00550000000wtO9</ns3:OwnerId><!--- CFID of contact --->
<!--- And any other fields
See http://www.sforce.com/resources/api.jsp for SOAP Message examples
See http://www.sforce.com for documentation on the fields you can send. --->
</sObjects>
</create>
</soapenv:Body>
</soapenv:Envelope></cfsavecontent>
</cfoutput>
<cfhttp url="#serurl_final#" method="POST">
<cfhttpparam type="HEADER" name="Content-Type" value="text/xml; charset=utf-8">
<cfhttpparam type="HEADER" name="Accept" value="application/soap+xml, application/dime, multipart/related, text/*">
<cfhttpparam type="HEADER" name="User-Agent" value="Axis/1.1">
<cfhttpparam type="HEADER" name="Host" value="na1.salesforce.com">
<cfhttpparam type="HEADER" name="Cache-Control" value="no-cache">
<cfhttpparam type="HEADER" name="Pragma" value="no-cache">
<cfhttpparam type="HEADER" name="Version" value="HTTP/1.0">
<cfhttpparam type="HEADER" name="SOAPAction" value="update">
<cfhttpparam type="HEADER" name="Content-Length" value="#len(trim(LeadSoap))#">
<cfhttpparam type="BODY" value="#trim(LeadSoap)#">
</cfhttp>
<cfset faultcode_start = Find("<faultcode>",cfhttp.FileContent)>
<cfset success_start = Find("<success>",cfhttp.FileContent)>
<cfcontent type="text/html" reset="Yes">
<cfif success_start>
<cfset leadid_start = Find("<id>",cfhttp.FileContent)>
<cfset leadid_end = Find("</id>",cfhttp.FileContent)>
<cfset leadid_final = Trim(Mid(cfhttp.FileContent,leadid_start+4,leadid_end-(leadid_start+4)))>
<cfoutput>
<b>Lead ID: <a href="https://na3.salesforce.com/#Left(leadid_final,15)#" target="_blank">#Left(leadid_final,15)#</a></b>
</cfoutput>
<cfelseif faultcode_start>
<cfset faultcode_end = Find("</faultcode>",cfhttp.FileContent)>
<cfset faultcode_final = Trim(Mid(cfhttp.FileContent,faultcode_start+11,faultcode_end-(faultcode_start+11)))>
<cfset faultstring_start = Find("<faultstring>",cfhttp.FileContent)>
<cfset faultstring_end = Find("</faultstring>",cfhttp.FileContent)>
<cfset faultstring_final = Trim(Mid(cfhttp.FileContent,faultstring_start+13,faultstring_end-(faultstring_start+13)))>
<cfoutput>
<b>Fault Code: #faultcode_final#</b><br>
<b>Fault String: #faultstring_final#</b>
</cfoutput>
<cfelse>
<b>Unknown response</b><br>
<cfoutput>#cfhttp.FileContent#</cfoutput>
</cfif>


There are no comments for this entry.
[Add Comment]