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
mohd atifmohd atif 

what is the further step to activate the batch apex class !!!

I have written this class which is select the query of the Accounts and making an csv file.

cloud u please suggest me what to do to see the working of batch apex class,



 global class ConvertAccounts implements  Database.Batchable<sObject>

        global final string query;
        global ConvertAccounts (String q)
       query = q;

global Database.QueryLocator start(Database.BatchableContext BC)

           return Database.getQueryLocator(query);

global void execute(Database.BatchableContext BC, List<Account> scope)
        folder f=[select id, name  from  folder  where Name='Accounts Details'];
        Document d = new Document();
        d.Name = 'Accounts CSV';;
        string header = 'id , AccountNumber , name , AnnualRevenue , BillingCity , BillingCountry , Description , CreatedDate , ModifiedDate \n';
        string finalstr = header ;
     for(Account a: scope)
       string recordString =','+a.AccountNumber+','+a.Name+','+a.AnnualRevenue+','+a.BillingCity+','+a.BillingCountry+','+a.Description+','+a.CreatedDate+','+a.LastModifiedDate +'\n';
       finalstr = finalstr +recordString;
    d.Body = Blob.valueOf(finalstr);
    d.ContentType = 'text/csv';
    d.Type = 'csv';
    insert d;
    update d;

global void finish(Database.BatchableContext BC)
  // Get the ID of the AsyncApexJob representing this batch job  
  // from Database.BatchableContext.    
  // Query the AsyncApexJob object to retrieve the current job's information.  

    AsyncApexJob a = [Select Id, Status, NumberOfErrors, JobItemsProcessed,
    TotalJobItems, CreatedBy.Email
    from AsyncApexJob where Id =:BC.getJobId()];

  // Send an email to the Apex job's submitter notifying of job completion.  
     Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
     String[] toAddresses = new String[] {'!Account.EMail'};
     mail.setSubject('Apex Sharing Recalculation ' + a.Status);
     mail.setPlainTextBody('The batch Apex job processed ' + a.TotalJobItems +
     'batches with '+ a.NumberOfErrors + ' failures.');

     Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });





You need something to kick it off. Either a scheduled APEX call or even a kick from the System Log. Something like:


ConvertAccounts ca = new ConvertAccounts();

ca.query = 'select id from account where YOUR CONDITIONS HERE' ;

ID batchprocessId = Database.executeBatch (ca) ;


Hope this helps,


Dan Smith