Calling Apex method from a Custom Button

To call Apex method from Custom Button, follow the below steps

1. Create a custom button and enter the following code

Syntax:

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

sforce.apex.execute("Class_Name","Method_Name",{parameter_Name:"value"});
location.reload(true);

Code:

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

var r = confirm("Record will not be able to edit after sending it to Approval Process.\nAre you sure want to send it for approval process?");
if(r == true)
{
    sforce.apex.execute("memberApproval","callApproval",{localId:"{!Member__c.Id}"});
    alert("Sent it for Approval");
}



2. Create a apex class with following code

Syntax:

global class ClassName {
    webservice static void methodName(DataType variable) 
                …………………        
                …………………
                …………………
    }
}

Code:

global class memberApproval {
    webservice static void callApproval(Id localId) 
                Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
                req1.setComments('Submitted for Approval');
                req1.setObjectId(localId);
                Approval.ProcessResult res = Approval.Process(req1);                  
    }
}

Output:



13 comments:

  1. HOW CAN I WRITE A TEST CLASS FOR THIS CLASS TO MOVE IT TO PRODUCTION??

    ReplyDelete
  2. Test class for this is

    @isTest
    private class testClass
    {
    static testMethod void test()
    {
    Member__c m = new Member__c();
    m.Name = 'Sample';
    m.Email_Address__c = 'magulancse24@gmail.com';
    m.Age__c = 25;
    insert m;
    memberApproval.callApproval(m.Id);
    }
    }

    ReplyDelete
  3. Hi

    I want to call normal apex class from custom button,here you are calling web service method

    ReplyDelete
  4. class abc{

    Submit(){

    }
    }

    this submit button i want to call
    can you suggest me how to call
    now am calling like as you but am getting error

    ReplyDelete
    Replies
    1. Only like this we can call apex method. Normal classes cannot be called.

      Cheers!!!

      Delete
  5. i want an alert message on the click of save button . how can i introduce this quote in my class

    ReplyDelete
    Replies
    1. Check this

      http://www.infallibletechie.com/2014/05/alert-button-in-visualforce-page-in.html

      Cheers!!!

      Delete
  6. hi,

    i want this process on sending email means i have created a button in the custom detailed page like send email.

    please share how should i write the apex class.

    ReplyDelete
  7. hi i was trying to write visual for ce page for the below quote. Too many errors. any help would be appreciated.

    ReplyDelete
  8. i'm unable to add my page. how do i add it

    ReplyDelete
  9. Has anyone ever returned a value (e.g., new record id) back to the Javascript code from the Apex code?

    Having trouble doing this because the Class is a Webservice class that must be void.

    Thanks!

    ReplyDelete
    Replies
    1. Hi Jeff,

      We can return the value from webservice method in this scenario.
      var ldData = sforce.apex.execute("ClassName","MethodName",{Param:value});
      //Retrun value we have to read, if it return lead record based on Id.
      var ldId = ldData[0].Id;
      var ldPhone = ldData[0].Phone;

      Method: webservice Static Lead getLeadRecord(id leadId){
      lead ld = [SELECT id, Phone FROM Lead where id=:leadId];
      return ld;
      }

      Please let me know if you find any issue.

      Delete
  10. Hi, I want to create records of a object through this sforce.apex.execute method and want to return the Id of the newly created record in javascript,then want to use this id to call a VF page.

    ReplyDelete