function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
rajsrajs 

How to call webservices,Guide me

Hi,I am not able to call the webservices which is exposed by our client.I have used webservice callout and http protocols but not working.

 

 can you guide me ?

 

What I have tried ,

 

My services are living here 'https://www.criminalbackground.com/HRXMLWebServiceTest/Service.asmx'

 

Http h = new Http(); HttpRequest req = new HttpRequest(); String url= 'https://www.criminalbackground.com/HRXMLWebServiceTest/Service.asmx'; req.setEndpoint(url); req.setMethod('POST'); req.setHeader('content-type','text/xml'); req.setHeader('SOAPAction','GetPackages(userId,password)'); HttpResponse res = new HttpResponse(); res= h.send(req); //code ...

how to call the 'getpackage(String UserId,String Password)' method from our apex class.

Please give some example code other than sfdoc examples which will be helpful to us.

 

Thanks,

Raj

Message Edited by rajs on 07-10-2009 07:59 AM
soofsoof

:-)

 

Okay, here's your code... create the class below.  Actually, I recommend that you read the docs, and you'll see how easy it is to generate the following class from your WSDL.

 

 


//Generated by wsdl2apex

public class CriminalBackground {
public class ArchiveOrder_element {
public String username;
public String password;
public Integer orderID;
private String[] username_type_info = new String[]{'username','http://www.w3.org/2001/XMLSchema','string','0','1','false'};
private String[] password_type_info = new String[]{'password','http://www.w3.org/2001/XMLSchema','string','0','1','false'};
private String[] orderID_type_info = new String[]{'orderID','http://www.w3.org/2001/XMLSchema','int','1','1','false'};
private String[] apex_schema_type_info = new String[]{'http://www.criminalbackground.com/webservices/','true','false'};
private String[] field_order_type_info = new String[]{'username','password','orderID'};
}
public class ServiceSoap {
public String endpoint_x = 'https://www.criminalbackground.com/HRXMLWebServiceTest/Service.asmx';
public Map inputHttpHeaders_x;
public Map outputHttpHeaders_x;
public String clientCert_x;
public String clientCertPasswd_x;
public Integer timeout_x;
private String[] ns_map_type_info = new String[]{'http://www.criminalbackground.com/webservices/', 'CriminalBackground'};
public String GetResults(String username,String password) {
CriminalBackground.GetResults_element request_x = new CriminalBackground.GetResults_element();
CriminalBackground.GetResultsResponse_element response_x;
request_x.username = username;
request_x.password = password;
Map response_map_x = new Map();
response_map_x.put('response_x', response_x);
WebServiceCallout.invoke(
this,
request_x,
response_map_x,
new String[]{endpoint_x,
'http://www.criminalbackground.com/webservices/GetResults',
'http://www.criminalbackground.com/webservices/',
'GetResults',
'http://www.criminalbackground.com/webservices/',
'GetResultsResponse',
'CriminalBackground.GetResultsResponse_element'}
);
response_x = response_map_x.get('response_x');
return response_x.GetResultsResult;
}
public String GetPackages(String username,String password) {
CriminalBackground.GetPackages_element request_x = new CriminalBackground.GetPackages_element();
CriminalBackground.GetPackagesResponse_element response_x;
request_x.username = username;
request_x.password = password;
Map response_map_x = new Map();
response_map_x.put('response_x', response_x);
WebServiceCallout.invoke(
this,
request_x,
response_map_x,
new String[]{endpoint_x,
'http://www.criminalbackground.com/webservices/GetPackages',
'http://www.criminalbackground.com/webservices/',
'GetPackages',
'http://www.criminalbackground.com/webservices/',
'GetPackagesResponse',
'CriminalBackground.GetPackagesResponse_element'}
);
response_x = response_map_x.get('response_x');
return response_x.GetPackagesResult;
}
public String ArchiveOrder(String username,String password,Integer orderID) {
CriminalBackground.ArchiveOrder_element request_x = new CriminalBackground.ArchiveOrder_element();
CriminalBackground.ArchiveOrderResponse_element response_x;
request_x.username = username;
request_x.password = password;
request_x.orderID = orderID;
Map response_map_x = new Map();
response_map_x.put('response_x', response_x);
WebServiceCallout.invoke(
this,
request_x,
response_map_x,
new String[]{endpoint_x,
'http://www.criminalbackground.com/webservices/ArchiveOrder',
'http://www.criminalbackground.com/webservices/',
'ArchiveOrder',
'http://www.criminalbackground.com/webservices/',
'ArchiveOrderResponse',
'CriminalBackground.ArchiveOrderResponse_element'}
);
response_x = response_map_x.get('response_x');
return response_x.ArchiveOrderResult;
}
public String SubmitScreening(String xml) {
CriminalBackground.SubmitScreening_element request_x = new CriminalBackground.SubmitScreening_element();
CriminalBackground.SubmitScreeningResponse_element response_x;
request_x.xml = xml;
Map response_map_x = new Map();
response_map_x.put('response_x', response_x);
WebServiceCallout.invoke(
this,
request_x,
response_map_x,
new String[]{endpoint_x,
'http://www.criminalbackground.com/webservices/SubmitScreening',
'http://www.criminalbackground.com/webservices/',
'SubmitScreening',
'http://www.criminalbackground.com/webservices/',
'SubmitScreeningResponse',
'CriminalBackground.SubmitScreeningResponse_element'}
);
response_x = response_map_x.get('response_x');
return response_x.SubmitScreeningResult;
}
}
public class GetResultsResponse_element {
public String GetResultsResult;
private String[] GetResultsResult_type_info = new String[]{'GetResultsResult','http://www.w3.org/2001/XMLSchema','string','0','1','false'};
private String[] apex_schema_type_info = new String[]{'http://www.criminalbackground.com/webservices/','true','false'};
private String[] field_order_type_info = new String[]{'GetResultsResult'};
}
public class GetPackagesResponse_element {
public String GetPackagesResult;
private String[] GetPackagesResult_type_info = new String[]{'GetPackagesResult','http://www.w3.org/2001/XMLSchema','string','0','1','false'};
private String[] apex_schema_type_info = new String[]{'http://www.criminalbackground.com/webservices/','true','false'};
private String[] field_order_type_info = new String[]{'GetPackagesResult'};
}
public class ArchiveOrderResponse_element {
public String ArchiveOrderResult;
private String[] ArchiveOrderResult_type_info = new String[]{'ArchiveOrderResult','http://www.w3.org/2001/XMLSchema','string','0','1','false'};
private String[] apex_schema_type_info = new String[]{'http://www.criminalbackground.com/webservices/','true','false'};
private String[] field_order_type_info = new String[]{'ArchiveOrderResult'};
}
public class SubmitScreeningResponse_element {
public String SubmitScreeningResult;
private String[] SubmitScreeningResult_type_info = new String[]{'SubmitScreeningResult','http://www.w3.org/2001/XMLSchema','string','0','1','false'};
private String[] apex_schema_type_info = new String[]{'http://www.criminalbackground.com/webservices/','true','false'};
private String[] field_order_type_info = new String[]{'SubmitScreeningResult'};
}
public class SubmitScreening_element {
public String xml;
private String[] xml_type_info = new String[]{'xml','http://www.w3.org/2001/XMLSchema','string','0','1','false'};
private String[] apex_schema_type_info = new String[]{'http://www.criminalbackground.com/webservices/','true','false'};
private String[] field_order_type_info = new String[]{'xml'};
}
public class GetResults_element {
public String username;
public String password;
private String[] username_type_info = new String[]{'username','http://www.w3.org/2001/XMLSchema','string','0','1','false'};
private String[] password_type_info = new String[]{'password','http://www.w3.org/2001/XMLSchema','string','0','1','false'};
private String[] apex_schema_type_info = new String[]{'http://www.criminalbackground.com/webservices/','true','false'};
private String[] field_order_type_info = new String[]{'username','password'};
}
public class GetPackages_element {
public String username;
public String password;
private String[] username_type_info = new String[]{'username','http://www.w3.org/2001/XMLSchema','string','0','1','false'};
private String[] password_type_info = new String[]{'password','http://www.w3.org/2001/XMLSchema','string','0','1','false'};
private String[] apex_schema_type_info = new String[]{'http://www.criminalbackground.com/webservices/','true','false'};
private String[] field_order_type_info = new String[]{'username','password'};
}
}


 

After this, go to Setup > Remote Site Settings and add a new remote site having URL https://www.criminalbackground.com and you're all set. 

 

Once you've created the class above and added the site, here's the code to call the web service:

 

String username = '<USERNAME>';

String password = '<PASSWORD>';

CriminalBackground.ServiceSoap port = new CriminalBackground.ServiceSoap();

String result = port.GetPackages(username, password);

 

 

Hope this helps!

 

-soof 

 

 

soofsoof

BTW, here's the link to the docs that will help you create classes to callout to webservices:

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_web_services_wsdl2apex.htm

 

-soof 

rajsrajs

Hi Soof,

    Thank you for your great guidance.Now I able to explore the  webservices.The way you approached is so understandable.I tried your code to call the getpackages() method,I got the following error

 

System.CalloutException: IO Exception: Read timed out

 

Class.CriminalBackground.ServiceSoap.GetPackages: line 50, column 13 Class.criminalBackgroundVF.getContents: line 43, column 17 External entry point

for your reference Line No:50 is WebServiceCallout.invoke() in getpackage method.and also tried the variable 'timeout_x' to set the maximum time out limit(60000 ms).

 

can you please tell me where I  made mistake

 

 

Once again I thank you,

 

Raj

Message Edited by rajs on 07-13-2009 04:26 AM