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
nelloCnelloC 

javascript function to callout to external webservice

I need a javascript function that I can call from within a visualforce page that will make a call out to an external web service and return the result. The reason for this is that the result needs to be passed back to a calling javascript method rather than being rendered on the page.

What's the best way is acheive this?

- By using actionFunction and calling the actionFunction method from my proposed javascript method. I have attempted this option and the call out from the apex class method works fine but I'm not sure how to return the result to the calling javascript method. (i.e. I have a javascript method calling the actionFunction js method, passing in appropriate parameters, but unable to receive back a result.)

- Or by using Ajax Proxy? But was this designed for use within s-controls?

- Or any other suggestions gratefully received..

Message Edited by nelloC on 11-29-2009 12:28 PM
Best Answer chosen by Admin (Salesforce Developers) 
Cool_DevloperCool_Devloper

Nello,

Why don't you make the callout from Ajax toolkit within the JS!!

That would certainly work with S-Control as well as VF page.

You can call the class method and store the returned results within the script itself.

Cool_D

All Answers

Cool_DevloperCool_Devloper

Nello,

Why don't you make the callout from Ajax toolkit within the JS!!

That would certainly work with S-Control as well as VF page.

You can call the class method and store the returned results within the script itself.

Cool_D

This was selected as the best answer
gm_sfdc_powerdegm_sfdc_powerde

>but I'm not sure how to return the result to the calling javascript method.

Add a rerender attribute to actionfunction, which would make it an AJAX function.  The ID of the outputPanel that you specify as "rerender" will be refreshed by calling your actionFunction.  So in the backend just make sure that you populate one or more instance variables with your callout result and bind these variables as hiddent variables (inputHidden) in the  output panel.

 

You would also need an oncomplete attribute on your actionFunction which invokes a Javascript function that can process the results from hidden field(s).   If this is not clear, please read Visualforce documentation on actionFunction and its rerender and oncomplete attributes.

nelloCnelloC

Thanks for your responses. As I need the function that does the call to also return the response, I went for the Ajax tookit option. It's worth noting a problem I got hung up on here. I was receiving back was a text response, the xml having been stripped out, and I couldn't work out why. Completely removing the remoteFuntion parameter mimeType (i.e. mimeType: "text/xml" ) solved this problem.

 

I assume another option could have been to call an Apex webservice (which would perform the call out) using sforce.apex.execute. Is that right?

Message Edited by nelloC on 11-30-2009 09:38 AM
Cool_DevloperCool_Devloper

That is correct Nello!

in fact, that's what i meant on my earlier post;)

Cool_D

nelloCnelloC

I've switched to making the call via an Apex webservice (as suggested) and all's working well... :)

Thanks for the info.

shrey.tyagi88@tcs.comshrey.tyagi88@tcs.com
Hi Nello,
     I am trying to do something similiar here.I am trying to make an external web service callout from javascript  , so that I can bypass the size limitations that apex enforces. Can you please let me know whether this worked for you or not? If you can give me some pointers on this , that would be great!!!

Thanks 
Shrey Tyagi