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
George1313George1313 

How to call external web service (callout) from Visualforce Page

I'm new to SalesForce, Apex, and Visual Pages and am trying to figure out how to call a very simple web service from a Visualforce Page. After spending several days going through tutorials, various samples and reading through these forums I feel I am very close. I've addressed all the error messages during the WSDL generation but still haven't been able to put everything together to get my much anticipated "Hello World".

 

I am working with a the very simple Microsoft Dot Net "Hello World" web service that can be accessed here: http://demo4.leaseteam.com/GeorgeTest/Service.asmx There are no inputs and the expected out put is a simple "Hello World".  This is what I have in place so far:

 

1) My site is configure under Administration Setup / Remote Site Settings

2) After some manual hacking of the WSDL with XML Spy I have WSDL generated and in an Apex Class. See below

 

//Generated by wsdl2apex

public class tempuriOrg {
public class HelloWorld_element {
private String[] apex_schema_type_info = new String[]{'http://tempuri.org/','true','false'};
private String[] field_order_type_info = new String[]{};
}
public class ServiceSoap {
public String endpoint_x = 'http://demo4.leaseteam.com/GeorgeTest/Service.asmx';
public Map<String,String> inputHttpHeaders_x;
public Map<String,String> outputHttpHeaders_x;
public String clientCert_x;
public String clientCertPasswd_x;
public Integer timeout_x;
private String[] ns_map_type_info = new String[]{'http://tempuri.org/', 'tempuriOrg'};
public String HelloWorld() {
tempuriOrg.HelloWorld_element request_x = new tempuriOrg.HelloWorld_element();
tempuriOrg.HelloWorldResponse_element response_x;
Map<String, tempuriOrg.HelloWorldResponse_element> response_map_x = new Map<String, tempuriOrg.HelloWorldResponse_element>();
response_map_x.put('response_x', response_x);
WebServiceCallout.invoke(
this,
request_x,
response_map_x,
new String[]{endpoint_x,
'http://tempuri.org/HelloWorld',
'http://tempuri.org/',
'HelloWorld',
'http://tempuri.org/',
'HelloWorldResponse',
'tempuriOrg.HelloWorldResponse_element'}
);
response_x = response_map_x.get('response_x');
return response_x.HelloWorldResult;
}
}
public class HelloWorldResponse_element {
public String HelloWorldResult;
private String[] HelloWorldResult_type_info = new String[]{'HelloWorldResult','http://www.w3.org/2001/XMLSchema','string','0','1','false'};
private String[] apex_schema_type_info = new String[]{'http://tempuri.org/','true','false'};
private String[] field_order_type_info = new String[]{'HelloWorldResult'};
}
}

 

3) I have another Apex Class written as the "controller". It is thus...

 

 

public class WSContoller

{

String output;
String input = 'detroit';

public void getRecord()

{

tempuriOrg.ServiceSoap stub = new tempuriOrg.ServiceSoap();

output= stub.HelloWorld();
//output= 'Test 2';

}

public String getOutput()

{

return output;
//return 'test me';

}

}

 

 4) My Visualforce Page is this...

 

<apex:page controller="WSContoller" tabStyle="Account">
It will be a nice to get output from the controller
<p>You belong to the {!Output} account.</p>
</apex:page>

 

Drum roll ..... I get the output without the expected "Hello World". My page shows:

 

It will be a nice to get output from the controller

You belong to the account.

 

If you've read this far, thank you and I'd appreciate any help or advise that you could give me.

 

Best Answer chosen by Admin (Salesforce Developers) 
George1313George1313

Thank you very much for your reply.  Your comments gave me enough information to question some of the code that I got from these forums and have now successfully contructed my Visualforce Page and Controller to get my expected results from my webservice.  I have changed my code to this.

 

 Visualforce page

 

<apex:page controller="WSContoller" tabStyle="Account">It will be a nice to get output from the controller<p>My webservice is now returning {!HelloWorld} !!</p></apex:page>

 

Controller

 

public class WSContoller{ public String getHelloWorld() { string MyReturn; tempuriOrg.ServiceSoap stub = new tempuriOrg.ServiceSoap(); MyReturn = stub.HelloWorld(); return MyReturn ; } }

 

 

 

 

  I now correctly get my page to display...

  It will be a nice to get output from the controller

My webservice is now returning Hello World !!

 

Thank you for your help. 

 

 


All Answers

ssikorassikora

public void getRecord() {tempuriOrg.ServiceSoap stub = new tempuriOrg.ServiceSoap();output= stub.HelloWorld();//output= 'Test 2';}

 Where are you calling the getRecord() method? Your page only calls getOutput() which returns String output which as far as I can see is set to nothing.

George1313George1313

Thank you very much for your reply.  Your comments gave me enough information to question some of the code that I got from these forums and have now successfully contructed my Visualforce Page and Controller to get my expected results from my webservice.  I have changed my code to this.

 

 Visualforce page

 

<apex:page controller="WSContoller" tabStyle="Account">It will be a nice to get output from the controller<p>My webservice is now returning {!HelloWorld} !!</p></apex:page>

 

Controller

 

public class WSContoller{ public String getHelloWorld() { string MyReturn; tempuriOrg.ServiceSoap stub = new tempuriOrg.ServiceSoap(); MyReturn = stub.HelloWorld(); return MyReturn ; } }

 

 

 

 

  I now correctly get my page to display...

  It will be a nice to get output from the controller

My webservice is now returning Hello World !!

 

Thank you for your help. 

 

 


This was selected as the best answer