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

Executing Javascript through Apex Controller

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?


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



I don't understand why you can't continue having your page do it when conditions are met.  You can have portions of your code rerender to cause the JS to fire when conditions are met if thats what you must do, but to be honest I would just take the time to learn how to have the actual Apex do the Webservice call.


Thanks for the reply.


Got another question about going with the Apex version. The WSDL looks to have been uploaded to our Saleforce instance at some time in the past. Im guessing since there is a class to that WSDL that I don't need to make an HTTP that correct? I would assume I could just refer to the class and it would make the WSDL call without an HTTP request.......  


I also considered using wsdl2apex to create another class from the uploaded WSDL, but that seems to be overkill. 


What is the best way to use the Class or associated WSDL?


Here is an example I this on the right track?  This method seems awkward to me. 


myWS.invokeExternalWs dns = new myWS.invokeExternalWs();
dns.invokeExternalWs.folderName = 'MyNewFolder';
dns.invokeExternalWs.ObjectID = '99999999999999999999';




Never seen that way of doing it.  it's been awhile since I've done the Webcallouts, but I would recommending just using the wsdl to apex.