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
Sree SalesforceSree Salesforce 

I have a requirement. where I need to be able to pass the name of a batch apex class . I am passing class name as a string. The function will call Database.ExecuteBatch using the string as the name of the class.

But i unable to pass the string as a class name in Db.executebatch();
public class DynamicBatchClass {
    public string selectedclass{get;set;}
    public string classname{get;set;}
    public string inputquery{get;set;}
    public DynamicBatchClass(){
    public list<selectoption> getclassnameslist ()
        map<string,sobject> instanceMap=new map<string,sobject>();
        list<selectoption> options=new list<selectoption>();
         list<ApexClass> ClassNames=new list<ApexClass>();
        options.add(new selectoption('--None--','--None--'));
        ClassNames=[select Id,name,body from ApexClass];
        for(ApexClass ac:ClassNames)
           options.add(new selectoption(ac.Id,;
           instanceMap.put(, ac);
   return options;
 public void run(){
    ApexClass newAp = [select id, name from apexclass where id=:selectedclass];
    string s = newap.Name;
    Database.executeBatch(new s() ,50);

Any one help me to pass the classname

VF :- <apex:page controller="DynamicBatchClass" showHeader="false" standardStylesheets="false" sidebar="false" docType="html-5.0" applyBodyTag="False" applyHtmlTag="False">    
   <html xmlns="" xmlns:xlink="" lang="en" >
    <apex:stylesheet value="{!URLFOR($Resource.SLDS24, 'assets/styles/salesforce-lightning-design-system-vf.min.css')}" />
      <div class="slds" role="banner">
      <div class="slds-grid">
             <div class="slds-form-element">
  <label class="slds-form-element__label" for="text-input-01">Input Label</label>
  <div class="slds-form-element__control">
    <input type="text" id="text-input-01" class="slds-input" placeholder="Placeholder Text" />
              <div class="slds-text-heading--large">Select Batch Class</div>
             <apex:selectList value="{!selectedclasses}" multiselect="false" size="1" label="Select Batch Class">
                 <apex:selectOptions value="{!classnameslist}"></apex:selectOptions>
    <apex:pageblock >
         <apex:pageBlockSection >
             <apex:inputTextArea label="Query"></apex:inputTextArea>
Charisse de BelenCharisse de Belen

Check out the Type Class and its forName and newInstance methods:

I hope this helps!
Sree SalesforceSree Salesforce
Type classType = Type.forName(selectedclass);
    Database.Batchable<sobject> instance = (Database.Batchable<sobject>)classType.newInstance();
    Database.executeBatch((Database.Batchable<sObject>)instance, 100); 
Charisse de BelenCharisse de Belen
Nice! If that information helped you, please select the Best Answer so this question can be marked Solved. Thanks!