How To: Integrate Salesforce with .NET application

As advertised, Salesforce (Force.com or SFDC) is Number one on demand CRM and is currently a very hot technology in IT industry. Through SOAP APIs, Salesforce tightly integrates with various back-end technologies like .NET & JAVA, which lets you, access and manipulates your data and functionality in the Force.com cloud. Behind the scenes, SFDC follow MVC design pattern with Objects, Fields, Relationships comes under Model Layer; Visual Force pages, Page Layouts, Tabs comes under View Layer and Workflows, Apex Classes, Triggers comes under Controller of Model View Controller, Apart from this, just like SQL, offers Salesforce’s official Object Query Language (SOQL) that lets you access anything about any record in your Salesforce database.

We can leverage Force.com SOAP API to integrate the Force.com platform with Microsoft .NET application with ease with below steps:

Step 1: Generate the WSDL

Salesforce automatically generates & maintains the WSDLs. Whatever objects we are creating or already there in Salesforce, it will be automatically available to partner/enterprise WSDL. You need to login to Salesforce & download the appropriate WSDL from navigation path: Setup >> Develop >> API >> Click on ‘Generate’

salesforce-generate-wsdl

Step 2: Generate Security Token for API access

In order to access the Salesforce API, we need to provide our login email, password, and the security token. A security token is an automatically-generated key from Salesforce. We can change the Security token via the Salesforce user interface. Changing password automatically sends a new security token to the email address on the user’s Salesforce record or user can do the same using Salesforce interface resetting of security password.

To get the token we need to go to Personal Settings section of the Salesforce platform. Click on the Username on right top corner of the page >> My Settings >> Personal >> Reset My Security Token.

salesforce-reset-token

Step 3: Referencing WSDL from .NET application

Add the reference to WSDL file (downloaded in Step#1) to your project in Visual Studio through “Add Service Reference“.

Right click on project and choose ‘Add Service Reference’ option from menu.

salesforce-wsdl-add-reference

salesforce-wsdl-add-reference-update

You can encounter an error with .Net integration for ListViewRecord and ListViewRecordColumn from Salesforce API.

To fix this issue we need to go to the dynamically generated file ‘Reference.cs’ file while adding our Web reference to the enterprise WSDL. Open the file and replace all ‘ListViewRecordColumn[][]’ strings to only ‘ListViewRecordColumn[]’. ExternalLinkIconBlue

Step 4: Authenticate and Create Salesforce API Session in .Net

You can now use objects of classes “SforceService” and “LoginResult” to connect to Salesforce API.

Initialize credantial information containing Username, Password & Security Token.

Authenticate & Create connection to Salesforce API.

Step 5: Perform CRUD operations

Once you are able to connect with Salesforce API, you can use three kinds of array-based objects to perfrom all CRUD operations:

  1. SaveResult – is returned with the insert and update database methods.ExternalLinkIconBlue
  2. DeleteResult – is returned with the delete database method.ExternalLinkIconBlue
  3. QueryResult – will hold array of records as results on successful execution of the query string. ExternalLinkIconBlue

Rest is quite straightforward, you just have to right queries in SOQL for SELECT, INSERT, UPDATE & DELETE and play around these Result objects accordingly.

Enjoy integrating applications on Salesforce