{"id":2445,"date":"2011-09-23T11:23:01","date_gmt":"2011-09-23T05:53:01","guid":{"rendered":"http:\/\/JitendraZaa.com\/blog\/?p=2445"},"modified":"2011-09-23T11:23:01","modified_gmt":"2011-09-23T05:53:01","slug":"create-a-custom-web-service-in-salesforce-and-consume-it-in-c-net-application","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/create-a-custom-web-service-in-salesforce-and-consume-it-in-c-net-application\/","title":{"rendered":"Create a custom Web service in Salesforce and consume it in C#.Net application"},"content":{"rendered":"<p>In <strong><a title=\"Consume enterprise wsdl in salesforce\" href=\"https:\/\/jitendrazaa.com\/blog\/webtech\/salesforce\/consume-salesforce-web-service-in-c-net-application\/\">Previous article<\/a><\/strong>, we have consumed the standard enterprise wsdl of the salesforce. In this article we will create the webservice using <strong>apex<\/strong> in salesforce and consume it in C#.Net application.<\/p>\n<p>In Salesforce create below class (using<strong> force.com IDE<\/strong>)<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nglobal public with sharing class SaveExpenditureWebService {\n\n\twebservice static Expenditure__c createExpenditure(Decimal amount,String expName, String paidByName )\n\t{\n\t\tExpenditure__c c = new Expenditure__c();\n\t\tPerson__c p = &#x5B;Select p.Name From Person__c p Where Name = :paidByName limit 1];\n\t\tc.Amount__c = amount;\n\t\tc.Name = expName;\n\t\tc.Exp_Date__c = Date.today();\n\t\tc.Paid_By__c = p.Id;\n\t\tinsert c;\n\n\t\treturn c;\n\t}\n}\n<\/pre>\n<p><strong>Note:<\/strong><\/p>\n<ul>\n<li>Apex class in which webservice is going to be written must be declared &#8220;<strong>global<\/strong>&#8220;\u009d.<\/li>\n<li>Keyword &#8220;<strong>webservice<\/strong>&#8220;\u009d must be used for a method which should be exposed as a webservice.<\/li>\n<li>Method must be declared as <strong>static<\/strong>.<\/li>\n<\/ul>\n<p>In above code, I am using a custom object &#8220;<strong>Expenditure__c<\/strong>&#8220;\u009d to insert into Salesforce. \u00a0Any type of code can be written.<br \/>\nNow in C#, create windows application with following controls:<\/p>\n<figure id=\"attachment_2446\" aria-describedby=\"caption-attachment-2446\" style=\"width: 498px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/09\/Create-Custom-web-service-in-salesforce-using-apex-and-consume-using-C.Net_.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-2446 \" title=\"Create Custom web service in salesforce using apex and consume using C#.Net\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/09\/Create-Custom-web-service-in-salesforce-using-apex-and-consume-using-C.Net_.png?resize=498%2C357&#038;ssl=1\" alt=\"Create Custom web service in salesforce using apex and consume using C#.Net\" width=\"498\" height=\"357\" \/><\/a><figcaption id=\"caption-attachment-2446\" class=\"wp-caption-text\">Create Custom web service in salesforce using apex and consume using C#.Net<\/figcaption><\/figure>\n<p><!--more-->Add the reference to &#8220;<strong>enterprise WSDL<\/strong>&#8220;\u009d as shown in previous article.<br \/>\nNow, 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)<br \/>\nIn this case, name of web reference added is &#8220;<strong>SFDCCustom<\/strong>&#8220;\u009d.<br \/>\nWrite below code, in c# application:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nusing System;\nusing System.Collections.Generic;\nusing System.ComponentModel;\nusing System.Data;\nusing System.Drawing;\nusing System.Linq;\nusing System.Text;\nusing System.Windows.Forms;\nusing SFDCWebServiceExample.SFDCCustom;\nusing SFDCWebServiceExample.SFDC_Enterprise_WSDL;\nusing System.Web.Services.Protocols;\n\nnamespace SFDCWebServiceExample\n{\n    public partial class AddExpenditure : Form\n    {\n        private SforceService binding;\n        private LoginResult lr;\n        private SaveExpenditureWebServiceService myBinding = new SaveExpenditureWebServiceService();\n\n        public AddExpenditure()\n        {\n            InitializeComponent();\n        }\n\n        private void btnExpenditure_Click(object sender, EventArgs e)\n        {\n            if (login())\n            {\n                try\n                {\n                    #region&#x5B;Consume Custome webservice]\n                    \/\/Code 2\n                    myBinding.SessionHeaderValue = new SFDCCustom.SessionHeader();\n                    myBinding.SessionHeaderValue. sessionId = lr.sessionId;\n\n                    \/\/ Update: Copy the URL returned by login to the endpoint for our web service\n                    int idx1 = lr.serverUrl.IndexOf(@&quot;\/services\/&quot;);\n                    int idx2 = myBinding.Url.IndexOf(@&quot;\/services\/&quot;);\n                    if (idx1 == 0 || idx2 == 0)\n                    {\n                        MessageBox.Show(&quot;Invalid URL strings in bindings&quot;);\n                    }\n\n                    myBinding.Url = lr.serverUrl.Substring(0, idx1) + myBinding.Url.Substring(idx2);\n\n                    decimal amnt = 0;\n\n                    Decimal.TryParse( txtAmount.Text,out amnt);\n\n                    SFDCCustom.Expenditure__c c = myBinding.createExpenditure(amnt, txtExpName.Text, cboPaidBy.SelectedItem.ToString());\n                    MessageBox.Show(&quot;Record Added Succesfully to salesforce&quot;);\n                    #endregion\n                }\n                catch (Exception e1)\n                {\n                    MessageBox.Show(e1.Message);\n                }\n            }\n        }\n\n        private bool login()\n        {\n            try\n            {\n                binding = new SforceService();\n                binding.Timeout = 6000;\n                lr = binding.login(txtUserName.Text, txtPwd.Text);\n                return true;\n            }\n            catch (SoapException e)\n            {\n                MessageBox.Show(e.Message);\n            }\n            return false;\n        }\n\n        private bool validateControls()\n        {\n            if (txtPwd.Text.Trim() == string.Empty &amp;&amp; txtUserName.Text.Trim() == string.Empty)\n            {\n                MessageBox.Show(&quot;Please provide the credentials&quot;);\n                return false;\n            }\n            return true;\n        }\n    }\n}\n\n<\/pre>\n<p><strong>Note:<\/strong><\/p>\n<ul>\n<li>To login into application, use the Enterprise WSDL and after that use custom WSDL (That&#8217;s why we have two objects of the binding).<\/li>\n<li>After login update &#8220;<strong>SessionHeaderValue&#8221;<\/strong> by the custom WSDL sessionHeader.<\/li>\n<li>After setting the session header, change the URL of the binding object.<\/li>\n<\/ul>\n<p><strong>Assumption:<\/strong><br \/>\nIn 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.<\/p>\n<p><a href=\"https:\/\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/09\/SFDCWebServiceExample.zip\">Download Source code of consuming and creating web service in salesforce<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will create the web service in salesforce and consume it in c#.Net application<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"jz_research_post":"","_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[22,9],"tags":[337,331,336],"class_list":["post-2445","post","type-post","status-publish","format-standard","hentry","category-csharp","category-salesforce","tag-apex","tag-salesforce","tag-visualforce"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":3537,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/consuming-external-webservice-in-apex\/","url_meta":{"origin":2445,"position":0},"title":"Consuming External Web Service in Apex &#8211; Salesforce","author":"Jitendra","date":"October 13, 2013","format":false,"excerpt":"One of the feature we have in Salesforce is that we can easily consume External Web Services. In this article, we will learn step by step demo of consuming Web Service in Apex. There are many public websites available to consume Web Service and one of them, I am using\u2026","rel":"","context":"In &quot;Apex&quot;","block_context":{"text":"Apex","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/apex\/"},"img":{"alt_text":"Generating Apex from WSDL in Salesforce","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2013\/10\/Generating-Apex-from-WSDL-in-Salesforce-1024x345.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2013\/10\/Generating-Apex-from-WSDL-in-Salesforce-1024x345.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2013\/10\/Generating-Apex-from-WSDL-in-Salesforce-1024x345.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":3411,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/apex-interview-question-salesforce-part-16\/","url_meta":{"origin":2445,"position":1},"title":"Apex Interview Question \u2013 Salesforce &#8211; Part 16","author":"Jitendra","date":"July 28, 2013","format":false,"excerpt":"151. Give Sample Code Snippet of Apex that that will show that how Parent and Child record can be inserted in Single Statement ? Ans : It can be done with help of External Id. [java] Date dt = Date.today().addDays(7); Opportunity newOpportunity = new Opportunity(Name = 'shivasoft', StageName = 'Prospecting',\u2026","rel":"","context":"In &quot;Apex&quot;","block_context":{"text":"Apex","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/apex\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2436,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/consume-salesforce-web-service-in-c-net-application\/","url_meta":{"origin":2445,"position":2},"title":"Consume Salesforce Web service in C# .Net Application","author":"Jitendra","date":"September 23, 2011","format":false,"excerpt":"This tutorial is the demonstration of how to consume the web service from salesforce.","rel":"","context":"In &quot;c#&quot;","block_context":{"text":"c#","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/microsoft\/csharp\/"},"img":{"alt_text":"C# Application to consume salesforce webservice","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/09\/C-Application-to-consume-salesforce-webservice.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":4486,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/continuation-object-in-apex-asynchronous-callouts-for-long-running-request-live-demo\/","url_meta":{"origin":2445,"position":3},"title":"Continuation object in Apex &#8211; Asynchronous callouts for long running request &#8211; Live Demo","author":"Jitendra","date":"May 22, 2015","format":false,"excerpt":"Check the below video first if you are planning to use Continuation https:\/\/youtu.be\/ya2N9EX9dmg We may run into\u00a0scenario in Salesforce project, where we need call external web service but no need to\u00a0wait for response. Response from external web service can be processed asynchronously and once processed it can be presented in\u2026","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Execution Flow of an Asynchronous Callout - Image from Salesforce documentation","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/05\/apex_continuations_diagram.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/05\/apex_continuations_diagram.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/05\/apex_continuations_diagram.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/05\/apex_continuations_diagram.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":2546,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/email-services-in-salesforce-with-simple-example\/","url_meta":{"origin":2445,"position":4},"title":"Email Services in Salesforce with simple example","author":"Jitendra","date":"January 11, 2012","format":false,"excerpt":"Complete tutorial of Email services in Salesforce with simple step by step example","rel":"","context":"In &quot;Apex&quot;","block_context":{"text":"Apex","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/apex\/"},"img":{"alt_text":"How Email Services works in Salesforce","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/12\/Email-Services-in-Salesforce.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":2501,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/very-useful-tips-and-tricks-of-the-apex-salesforce-interview-questions-part-4\/","url_meta":{"origin":2445,"position":5},"title":"Latest Salesforce Interview Questions &#8211; Part 4 &#8211; Related to Dynamic Apex","author":"Jitendra","date":"November 27, 2011","format":false,"excerpt":"Most Frequently Asked interview questions of Apex, Dynamic Apex, SOSL, Visualforce, SOQL in Salesforce.com SFDC","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/2445","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=2445"}],"version-history":[{"count":0,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/2445\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=2445"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=2445"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=2445"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}