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 force.com 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 = Date.today(); c.Paid_By__c = p.Id; insert c; return c; } }
Note:
- 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:
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() { InitializeComponent(); } private void btnExpenditure_Click(object sender, EventArgs e) { if (login()) { try { #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"); #endregion } catch (Exception e1) { MessageBox.Show(e1.Message); } } } private bool login() { try { binding = new SforceService(); binding.Timeout = 6000; lr = binding.login(txtUserName.Text, txtPwd.Text); return true; } catch (SoapException e) { MessageBox.Show(e.Message); } 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; } } }
Note:
- 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.
Assumption:
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
Leave a Reply