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

Apex from JS

Can I call apex classes from JS which is behind a button or a link ? An example would be great. thanks 
Sure you can. You'll need to follow a few simple steps, though:

1. Mark you class as global
Mark the method that you want to use with webservice and static keywords
3. In javascript use the function sforce.apex.execute('Your_class_name' ,
'your_method_name', {arg1: <<ar1_value>>, arg2: <<arg2_value>>});

Here is an example:

global class MyWSClass{
 webService static string foo(Integer arg1, String arg2){
//your logic here
return 'Hello World';
} }


var result = sforce.apex.execute("MyWSClass" ,"foo",
{arg1: 1,
arg2: 'some data'





Thanks Shamil

I tried that but I get

sforce.apex has no properties

error, so I put it the ajax.js and got past that.
Now I get the 'No Operation available for request..., please check WSDL for the service'
Which WSDl is being referred here?

Message Edited by MDXHiker on 11-13-2008 03:31 PM
Please double check that
1. you have created apex class called MyWSClass with the code from my previous post

2. in javascript you included

before you call the Apex class method

This example perfectly works in my case.

I am using those and ofcourse I have named my class differently. But calling it with the given name correctly.
Does the WSDL of the class go somewhere ?
You can find WSDL here:
Setup -> Develop -> Apex Classes -> click 'WSDL' link next to your class name.

It would be very helpful if you could post your code here.
Have you set a developer namespace for your login? (to use managed packaging for example). If so that'll affect the fully qualified name of the class that you need to specify in the javascript. (myNamespace.myClass instead of myClass)