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>();
       system.debug('---->>>>-');
         list<ApexClass> ClassNames=new list<ApexClass>();
        options.add(new selectoption('--None--','--None--'));
        ClassNames=[select Id,name,body from ApexClass];
        for(ApexClass ac:ClassNames)
        { 
           if(ac.body.contains('batch')){
           system.debug('-----'+ac.name);
           options.add(new selectoption(ac.Id,ac.name));
           instanceMap.put(ac.name, ac);
           system.debug('--instanceMap--'+instanceMap);
      }
     }
     
   return options;
 }
 
 public void run(){
     system.debug('--selectedclasses--'+selectedclass);
     system.debug('--query-->'+inputquery);
    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="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" lang="en" >
    <apex:stylesheet value="{!URLFOR($Resource.SLDS24, 'assets/styles/salesforce-lightning-design-system-vf.min.css')}" />
 <apex:form>
     
      <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>
</div>
              <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:selectList>
       </div>   
    </div>
    <apex:pageblock >
     
         <apex:pageBlockSection >
             <apex:inputTextArea label="Query"></apex:inputTextArea>
        </apex:pageBlockSection>
     </apex:pageblock>
 </apex:form>
  </html>
</apex:page>
Charisse de BelenCharisse de Belen
Hello,

Check out the Type Class and its forName and newInstance methods: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_type.htm

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!