+ Start a Discussion

Custom Button to run batch class

Hi Guys,
I need your help that how can I create a custom button on list view to run the batch class.
Right now I go to developer console and click on Debug and click on Open Execute Anonymous Window and then I enter Database.executeBatch(new CreateOrderBatch(),1); and then execute it. When I execute it then it makes a API call.
Is there a way I can just click on the Custom Button on List View and it makes API calls instead of going Developer Console?

You can use Ajax toolit to execute anonymous apex from Custom button, like below code snippet: myClass is class name and "makeContact" is method name.
sforce.apex.execute("myClass" , "makeContact",
{lastName:"Smith", a:account});
You can also have a look at Question 157 here. (http://www.jitendrazaa.com/blog/salesforce/apex-interview-question-salesforce-part-16/)

Yeah you can do it using webservice methods in apex

Apex Class:

​global with sharing class ValidateBefore {
webService static String validate(String param) {
      Database.executeBatch(new CreateOrderBatch(),1); // this is your call

Custom Button:


var result = sforce.apex.execute("ValidateBefore","validate",{"{!Account.Name}"});
if(result == 'OK') {
window.location = '/{!Account.Id}';
} else {
James PeckJames Peck
Depending on what you are trying to do, you should be able to create a custom button in visualforce to run whatever Apex code you're trying to run.