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
Sharath K MSharath K M 

Can the batch size of batch class can be too small?

What is the least number of records we can have on a batch? I'm trying to update a single lead record using batch class. Am I allowed to do so? Coz I don't want to update all the lead records.
Best Answer chosen by Sharath K M
Suraj Tripathi 47Suraj Tripathi 47
Hi Sharath,

Yes we can run batch class for single record also and it can vary from 1 to 2000 but the default size of batch class is 200
if you don't assign size then it will automatically consider it 200.

If you find your Solution then mark this as the best answer. 

Thank you!

Regards 
Suraj Tripathi



 

All Answers

AbhinavAbhinav (Salesforce Developers) 
Yes Batch size can be as small as 1 and max upto 2000 . If you haven't mention any batch size parameters while executing batch class it will take batch size as 200(edited typo).

You wont get any error if you are having single record. However general purpose of batch class is process things when we have records in bulk.

If it helps mark it as best answer.

Thanks!
Suraj Tripathi 47Suraj Tripathi 47
Hi Sharath,

Yes we can run batch class for single record also and it can vary from 1 to 2000 but the default size of batch class is 200
if you don't assign size then it will automatically consider it 200.

If you find your Solution then mark this as the best answer. 

Thank you!

Regards 
Suraj Tripathi



 
This was selected as the best answer
Sharath K MSharath K M
Hi Suraj,
Thanks for the quick reply. I tried to perform the update operation on lead. Below is the code

global class LeadBatchUpdate implements Database.Batchable<sObject> {
    
   global Database.QueryLocator start(Database.BatchableContext bc)
   {
      return Database.getQueryLocator([Select AnnualRevenue from Lead LIMIT 1]); 
   }
    
    global void execute(Database.BatchableContext bc,List<Lead> llead)
    {
        for(Lead l : llead)
        {
            l.AnnualRevenue = 970751;
        }
    }
    
    global void finish(Database.BatchableContext bc)
    {
       Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddress= new String[]{'yourmailid@test.com'};
            mail.setToAddresses(toAddress);
            mail.setSubject('Batch Updated');
            mail.setPlainTextBody('Congartulations!!!! Lead Updated');
        Messaging.sendEmail(new Messaging.SingleEmailMessage[]{mail});
    }
    

}

When I ran the above code, I was able to run this code without any errors but the value of AnnualRevenue never got updated. I even received a mail to my email, since I've provided my mail_id in the finish method. Could you please tell me why or what is wrong in the above code?

Thanks in Advance :) 
Suraj Tripathi 47Suraj Tripathi 47
Hi Sharath, 

sorry to reply you late your code is perfectly fine and you just need to update the list llead. 
replace these lines by my code.
 
global void execute(Database.BatchableContext bc,List<Lead> llead)
    {
        for(Lead l : llead)
        {
            l.AnnualRevenue = 970751;

        }
Database.SaveResult[] res = Database.update(llead,false);
    }

I hope it should work.

Thank you!
Regards,
Suraj Tripathi