Create a custom Web service in Salesforce and consume it in C#.Net application

In this tutorial, we will create the web service in salesforce and consume it in c#.Net application

In Previous article, we have consumed the standard enterprise wsdl of the salesforce. In this article we will create the webservice using apex in salesforce and consume it in C#.Net application.

In Salesforce create below class (using IDE)

global public with sharing class SaveExpenditureWebService {

	webservice static Expenditure__c createExpenditure(Decimal amount,String expName, String paidByName )
		Expenditure__c c = new Expenditure__c();
		Person__c p = [Select p.Name From Person__c p Where Name = :paidByName limit 1];
		c.Amount__c = amount;
		c.Name = expName;
		c.Exp_Date__c =;
		c.Paid_By__c = p.Id;
		insert c;

		return c;


  • Apex class in which webservice is going to be written must be declared “global“.
  • Keyword “webservice“ must be used for a method which should be exposed as a webservice.
  • Method must be declared as static.

In above code, I am using a custom object “Expenditure__c“ to insert into Salesforce.  Any type of code can be written.
Now in C#, create windows application with following controls:

Create Custom web service in salesforce using apex and consume using C#.Net
Create Custom web service in salesforce using apex and consume using C#.Net

Add the reference to “enterprise WSDL“ as shown in previous article.
Now, add the reference to newly created custom WSDL. (In Salesforce, go to the Apex classes and click on wsdl button to get the generated WSDL link)
In this case, name of web reference added is “SFDCCustom“.
Write below code, in c# application:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SFDCWebServiceExample.SFDCCustom;
using SFDCWebServiceExample.SFDC_Enterprise_WSDL;
using System.Web.Services.Protocols;

namespace SFDCWebServiceExample
    public partial class AddExpenditure : Form
        private SforceService binding;
        private LoginResult lr;
        private SaveExpenditureWebServiceService myBinding = new SaveExpenditureWebServiceService();

        public AddExpenditure()

        private void btnExpenditure_Click(object sender, EventArgs e)
            if (login())
                    #region[Consume Custome webservice]
                    //Code 2
                    myBinding.SessionHeaderValue = new SFDCCustom.SessionHeader();
                    myBinding.SessionHeaderValue. sessionId = lr.sessionId;

                    // Update: Copy the URL returned by login to the endpoint for our web service
                    int idx1 = lr.serverUrl.IndexOf(@"/services/");
                    int idx2 = myBinding.Url.IndexOf(@"/services/");
                    if (idx1 == 0 || idx2 == 0)
                        MessageBox.Show("Invalid URL strings in bindings");

                    myBinding.Url = lr.serverUrl.Substring(0, idx1) + myBinding.Url.Substring(idx2);

                    decimal amnt = 0;

                    Decimal.TryParse( txtAmount.Text,out amnt);

                    SFDCCustom.Expenditure__c c = myBinding.createExpenditure(amnt, txtExpName.Text, cboPaidBy.SelectedItem.ToString());
                    MessageBox.Show("Record Added Succesfully to salesforce");
                catch (Exception e1)

        private bool login()
                binding = new SforceService();
                binding.Timeout = 6000;
                lr = binding.login(txtUserName.Text, txtPwd.Text);
                return true;
            catch (SoapException e)
            return false;

        private bool validateControls()
            if (txtPwd.Text.Trim() == string.Empty && txtUserName.Text.Trim() == string.Empty)
                MessageBox.Show("Please provide the credentials");
                return false;
            return true;


  • To login into application, use the Enterprise WSDL and after that use custom WSDL (That’s why we have two objects of the binding).
  • After login update “SessionHeaderValue” by the custom WSDL sessionHeader.
  • After setting the session header, change the URL of the binding object.

In drop down list, the names of the person added must be available in Salesforce, so that the SOQL to find the person object should return the value.

Download Source code of consuming and creating web service in salesforce

Related posts

9 thoughts on “Create a custom Web service in Salesforce and consume it in C#.Net application”

  1. Hi ,
    I am getting Below Error.
    Can You help me ….Please.
    Error 1: The type or namespace name ‘Expenditure__c’ does not exist in the namespace ‘SFDCWebServiceExample.SFDCCustom’ (are you missing an assembly reference?)

  2. Hi,
    I’m getting error “No service available for class ‘SaveExpenditureWebServiceService'”

    are there something I missed when copy paste example source code?

  3. hi,
    i have one requirement of integration of sales force and biztalk.can you give me code
    of integrating biztalk and salesforce.?it’s very urgent.please help me!!!

  4. Hello,
    After adding first Web reference, i added the Generated WSDL from apex class to the web reference,but its not working.(as, in your above given example it is “SFDCCustom”)
    Thank You.

  5. Hi Jitendrazaaa/ Everyone,

    I am new to salesforce. I am here to integrate my basic windows app (visual studio) data to sales force lead.
    I followed your first tutorials and connected to sales force API. got all the salesforce objects into message box. So while following I couldn’t how to connect to sales force and load data to lead.

    Can please help me with load data into sales lead using windows app.
    Thank you

Leave a Reply