+ Start a Discussion
p1errep1erre 

Visualforce: how to call external web service?

I have following issue:
when I create a new Account I first would like to fetch the Account data from an external web service
and populate the Account with the result of my web service call. My idea is to add a button (GetAccountData) that would trigger the
web service (with a Controller ?), get the response and display the data on the Account Tab.
is there a way  in Visualforce to add such a functionality ?

reshmareshma

The sample apex page

 

<apex:page controller="Temperature">

<apex:form>

<h1>Congratulations</h1>

This is your new Page

<apex:commandButton action="{!recordReplicator}" value="Next"/>

The output webservice is {!output}

</apex:form>

</apex:page>

Controller

public class Temperature

{

Double output;

public void recordReplicator()

{

webservicesDaehostingComTemperature.TemperatureConversionsSoap stub = new webservicesDaehostingComTemperature.TemperatureConversionsSoap();

Double nFahrenheit=129;

Double nWindSpeed=20;

System.debug('In temperature Trigger');

output= stub.WindChillInFahrenheit(nFahrenheit,nWindSpeed);

System.Debug('output'+output);

}

public double getOutput()

{

return output;

}


}

This call an external webservice the apex class is

webservicesDaehostingComTemperature

//Generated by wsdl2apex

public class webservicesDaehostingComTemperature {
    public class WindChillInCelciusResponse_element {
        public Double WindChillInCelciusResult;
        private String[] WindChillInCelciusResult_type_info = new String[]{'WindChillInCelciusResult','http://www.w3.org/2001/XMLSchema','decimal','1','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://webservices.daehosting.com/temperature','true'};
        private String[] field_order_type_info = new String[]{'WindChillInCelciusResult'};
    }
    public class CelciusToFahrenheitResponse_element {
        public Double CelciusToFahrenheitResult;
        private String[] CelciusToFahrenheitResult_type_info = new String[]{'CelciusToFahrenheitResult','http://www.w3.org/2001/XMLSchema','decimal','1','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://webservices.daehosting.com/temperature','true'};
        private String[] field_order_type_info = new String[]{'CelciusToFahrenheitResult'};
    }
    public class WindChillInFahrenheitResponse_element {
        public Double WindChillInFahrenheitResult;
        private String[] WindChillInFahrenheitResult_type_info = new String[]{'WindChillInFahrenheitResult','http://www.w3.org/2001/XMLSchema','decimal','1','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://webservices.daehosting.com/temperature','true'};
        private String[] field_order_type_info = new String[]{'WindChillInFahrenheitResult'};
    }
    public class WindChillInCelcius_element {
        public Double nCelcius;
        public Double nWindSpeed;
        private String[] nCelcius_type_info = new String[]{'nCelcius','http://www.w3.org/2001/XMLSchema','decimal','1','1','false'};
        private String[] nWindSpeed_type_info = new String[]{'nWindSpeed','http://www.w3.org/2001/XMLSchema','decimal','1','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://webservices.daehosting.com/temperature','true'};
        private String[] field_order_type_info = new String[]{'nCelcius','nWindSpeed'};
    }
    public class FahrenheitToCelcius_element {
        public Double nFahrenheit;
        private String[] nFahrenheit_type_info = new String[]{'nFahrenheit','http://www.w3.org/2001/XMLSchema','decimal','1','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://webservices.daehosting.com/temperature','true'};
        private String[] field_order_type_info = new String[]{'nFahrenheit'};
    }
    public class FahrenheitToCelciusResponse_element {
        public Double FahrenheitToCelciusResult;
        private String[] FahrenheitToCelciusResult_type_info = new String[]{'FahrenheitToCelciusResult','http://www.w3.org/2001/XMLSchema','decimal','1','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://webservices.daehosting.com/temperature','true'};
        private String[] field_order_type_info = new String[]{'FahrenheitToCelciusResult'};
    }
    public class TemperatureConversionsSoap {
        public String endpoint_x = 'http://webservices.daehosting.com/services/TemperatureConversions.wso';
        private String[] ns_map_type_info = new String[]{'http://webservices.daehosting.com/temperature', 'webservicesDaehostingComTemperature'};
        public Double WindChillInFahrenheit(Double nFahrenheit,Double nWindSpeed) {
            webservicesDaehostingComTemperature.WindChillInFahrenheit_element request_x = new webservicesDaehostingComTemperature.WindChillInFahrenheit_element();
            webservicesDaehostingComTemperature.WindChillInFahrenheitResponse_element response_x;
            request_x.nFahrenheit = nFahrenheit;
            request_x.nWindSpeed = nWindSpeed;
            Map<String, webservicesDaehostingComTemperature.WindChillInFahrenheitResponse_element> response_map_x = new Map<String, webservicesDaehostingComTemperature.WindChillInFahrenheitResponse_element>();
            response_map_x.put('response_x', response_x);
            WebServiceCallout.invoke(
              this,
              request_x,
              response_map_x,
              new String[]{endpoint_x,
              '',
              'http://webservices.daehosting.com/temperature',
              'WindChillInFahrenheit',
              'http://webservices.daehosting.com/temperature',
              'WindChillInFahrenheitResponse',
              'webservicesDaehostingComTemperature.WindChillInFahrenheitResponse_element'}
            );
            response_x = response_map_x.get('response_x');
            return response_x.WindChillInFahrenheitResult;
        }
        public Double CelciusToFahrenheit(Double nCelcius) {
            webservicesDaehostingComTemperature.CelciusToFahrenheit_element request_x = new webservicesDaehostingComTemperature.CelciusToFahrenheit_element();
            webservicesDaehostingComTemperature.CelciusToFahrenheitResponse_element response_x;
            request_x.nCelcius = nCelcius;
            Map<String, webservicesDaehostingComTemperature.CelciusToFahrenheitResponse_element> response_map_x = new Map<String, webservicesDaehostingComTemperature.CelciusToFahrenheitResponse_element>();
            response_map_x.put('response_x', response_x);
            WebServiceCallout.invoke(
              this,
              request_x,
              response_map_x,
              new String[]{endpoint_x,
              '',
              'http://webservices.daehosting.com/temperature',
              'CelciusToFahrenheit',
              'http://webservices.daehosting.com/temperature',
              'CelciusToFahrenheitResponse',
              'webservicesDaehostingComTemperature.CelciusToFahrenheitResponse_element'}
            );
            response_x = response_map_x.get('response_x');
            return response_x.CelciusToFahrenheitResult;
        }
        public Double FahrenheitToCelcius(Double nFahrenheit) {
            webservicesDaehostingComTemperature.FahrenheitToCelcius_element request_x = new webservicesDaehostingComTemperature.FahrenheitToCelcius_element();
            webservicesDaehostingComTemperature.FahrenheitToCelciusResponse_element response_x;
            request_x.nFahrenheit = nFahrenheit;
            Map<String, webservicesDaehostingComTemperature.FahrenheitToCelciusResponse_element> response_map_x = new Map<String, webservicesDaehostingComTemperature.FahrenheitToCelciusResponse_element>();
            response_map_x.put('response_x', response_x);
            WebServiceCallout.invoke(
              this,
              request_x,
              response_map_x,
              new String[]{endpoint_x,
              '',
              'http://webservices.daehosting.com/temperature',
              'FahrenheitToCelcius',
              'http://webservices.daehosting.com/temperature',
              'FahrenheitToCelciusResponse',
              'webservicesDaehostingComTemperature.FahrenheitToCelciusResponse_element'}
            );
            response_x = response_map_x.get('response_x');
            return response_x.FahrenheitToCelciusResult;
        }
        public Double WindChillInCelcius(Double nCelcius,Double nWindSpeed) {
            webservicesDaehostingComTemperature.WindChillInCelcius_element request_x = new webservicesDaehostingComTemperature.WindChillInCelcius_element();
            webservicesDaehostingComTemperature.WindChillInCelciusResponse_element response_x;
            request_x.nCelcius = nCelcius;
            request_x.nWindSpeed = nWindSpeed;
            Map<String, webservicesDaehostingComTemperature.WindChillInCelciusResponse_element> response_map_x = new Map<String, webservicesDaehostingComTemperature.WindChillInCelciusResponse_element>();
            response_map_x.put('response_x', response_x);
            WebServiceCallout.invoke(
              this,
              request_x,
              response_map_x,
              new String[]{endpoint_x,
              '',
              'http://webservices.daehosting.com/temperature',
              'WindChillInCelcius',
              'http://webservices.daehosting.com/temperature',
              'WindChillInCelciusResponse',
              'webservicesDaehostingComTemperature.WindChillInCelciusResponse_element'}
            );
            response_x = response_map_x.get('response_x');
            return response_x.WindChillInCelciusResult;
        }
    }
    public class WindChillInFahrenheit_element {
        public Double nFahrenheit;
        public Double nWindSpeed;
        private String[] nFahrenheit_type_info = new String[]{'nFahrenheit','http://www.w3.org/2001/XMLSchema','decimal','1','1','false'};
        private String[] nWindSpeed_type_info = new String[]{'nWindSpeed','http://www.w3.org/2001/XMLSchema','decimal','1','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://webservices.daehosting.com/temperature','true'};
        private String[] field_order_type_info = new String[]{'nFahrenheit','nWindSpeed'};
    }
    public class CelciusToFahrenheit_element {
        public Double nCelcius;
        private String[] nCelcius_type_info = new String[]{'nCelcius','http://www.w3.org/2001/XMLSchema','decimal','1','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://webservices.daehosting.com/temperature','true'};
        private String[] field_order_type_info = new String[]{'nCelcius'};
    }
}
 
 
just try click next button then the result is displayed.
p1errep1erre
Hi Reshma

thanks very much for your sample code.
I have copied the classes and the page and could compile them successfully.

Now, when i press the NEXT button i get following visualforce error:

IO Exception: Unauthorized endpoint, please check Setup-&gt;Security-&gt;Remote site settings. endpoint = http://webservices.daehosting.com/services/TemperatureConversions.wso

Do i have to do define some settings within salesforce to enable the access to the external called web service?


p1errep1erre
Hi Reshma

ok, after having defined the web service in Remote Site Setting as follows it worked!!

Remote Site NameTemperatureConversions
Remote Site URLhttp://webservices.daehosting.com


Thanks a lot for your quick and very detailed response

/p1erre
reshmareshma
ya i missed this point
SadminSadmin
Hi Reshma,
 
I am also working on a similar requirement.How can i get the wsdl for the example wsdl2apex code ,which you have provided here?
 
Thanks
 
rawiswarrawiswar
the wsdl is available in the website itself ... (the same as the new remote site you have added).
OR, you could also generate it from the source code view page (Click on Generate WSDL in the Apex code view page).

bhaveshjogibhaveshjogi

Hi Reshma,

 

Where is the web services of this example?

wixxeywixxey

Hello Reshma

I am new to VF ... i have copied your code and even use the wsdl2apex method .... but it is generating this error

 

Error: Compile Error: Method does not exist or incorrect signature: WebServiceCallout.invoke(webservicesDaehostingComTemperature.TemperatureConversionsSoap, webservicesDaehostingComTemperature.WindChillInFahrenheit_element, MAP<String,webservicesDaehostingComTemperature.WindChillInFahrenheitResponse_element>, LIST<String>) at line 52 column 13

 

Kindly help me ...

mnz123mnz123
Can anyone EXPLAIN, what's going on here? I could run this example with a visualforce page showing me a temperature value on clicking a NEXT button.