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
Ben MorchBen Morch 

Call Apex class from custom button/link with JavaScript

Hello All,

I am trying to call a custom class from a link on the Account object using Execute JavaScript.  Ultimately I want to call the SecurityCheck class with a parameter of the account Id to instantiate the "sec" variable.  Then set the "show" variable to the results of sec.showPage().  I am doing something wrong here as I am not very good with JavaScript.  Once I have show = true || false then I will direct to one of two URLs.

My questions are:
1) How do I properly instantiate a variable in JavaScript by calling a class constructor with a parameter?
2) How do I then call a method of that class (class.showPage)?
3) Is there a better way to do this?  Like JS on the VF page that I am directing to?
 
{!REQUIRESCRIPT("/soap/ajax/31.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/31.0/apex.js")}

try{
    var acct = '{!Account.Id}';
    var sec = sforce.apex.execute("SecurityCheck","SecurityCheck",{acct:acct});

    var show = sforce.apex.execute("SecurityCheck","sec.showPage",{});  

    //alert(show);
}
catch(err){
    var txt = "Error: ";
    txt+=err.description+" 11";
    alert(txt);
}

If (show == true) {
    window.open('ThisURL');
} else {
    window.open('ThatURL');
}

Thanks all for taking a look.
NekosanNekosan

Define class with webservice 
 WebService static void updateStatus(Id RecordID, String status) {      
//add logic here
}

In javascript call similar to following syntax
sforce.apex.execute("Utils","updateStatus",{ID:"{!object.Id}", String:'Submit'});
location.reload(true); 
 
NekosanNekosan
You can also refer to 
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_and_ajax.htm.
Hope this helps.