Friday, April 17, 2015

Consuming External Web Service in Salesforce (SOAP based Web Service)

The goal of this article to explain steps to consume SOAP based external web service in SFDC. The Steps are as follows :


Step 1:

1.1 Get a WSDL file of the webservice you want to consume.
(here in example I've used Temperature convertion webservice, the one available at    
         http://www.w3schools.com/webservices/tempconvert.asmx?WSDL )

Step 2:

2.1 Go to Develop>Apex classes and click on generate from WSDL

2.2 While you try to parse you'll get an error like "Found more than one wsdl:binding" , then manually remove all the Soap1.2 bindings as it is not supported by Salesforce.com (Salesforce compatible version Simple Object Access Protocol (SOAP) 1.1). After removing all the errors you'll get WSDL like the attached tempconvert.wsdl

2.3 After removing all the duplicate and incompatible bindings,etc. an apex class will be generated as soon as you click parse WSDL. Name the class and save.

2.4 Now you'll find a new class among rest of your Apex classes. now you you have to invoke the functions defined in the class to utilize the data from web service in your salesforce.com org.

Here is the code snippets with screenshot :

User Interface (Developed Using Visual force)



Controller Class (Developed Using Apex)

public class ConvertTempCls { public string cel{get;set;} public string far{get;set;} public string temp{get;set;} public string temp1{get;set;} public boolean showRes{get;set;} public ConvertTempCls(){ } public void convertTemp(){ tempuriOrg.TempConvertSoap obj = new tempuriOrg.TempConvertSoap(); temp=''; temp1=''; if(cel!='') temp=obj.CelsiusToFahrenheit(cel); if(far!='') temp1=obj.FahrenheitToCelsius(far); showRes=true; } }


Visual force Page

<apex:page controller="ConvertTempCls" > <apex:form > <apex:pageBlock > <apex:pageBlockButtons > <apex:commandButton value="Convert Temp" action="{!convertTemp}" /> </apex:pageBlockButtons> <table> <tr> <td>Enter temp in Celsius</td> <td><apex:inputText value="{!cel}" /></td> <td width="30px" ></td> <td><apex:outputText rendered="{!showRes}" >Result:-Temp in Fahrenheit is &nbsp;&nbsp; {!temp} </apex:outputText></td> </tr> <tr> <td>Enter temp in Fahrenheit </td> <td><apex:inputText value="{!far}" /></td> <td width="30px"></td> <td><apex:outputText rendered="{!showRes}" >Result:-Temp in Celsius is &nbsp;&nbsp; {!temp1} </apex:outputText></td> </tr> </table> </apex:pageBlock> </apex:form> </apex:page>


Generated Apex Class from WSDL

//Generated by wsdl2apex public class tempuriOrg { public class FahrenheitToCelsiusResponse_element { public String FahrenheitToCelsiusResult; private String[] FahrenheitToCelsiusResult_type_info = new String[]{'FahrenheitToCelsiusResult','http://tempuri.org/',null,'0','1','false'}; private String[] apex_schema_type_info = new String[]{'http://www.w3schools.com/webservices/','true','false'}; private String[] field_order_type_info = new String[]{'FahrenheitToCelsiusResult'}; } public class CelsiusToFahrenheitResponse_element { public String CelsiusToFahrenheitResult; private String[] CelsiusToFahrenheitResult_type_info = new String[]{'CelsiusToFahrenheitResult','http://tempuri.org/',null,'0','1','false'}; private String[] apex_schema_type_info = new String[]{'http://www.w3schools.com/webservices/','true','false'}; private String[] field_order_type_info = new String[]{'CelsiusToFahrenheitResult'}; } public class FahrenheitToCelsius_element { public String Fahrenheit; private String[] Fahrenheit_type_info = new String[]{'Fahrenheit','http://www.w3schools.com/webservices/',null,'0','1','false'}; private String[] apex_schema_type_info = new String[]{'http://www.w3schools.com/webservices/','true','false'}; private String[] field_order_type_info = new String[]{'Fahrenheit'}; } public class CelsiusToFahrenheit_element { public String Celsius; private String[] Celsius_type_info = new String[]{'Celsius','http://www.w3schools.com/webservices/',null,'0','1','false'}; private String[] apex_schema_type_info = new String[]{'http://www.w3schools.com/webservices/','true','false'}; private String[] field_order_type_info = new String[]{'Celsius'}; } public class TempConvertSoap { public String endpoint_x = 'http://www.w3schools.com/webservices/tempconvert.asmx'; public Map<String,String> inputHttpHeaders_x; public Map<String,String> outputHttpHeaders_x; public String clientCertName_x; public String clientCert_x; public String clientCertPasswd_x; public Integer timeout_x; private String[] ns_map_type_info = new String[]{'http://www.w3schools.com/webservices/', 'tempuriOrg'}; public String CelsiusToFahrenheit(String Celsius) { tempuriOrg.CelsiusToFahrenheit_element request_x = new tempuriOrg.CelsiusToFahrenheit_element(); request_x.Celsius = Celsius; tempuriOrg.CelsiusToFahrenheitResponse_element response_x; Map<String, tempuriOrg.CelsiusToFahrenheitResponse_element> response_map_x = new Map<String, tempuriOrg.CelsiusToFahrenheitResponse_element>(); response_map_x.put('response_x', response_x); WebServiceCallout.invoke( this, request_x, response_map_x, new String[]{endpoint_x, 'http://www.w3schools.com/webservices/CelsiusToFahrenheit', 'http://www.w3schools.com/webservices/', 'CelsiusToFahrenheit', 'http://www.w3schools.com/webservices/', 'CelsiusToFahrenheitResponse', 'tempuriOrg.CelsiusToFahrenheitResponse_element'} ); response_x = response_map_x.get('response_x'); return response_x.CelsiusToFahrenheitResult; } public String FahrenheitToCelsius(String Fahrenheit) { tempuriOrg.FahrenheitToCelsius_element request_x = new tempuriOrg.FahrenheitToCelsius_element(); request_x.Fahrenheit = Fahrenheit; tempuriOrg.FahrenheitToCelsiusResponse_element response_x; Map<String, tempuriOrg.FahrenheitToCelsiusResponse_element> response_map_x = new Map<String, tempuriOrg.FahrenheitToCelsiusResponse_element>(); response_map_x.put('response_x', response_x); WebServiceCallout.invoke( this, request_x, response_map_x, new String[]{endpoint_x, 'http://www.w3schools.com/webservices/FahrenheitToCelsius', 'http://www.w3schools.com/webservices/', 'FahrenheitToCelsius', 'http://www.w3schools.com/webservices/', 'FahrenheitToCelsiusResponse', 'tempuriOrg.FahrenheitToCelsiusResponse_element'} ); response_x = response_map_x.get('response_x'); return response_x.FahrenheitToCelsiusResult; } } }

8 comments:

  1. Replies
    1. The development of artificial intelligence (AI) has propelled more programming architects, information scientists, and different experts to investigate the plausibility of a vocation in machine learning. Notwithstanding, a few newcomers will in general spotlight a lot on hypothesis and insufficient on commonsense application. machine learning projects for final year In case you will succeed, you have to begin building machine learning projects in the near future.

      Projects assist you with improving your applied ML skills rapidly while allowing you to investigate an intriguing point. Furthermore, you can include projects into your portfolio, making it simpler to get a vocation, discover cool profession openings, and Final Year Project Centers in Chennai even arrange a more significant compensation.

      Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account.

      The Nodejs Projects Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training

      Delete
  2. Just to add for this Blog readers, please add the below :

    http://www.w3schools.com/webservices/tempconvert.asmx

    in your "Remotes Site Settings" to access the service

    ReplyDelete
  3. WSDL :
    http://www.w3schools.com/xml/tempconvert.asmx?WSDL

    ReplyDelete
  4. i got below error
    System.CalloutException: Web service callout failed: Unexpected element. Parser was expecting element 'http://schemas.xmlsoap.org/soap/envelope/:Envelope' but found ':head'

    can you please help on it.

    ReplyDelete
  5. Very efficiently written information. It will be beneficial to anybody who utilizes it, including me. Keep up the good work. For sure i will check out more posts. This site seems to get a good amount of visitors. Webdesign bureau

    ReplyDelete
  6. Where you live issues. In the event that it is a long way from the disconnected instructional hubs, at that point it might imply that you are just left with the internet preparing choice. In the event that you live in a region with no web association, this may, then again, imply that your lone choice is the disconnected schools.
    Web Design Curriculum

    ReplyDelete
  7. I know your expertise on this. I must say we should have an online discussion on this. Writing only comments will close the discussion straight away! And will restrict the benefits from this information. freelance web designer

    ReplyDelete

Governor Limits Example-Too Many DML Statements :151

Governor Limits Example : Too Many DML Statements :151 (Sunil Sharma) Example:Code Hitting Governor Limits System.LimitExceptio...