Salesforce - Executing Javascript through Apex Controller

13,782

Read these reference documents thoroughly first: Action Functions, Apex in Ajax, or Visualforce JavaScript Remoting.

The easiest way to do what you're asking is to include the connection.js and apex.js files on the page using an <apex:includeScript> tag (like below).

<apex:includeScript value="/soap/ajax/24.0/connection.js"/>
<apex:includeScript value="/soap/ajax/24.0/apex.js"/>

Then, put your JavaScript in a function in a <script> tag under the <page> tag.

<script>
    function invokeWebService()
    {
        var xfolder = "MyNewFolder"
        var parentid = "999999999999999"
        var myvar = sforce.apex.execute("myWS","invokeExternalWs", {folderName:xfolder,ObjectID:parentid});
        window.alert('Folder created: ' + myvar);
    }
</script>

Finally, call your function with the onclick attribute on an input button.

<button type="button" onclick="invokeWebService();" style="btn">Invoke</button>

Note.. I haven't tested any of this, but it should work with minor tweaks (I've used a similar approach many times).

Share:
13,782
user891859
Author by

user891859

Coldfusion developer of 12 years and recently started learning Salesforce.com.

Updated on July 23, 2022

Comments

  • user891859
    user891859 almost 2 years

    Im new to Salesforce in general and Im trying to decide how to handle the invoking of a SOAP webservice. Currently the webservice is being executed via AJAX when a user clicks a button on the Opportunity page. I have been asked to move the webservice invocation from the button and place it into a custom controller page. So the webservice needs to be invoked seamlessly when certain conditions are met, opposed to having the user click a button.

    I'd like to just kick off the webservice using the same ajax statement because it will save me time. Although it would seem to make more sense to invoke the webservice via Apex, but am still researching that topic. So here is my question:

    Is it possible to execute the following javascript from within an Apex controller? If so how?

    {!REQUIRESCRIPT("/soap/ajax/24.0/connection.js")}
    {!REQUIRESCRIPT("/soap/ajax/24.0/apex.js")}
    var xfolder = "MyNewFolder"
    var parentid = "999999999999999"
    var myvar = sforce.apex.execute("myWS","invokeExternalWs", {folderName:xfolder,ObjectID:parentid});
    window.alert('Folder created: ' + myvar);
    
  • Matt K
    Matt K about 12 years
    Looking at this answer might me helpful as well: stackoverflow.com/a/7715982/549141
  • user891859
    user891859 about 12 years
    Thanks for the reply. I was really hoping there was a way to execute the javascript within an Apex controller, without the use of VisualForce. The more I think about this the better sense a controller version makes. I would need the javascript fired off seamlessly and I don't want to add a page in between the current process. Anyway, thanks again for the advice and information, I will definitely be able to use this in the future.
  • Matt K
    Matt K about 12 years
    You're welcome. By the way, you don't actually need to execute JavaScript within an Apex controller to call a web service. You could just call myWS.invokeExternalWs(folderName,objectId); in Apex. I didn't realize that's what you were trying to do.
  • user891859
    user891859 about 12 years
    Exactly and that is what I am working with now. Trudging through the mud that is Apex (speaking for myself of course). :) Thanks again!