Activity Forums Salesforce® Discussions How to execute Apex from Custom button or Javascript?

  • Satya

    Member
    March 22, 2016 at 1:16 pm

    We can call any apex method through JavaScript by following function:
    sforce.apex.execute("class_Name","Method_Name",{Parameter_Name:Parameter_Value});

  • Shubham

    Member
    March 22, 2016 at 1:23 pm

    In this case you can use apex action function.

      Example

    VF Page---
    <apex:page Controller="myController">
    <apex:form>
    <apex:actionFunction name="callAction" action="{!myFunction}" />
    <button type="button" onclick="callAction()">Call My Function</button>
    <apex:outputPanel rendered="{!panel}">
    Controller function Called.
    </apex:outputPanel>
    </apex:form>
    </apex:page>

    Apex Class---
    public class myController {
    public Boolean panel {get;set;}
    public myController(){
    panel = false;
    }
    public void myFunction(){
    panel = true;
    }
    }

  • Ajit

    Member
    March 23, 2016 at 11:36 am

    {!requireScript("/soap/ajax/20.0/connection.js")}
    {!requireScript("/soap/ajax/20.0/apex.js")}

    retStr = sforce.apex.execute("Testapex", "method()",{});

    the class which you are calling must be a global class

    global Testapex{

    global void method(){

    }
    }

  • Parul

    Member
    September 19, 2018 at 9:20 pm

    Hi,

    global class MyClass
    {
    webservice static void myMethod() // you can pass parameters
    {
    // Do something
    }
    }

    Now create a custom button:

    Goto --> Setup --> Object --> Buttons, links and Actions section-->Click New Button or Link.

    Enter the Name of the button
    Behaviour : Execute Javascript
    Content source : On-Click Javascript

    use below JS:

    {!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}
    {!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")}

    if({!Account__c.Name}!=Null)
    {
    sforce.apex.execute("SampleClass","sampleMethod",{}"});
    alert("This is {!Account__c.Name}");
    }

    Thanks

  • shariq

    Member
    September 19, 2018 at 10:21 pm

    Hi,

    You need to create action function in vf page which will call apex, action function will be called by js.

    So indirectly you are calling apex from js.

    Hope this helps.

  • shariq

    Member
    September 20, 2018 at 11:27 pm

    Hi,

    Example -

    You have to make use of AJAX toolkit to call the method in Apex Controller.

    {!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}
    {!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")}
    //sforce.apex.execute("classname","methodname","parameters seperated by comma");
    sforce.apex.execute("classname","removeItemFromEbayList");
    The consideration for this is that the class and the methods should be global and webservice. so you should modify the access modifier in your class to global and method from public to webservice.

    Hope this helps.

  • Parul

    Member
    September 21, 2018 at 3:56 am

    Hi,

    Refer the above example to execute Apex using Javascript and custom button. It provide you solution

     

    Thanks

Log In to reply.

Popular Salesforce Blogs