Tuesday, June 26, 2012

.Net Web Service with Salesforce.com

Part 1 – Update Salesforce Recod using .Net Application
  • Step 1:
    • Create a field TestAccount [Text] in Account.
    • Step 2:
      • Create a .Net application that extracts salesforce's objects records (In this case, Accounts) and update TestAccount field with its Parent Account field.
      • You have to add Web Reference of your enterprise WSDL in this .Net Application.
      • To generate WSDL:  Setup | Develop | API| Generate Enterprise WSDL | Generate.
      • Right Click on it and Save Enterprise WSDL and Import into you .net Application using Webrefernce.
        • If you don't know how to add Webrefernce. (Click here)
  • Now Refer to "Walk Through the Sample Code" in Web Services API Developer's Guide :http://www.salesforce.com/us/developer/docs/api/apex_api.pdf
  • This Walkthorugh Application is a very simple app you can easily query data from the querySample() method. So Query that record by passing a hardcoded record Id e.g. :
    • QueryResult qr = null;
    • string recordId = "01pP00000004kUC"; //SampleID
    • qr = binding.query("SELECT Id, Name, ParentId FROM Account where Id = '" + recordId + "'");
  • In The 2nd part of this App we will update this recordId which will get Id from Outbound Message.
  • To update record we need to use SaveResult and sObject to Update. Here is a sample code to update it. This will Update TestAccount field and populate the Value of ParentId into it.
    • for (int i = 0; i < qr.records.Length; i++)
    • {
    •    Account acc = (Account)qr.records[i];
    •    acc.TestAccount__c = acc.ParentId;
    •    SaveResult[] saveResults = binding.update(new sObject[] { acc }); // updating results in salesforce.
    • }
  • Now Run and Test it whether it is Updating that record or not.
  • How to Import WSDL into .net APP
    • In Solution Explorer Right Click on Application name and click on Add Service Reference.
    • Now Click Advanced a new window will pop up and Now Click on Add Web Reference.
    • Again a window will pop up here you need to specify the path of you Enterprise WSDL e.g. : C:\Users\Administrator\Desktop\enterprise.wsdl
    • Now Click on Add reference and that's it your webrefernce is added into you App.
    • In order to use it you have to add a name space e.g. :  using ApplicationName.WebserviceName;
Part 2 – Outbound Message to invoke .Net Application
Step 3:
Create a Web Service that calls your .Net Application.
Add reference of your .Net application to the Web Service.
Sample Code:
Below is the code of web service file .asmx that implements class WebService in namespace testWebservice
<%@ WebService Language="C#" CodeBehind="~/App_Code/WebService.cs" %>
Below is the code of a Class Webservice
using System;  using System.Collections.Generic;  using System.Web;  using System.Web.Services;  using ConsoleApplication5;    namespace testWebservice  {      [WebService(Namespace = "http://theinsidecloud.org/")]      [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]      public class WebService : System.Web.Services.WebService      {          public WebService()          {              //Uncomment the following line if using designed components              //InitializeComponent();          }          [WebMethod]          public void HelloWorld(String id)          {              Program app = new Program();              app.Accountid = id;              app.run();          }      }  }

Step 4:
  • Now host this Web Service.
Step 5:
  • Create a workflow to send an outbound message that will send Account Id. Specify end point URL where you have hosted your web service.
  • Select a field to be sent as a parameter for the Web Service.
Step 6: 
  • After saving the out bound message you will have its detail page. Click on Click for WSDL.
  • Add web reference of this WSDL in your Web Service.
  • After Adding WSDL to your WebService , Host the updated application to the Web.
So whenever your Workflow fires, it will hit the hosted Web Service with some parameter (in this case Id). Web Service will call .Net application and update a particular record that matches with the parameter.

1 comment: