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
nksfnksf 

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?

Thanks!
JitendraJitendra
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/)

 
gurditta.garg@gmail.comgurditta.garg@gmail.com
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:

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

var result = sforce.apex.execute("ValidateBefore","validate",{"{!Account.Name}"});
if(result == 'OK') {
window.location = '/{!Account.Id}';
} else {
alert(result);
}
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.