{"id":3537,"date":"2013-10-13T13:34:31","date_gmt":"2013-10-13T08:04:31","guid":{"rendered":"http:\/\/JitendraZaa.com\/blog\/?p=3537"},"modified":"2013-10-13T13:34:31","modified_gmt":"2013-10-13T08:04:31","slug":"consuming-external-webservice-in-apex","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/consuming-external-webservice-in-apex\/","title":{"rendered":"Consuming External Web Service in Apex &#8211; Salesforce"},"content":{"rendered":"<p style=\"text-align: justify;\">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 in this article is <a title=\"Free Web Services\" href=\"http:\/\/www.webservicex.net\" rel=\"nofollow\">http:\/\/www.webservicex.net<\/a> , from this location we are using <a title=\"Stock Quote Free Webservice\" href=\"http:\/\/www.webservicex.net\/WS\/WSDetails.aspx?CATID=2&amp;WSID=9\" rel=\"nofollow\">Stock Quote Webservice<\/a>. To consume this, we need WSDL.<\/p>\n<p style=\"text-align: justify;\">The <a title=\"Web Services Description Language\" href=\"http:\/\/en.wikipedia.org\/wiki\/Web_Services_Description_Language\" rel=\"nofollow\">Web Services Description Language (WSDL)<\/a> is an XML-based interface description language that is used for describing the functionality offered by a Web Service.<\/p>\n<p style=\"text-align: justify;\">WSDL have to be <a title=\"Free WSDL to Download\" href=\"http:\/\/www.webservicex.net\/stockquote.asmx?WSDL\">downloaded from this location<\/a>. We need this WSDL in later part of this tutorial.<\/p>\n<p style=\"text-align: justify;\">Once, WSDL is downloaded and saved on local drive. We have to go to Salesforce and navigate to <strong>&#8220;Setup | Develop | Apex Classes&#8221;<\/strong>. On right hand side, you will find button named as <strong>&#8220;Generate from WSDL&#8221;<\/strong>. This button will generate equivalent Apex class to support Webservice call. In some Programming languages, these classes are known as Proxy classes or Stubs.<a href=\"https:\/\/jitendrazaa.com\/blog\/wp-content\/uploads\/2013\/10\/stockquote_New.asmx_.xml_.txt\"><br \/>\n<\/a><\/p>\n<p style=\"text-align: justify;\">When we click on &#8220;Generate from WSDL&#8221; button, it will prompt for WSDL File. Select WSDL file downloaded previously and click on &#8220;Parse WSDL&#8221; button. On next page you will get this error : <strong>&#8220;Failed to parse wsdl: Found more than one wsdl:portType. WSDL with multiple portType not supported&#8221;<\/strong>.<\/p>\n<p style=\"text-align: justify;\">In some cases you may also get error like :<strong> &#8220;Failed to parse wsdl: Found more than one wsdl:binding. WSDL with multiple binding not supported&#8221;<\/strong>.<\/p>\n<p style=\"text-align: justify;\"><!--more-->Intentionally, I have used this WSDL to explain that currently Salesforce supports only single portType and binding.<\/p>\n<p style=\"text-align: justify;\"><a title=\"Webservice Porttype Tutorial\" href=\"http:\/\/www.w3schools.com\/webservices\/ws_wsdl_ports.asp\">PortType <\/a>: defines a web service, the operations that can be performed, and the messages that are involved.<br \/>\n<a title=\"Webservice Binding\" href=\"http:\/\/www.w3schools.com\/webservices\/ws_wsdl_binding.asp\">Binding <\/a>: WSDL bindings defines the message format and protocol details for a web service.<\/p>\n<p style=\"text-align: justify;\">Reason, we are getting an error because &#8220;<em>wsdltoApex<\/em>&#8221; doesnt support multiple PortType, Binding, SOAP 1.2 and Schema imports. <a title=\"Salesforce WSDLtoApex Documentation\" href=\"http:\/\/www.salesforce.com\/us\/developer\/docs\/apexcode\/Content\/apex_callouts_wsdl2apex.htm\">You can read more here from Salesforce documentation<\/a>.<\/p>\n<p style=\"text-align: justify;\"><strong>How to resolve multiple portType and Binding error in Apex while generating stubs ?<\/strong><\/p>\n<p style=\"text-align: justify;\">We have to modify downloaded WSDL to make sure it only contains Single Binding and single PortType. Before modifying I would suggest to read about WSDL elements. I have uploaded both versions of WSDL (Please change attached file extensions from txt to xml). First one is Actual WSDL and second I have modified to remove errors. You can compare both and check how I removed multiple PortType and Binding elements from WSDL.<\/p>\n<ol>\n<li><a href=\"https:\/\/jitendrazaa.com\/blog\/wp-content\/uploads\/2013\/10\/stockquote_New.asmx_.xml_.txt\">Stockquote Actual WSDL File (Change extension from txt to xml)<\/a><\/li>\n<li><a href=\"https:\/\/jitendrazaa.com\/blog\/wp-content\/uploads\/2013\/10\/stockquote.asmx_.xml_.txt\">Stockquote Updated WSDL File (Change extension from txt to xml)<\/a><\/li>\n<\/ol>\n<p>Once, you modify your wsdl, try to generate Apex class again and this time you will be able to generate class without any error.<\/p>\n<figure id=\"attachment_3541\" aria-describedby=\"caption-attachment-3541\" style=\"width: 625px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2013\/10\/Generating-Apex-from-WSDL-in-Salesforce.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-large wp-image-3541\" alt=\"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=625%2C210&#038;ssl=1\" width=\"625\" height=\"210\" \/><\/a><figcaption id=\"caption-attachment-3541\" class=\"wp-caption-text\">Generating Apex from WSDL in Salesforce<\/figcaption><\/figure>\n<p>So, here we have successfully generated Stub classes for Webservice.<\/p>\n<p><strong>How to use generated web-service Apex stub ?<\/strong><\/p>\n<p style=\"text-align: justify;\">Before trying to use webservice, we have to inform Salesforce that our code will try to get some Data from External source (rather interact with external system). And therefore Remote Site Setting comes into picture.<\/p>\n<p style=\"text-align: justify;\">Navigate to &#8220;<strong>Setup | Security Control | Remote Site Settings<\/strong>&#8220;.<\/p>\n<p style=\"text-align: justify;\">Create new Site setting for URL &#8220;<a title=\"Free Public Webservice\" href=\"http:\/\/www.webservicex.net\" rel=\"nofollow\">http:\/\/www.webservicex.net<\/a>&#8220;. If we skip this step, we will endup with error like <strong>&#8220;Unauthorized\u00a0Endpoint&#8221;<\/strong>.<\/p>\n<p style=\"text-align: justify;\">We can use generated Apex class in Visualforce or some other location. To keep this tutorial simple, I am using &#8220;Developer Console&#8221;. Open Developer Console and press &#8220;Ctrl+E&#8221;.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nwwwWebservicexNet.StockQuoteSoap proxyClass = new wwwWebservicexNet.StockQuoteSoap();\nString retVal = proxyClass.GetQuote('CTSH');\nSystem.debug(retVal);\n<\/pre>\n<p style=\"text-align: justify;\">In above code &#8220;CTSH&#8221; is Stock symbol for Cognizant Technology Solution. You can use any other valid Stock Symbol. Output will be visible in Console Log. We can use this output as per our need<\/p>\n<p style=\"text-align: justify;\"><strong>How you will find that which Class to instantiate ?<\/strong><br \/>\nAs shown in above code, I have created Object for &#8220;wwwWebservicexNet.StockQuoteSoap&#8221;. \u00a0I was able to identify that which class to instantiate? I was able to identify by porttype element name from WSDL.<\/p>\n<p style=\"text-align: justify;\"><strong>How to increase Time out in Webservice ?<\/strong><\/p>\n<p style=\"text-align: justify;\">You may get Timeout exception while calling webservice. Default time is 10sec (At time of writing this tutorial, may change in future release). we can use &#8220;<strong>timeout_x<\/strong>&#8221; property to increase time to wait for response from web service. So above code can be re-written as<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nwwwWebservicexNet.StockQuoteSoap proxyClass = new wwwWebservicexNet.StockQuoteSoap();\nproxyClass.timeout_x = 20000 ; \/\/ timeout in milliseconds\nString retVal = proxyClass.GetQuote('CTSH');\nSystem.debug(retVal);\n<\/pre>\n<p><strong>Some Known Limitations to WSDL2Apex :<\/strong><\/p>\n<ol>\n<li>It does not support multiple port bindings.<\/li>\n<li>Inheritance is not supported in wsdl to APEX conversion.<\/li>\n<li>Complex Object types such as Enumeration are not supported.<\/li>\n<li>WSDL Import functionality is not supported.<\/li>\n<\/ol>\n<p>I hope this tutorial will be helpful for newbies. Please post your comment and feedback about this tutorial. I will be delighted to answer your query about this article.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 in this article is http:\/\/www.webservicex.net [&hellip;]<\/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_post_was_ever_published":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":""},"categories":[20,9],"tags":[337,77,120,331,184,216,225,226],"class_list":["post-3537","post","type-post","status-publish","format-standard","hentry","category-apex","category-salesforce","tag-apex","tag-developer-console","tag-integration","tag-salesforce","tag-soap","tag-web-service","tag-wsdl","tag-wsdl2apex"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":2445,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/create-a-custom-web-service-in-salesforce-and-consume-it-in-c-net-application\/","url_meta":{"origin":3537,"position":0},"title":"Create a custom Web service in Salesforce and consume it in C#.Net application","author":"Jitendra","date":"September 23, 2011","format":false,"excerpt":"In this tutorial, we will create the web service in salesforce and consume it in c#.Net application","rel":"","context":"In &quot;c#&quot;","block_context":{"text":"c#","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/microsoft\/csharp\/"},"img":{"alt_text":"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=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":4501,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/using-soapui-with-salesforce-to-test-standard-and-custom-web-services-response\/","url_meta":{"origin":3537,"position":1},"title":"Using soapUI with Salesforce to test standard and custom web services response","author":"Jitendra","date":"May 27, 2015","format":false,"excerpt":"soapUI is most common tool available to test Soap based web services, it also has capability to test REST web services. soapUI can be used to test Partner WSDL, enterprise WSDL, Tooling API, Metadata API to study capability and response from Salesforce before writing any code in Java, C# or\u2026","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Create New SoapUI project","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/05\/Create-New-SoapUI-project.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/05\/Create-New-SoapUI-project.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/05\/Create-New-SoapUI-project.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":2436,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/consume-salesforce-web-service-in-c-net-application\/","url_meta":{"origin":3537,"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":6274,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/enterprise-territory-management-auto-account-assignment-using-apex\/","url_meta":{"origin":3537,"position":3},"title":"Enterprise Territory Management &#8211; Auto Account Assignment using Apex","author":"Jitendra","date":"September 22, 2017","format":false,"excerpt":"Use Apex code to auto assign Accounts on basis of Enterprise Territory Assignment rules","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Enterprise Territory - Auto Account Assignment using Apex","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/09\/Enterprise-Territory-Auto-Account-Assignment-using-Apex.jpg?fit=900%2C600&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/09\/Enterprise-Territory-Auto-Account-Assignment-using-Apex.jpg?fit=900%2C600&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/09\/Enterprise-Territory-Auto-Account-Assignment-using-Apex.jpg?fit=900%2C600&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/09\/Enterprise-Territory-Auto-Account-Assignment-using-Apex.jpg?fit=900%2C600&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":2903,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-toolkit-for-php\/","url_meta":{"origin":3537,"position":4},"title":"Salesforce Toolkit for PHP","author":"Jitendra","date":"June 7, 2012","format":false,"excerpt":"Example of using PHP toolkit in Salesforce with Sample code","rel":"","context":"In &quot;Force.com&quot;","block_context":{"text":"Force.com","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/force-com\/"},"img":{"alt_text":"force.com toolkit for PHP toolkit","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/06\/force.com-toolkit-for-PHP-toolkit.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/06\/force.com-toolkit-for-PHP-toolkit.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/06\/force.com-toolkit-for-PHP-toolkit.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":4532,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-to-salesforce-integration-using-named-credentials-in-just-5-lines-of-code\/","url_meta":{"origin":3537,"position":5},"title":"Salesforce to Salesforce integration using Named Credentials in 5 lines","author":"Jitendra","date":"May 29, 2015","format":false,"excerpt":"I have already written article to integrate Salesforce with other Salesforce instance around 3 years back. In last 3 years, Salesforce has changed a lot. This time I will integrate Salesforce with other Salesforce only only in 5 lines of code, can you believe it \u00a0:) ? Check my old\u2026","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Authenticated Named Credential in Salesforce","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/05\/Authenticated-Named-Credential-in-Salesforce.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/3537","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=3537"}],"version-history":[{"count":0,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/3537\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=3537"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=3537"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=3537"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}