We went over the basics of invoking an external web service from Salesforce.com Apex code in Parts I, II & III of this series. In this article, we will go over returning an array from the external web service back to Salesforce.com.
Let’s dive into the specifics right away:
- First, create your asp.net web service (I used Visual Studio 2008). I just created a basic asp.net web service called ArrayWebService, and changed the HelloWorld() method to the “GetArray()” method instead, with a string array as the return type. Here’s my web service code, I have highlighted the important parts:-
Code Snippet
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Services;
- namespace ArrayWebService
- {
- /// <summary>
- /// Summary description for Service1
- /// </summary>
- [WebService(Namespace = "http://tempuri.org/")]
- [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
- [System.ComponentModel.ToolboxItem(false)]
- public class Service1 : System.Web.Services.WebService
- {
- [WebMethod]
- public string[] GetArray()
- {
- return new String[] {
- "Empire State Building"
- , "Rockefeller Center"
- , "Wall Street"
- , "World Trade Center"
- , "Statue Of Liberty" };
- }
- }
- }
- Next, publish your web service. In solution explorer, right-click on the web service, and select publish as shown in Figure 1 below. This is required to “publish” your web service to the local IIS. Prior to doing this, make sure you have a folder called “ArrayWebService” created under your local IIS folder (usually this is C:\InetPub\wwwroot).
- On the “Publish Web” screen, enter the path as shown in Figure # 2 below. Make sure that the options are selected as shown. Hit Publish – this should copy all of your project files under the C:\InetPub\wwwroot\ArrayWebService folder.
- Test your web service by browsing to http://localhost/arraywebservice/service1.asmx. This should show a screen similar to Figure # 3 below.
- You need to now make sure that your web service is now callable by salesforce.com. In order for this to happen, your web service should be “exposed” to the internet. If you are on a company network, you are most likely behind a firewall and salesforce.com will not be able to access your web service. I would like to suggest here that you go through Part II of this article series to understand how to use port forwarding to expose your WS to Salesforce.com.
- To test if your web service is working correctly, go to http://www.webpagetest.org/, and in the web site address, enter your web service’s address – this should be something like http://{your IP Address}/ArrayWebService/Service1.asmx.
- If you are able to run your web service locally, but are having trouble testing it from “webpagetest.org”, it is most likely that your web service is not yet exposed to the internet. Consult the network person at your firm to check how you can fix this.
- Once you are able to successfully test the WS, try creating a class in Salesforce.com from the web service WSDL – detailed description of this process is covered in Part III of this series – go through it to understand how to create the class on the Salesforce side using your web service WSDL.
- When you create the class in Salesforce, the code should look like the below. Take special note of the fact that I have modified the IP Address of the web service to go to my machine. Another thing to note here is that the “GetArray” method does not return a string array – instead, it returns an object of type “InvokeArrayWebService.ArrayOfString”. This means that when invoking the method, we will have to write one more extra step to extra the return result from the ArrayOfString return object.
Code Snippet
- //Generated by wsdl2apex
- public class InvokeArrayWebService {
- public class GetArrayResponse_element {
- public InvokeArrayWebService.ArrayOfString GetArrayResult;
- private String[] GetArrayResult_type_info = new String[]{'GetArrayResult','http://tempuri.org/','ArrayOfString','0','1','false'};
- private String[] apex_schema_type_info = new String[]{'http://tempuri.org/','true','false'};
- private String[] field_order_type_info = new String[]{'GetArrayResult'};
- }
- public class GetArray_element {
- private String[] apex_schema_type_info = new String[]{'http://tempuri.org/','true','false'};
- private String[] field_order_type_info = new String[]{};
- }
- public class ArrayOfString {
- public String[] string_x;
- private String[] string_x_type_info = new String[]{'string','http://www.w3.org/2001/XMLSchema','string','0','-1','true'};
- private String[] apex_schema_type_info = new String[]{'http://tempuri.org/','true','false'};
- private String[] field_order_type_info = new String[]{'string_x'};
- }
- public class Service1Soap {
- public String endpoint_x = 'http://76.116.65.169/arraywebservice/service1.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://tempuri.org/', 'InvokeArrayWebService'};
- public InvokeArrayWebService.ArrayOfString GetArray() {
- InvokeArrayWebService.GetArray_element request_x = new InvokeArrayWebService.GetArray_element();
- InvokeArrayWebService.GetArrayResponse_element response_x;
- Map<String, InvokeArrayWebService.GetArrayResponse_element> response_map_x = new Map<String, InvokeArrayWebService.GetArrayResponse_element>();
- response_map_x.put('response_x', response_x);
- WebServiceCallout.invoke(
- this,
- request_x,
- response_map_x,
- new String[]{endpoint_x,
- 'http://tempuri.org/GetArray',
- 'http://tempuri.org/',
- 'GetArray',
- 'http://tempuri.org/',
- 'GetArrayResponse',
- 'InvokeArrayWebService.GetArrayResponse_element'}
- );
- response_x = response_map_x.get('response_x');
- return response_x.GetArrayResult;
- }
- }
- }
- Next, I created an anonymous code block with the below piece of code. Take special note of the highlighted portion – since the GetArray() method is returning an object of type “InvokeArrayWebService.ArrayOfString”, we cannot directly access the array being returned. We need to go into the “string_x” object inside the returned object to get the returned array.
Code Snippet
- InvokeArrayWebService.Service1Soap s1 = new InvokeArrayWebService.Service1Soap();
- InvokeArrayWebService.ArrayOfString arrayofstring;
- arrayofstring = s1.GetArray();
- String [] returnArray = arrayofstring.string_x;
- for(String s: returnArray)
- {
- System.Debug('*****************' + s);
- }
- If you look carefully at the class generated from the WSDL, you can figure it out yourself. The ArrayOfString object has a string[] named string_x – this is the object that will contain the return value. You can inspect the “field_order_type_info” object to deduce this.
Code Snippet
- public class ArrayOfString {
- public String[] string_x;
- private String[] string_x_type_info = new String[]{'string','http://www.w3.org/2001/XMLSchema','string','0','-1','true'};
- private String[] apex_schema_type_info = new String[]{'http://tempuri.org/','true','false'};
- private String[] field_order_type_info = new String[]{'string_x'};
- }
- So, there you go, this is how you can return an array from an external web service to the Salesforce.com apex class.
No comments:
Post a Comment