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
smitha george 5smitha george 5 

Executing the batch class

Hi,
I am new to salesforce, Please help me how to execute this batch class.
Thanks in Advance.


global class AccountBatch implements Database.batchable<sobject>
{
  global database.querylocator start(database.batchablecontext bc)
    {
       string query = 'select id, name from Account';
       return database.getquerylocator(query);
     }
   global void execute(database.batchablecontext bc, list<account>scope)
     {
     list<account> acc= new list<account>();
      for(account a:acc)
      {
        a.name= 'Mr' +a.name;
        acc.add(a);
        }
        
      update acc;
    }
    global void finish(database.batchablecontext bc)
    {
    }
    }
 
Best Answer chosen by smitha george 5
sfdcMonkey.comsfdcMonkey.com
update your batch class with below code : use below code

play for look on scope not acc 
global class AccountBatch implements Database.batchable<sobject>
{
  global database.querylocator start(database.batchablecontext bc)
    {
       string query = 'select id, name from Account';
       return database.getquerylocator(query);
     }
   global void execute(database.batchablecontext bc, list<account>scope)
     {
     list<account> acc= new list<account>();
      for(account a:scope)
      {
        a.name= 'Mr ' +a.name;
        acc.add(a);
        }
        
      update acc;
    }
    global void finish(database.batchablecontext bc)
    {
    }
    }
 Let me inform if it helps you and kindly mark it best answer if it helps you so it make proper solution for others
    thanks 
 

All Answers

sfdcMonkey.comsfdcMonkey.com
In the Developer Console, click Debug > Open Execute Anonymous Window
Type the following Apex code:

AccountBatch batch = new AccountBatch();
Database.executeBatch(batch);

Click Execute

for reference : https://help.salesforce.com/articleView?id=000171199&language=en_US&r=https%3A%2F%2Fwww.google.co.in%2F&type=1


i hope it helps you.
      Let me inform if it helps you and kindly mark it best answer if it helps you so it make proper solution for others
    thanks 
http://sfdcmonkey.com  (http://sfdcmonkey.com )
Saravana Muthu 8Saravana Muthu 8
Hi,

Goto apex class and click schedule apex button then you can see this class.

You can choose to schedule at a preferred date and time.

Please let me know if it helps.

Please don't forget to mark this as solved if it's resolved.

Thanks,
Saravana
smitha george 5smitha george 5
Hi Piyush,

After executing the calss still account names remain the same.It doesnt change.

Thanks
Smitha
sfdcMonkey.comsfdcMonkey.com
update your batch class with below code : use below code

play for look on scope not acc 
global class AccountBatch implements Database.batchable<sobject>
{
  global database.querylocator start(database.batchablecontext bc)
    {
       string query = 'select id, name from Account';
       return database.getquerylocator(query);
     }
   global void execute(database.batchablecontext bc, list<account>scope)
     {
     list<account> acc= new list<account>();
      for(account a:scope)
      {
        a.name= 'Mr ' +a.name;
        acc.add(a);
        }
        
      update acc;
    }
    global void finish(database.batchablecontext bc)
    {
    }
    }
 Let me inform if it helps you and kindly mark it best answer if it helps you so it make proper solution for others
    thanks 
 
This was selected as the best answer
smitha george 5smitha george 5
Hi Piyush,

It executed fine.Thank you very much.
Can you tell me what is the use of scope ?

Thanks,
Smitha
sfdcMonkey.comsfdcMonkey.com
Smitha , scope is just a variable name which is having list of accounts [ list<account> ], and this list of accounts was set (return to execute() method) by start() method in our batch class., and then we are do a loop on all accounts and update the name :)
Hope its helps you to understand
Thanks
smitha george 5smitha george 5
Yes Piyush.
Thank you so much.