It is possible to be done, but not directly through button, but with button + VF page + Apex Class. Here is what I mean:
1) Create Apex class (in my case TestClass) that collects selected records ids and run your batch:
public class TestClass {
public List<String> ids;
public TestClass(ApexPages.StandardSetController controller) {
this.ids = new List<String>();
List<sObject> records = (List<sObject>) controller.getSelected();
for (sObject record_i : records) {
this.ids.add(record_i.Id);
}
}
public void runBatch() {
//run your batch here with set of ids you have
String query = 'SELECT Description, Id FROM Account WHERE Id IN (\'' + String.join(this.ids, '\',\'') + '\')';
Database.executeBatch(new TestBatch(query), 20);
}
}
2) Create VF page (in my case TestPage) with standardController of the object you want to set your button for (in my case Account):
<apex:page standardController="Account" recordSetVar="records" extensions="TestClass" action="{!runBatch}"></apex:page>
3) Create Custom Button with below customization:
4) Add button to List View Search Layout and use.
P.S.: as you can see, there is no just direct way without VF page step, however, you can add pop-up with message that batch has scheduled. I have also added my batch example to simplify the solution.
My TestBatch class:
public class TestBatch implements Database.Batchable<sObject> {
public String query;
public TestBatch(String query) {
this.query = query;
}
public Database.QueryLocator start(Database.BatchableContext bc) {
return Database.getQueryLocator(this.query);
}
public void execute(Database.BatchableContext bc, List<Account> scope) {
for (Account acc_i : scope) {
acc_i.Description = 'I was in batch';
}
update scope;
}
public void finish(Database.BatchableContext bc) {
}
}