+ Start a Discussion
Anudeep BAnudeep B 

Batch Apex issue.

i am trying to execute below code. where updating a site field in account object.


global class Accbtchstate implements database.batchable<sobject>
{
global database.querylocator start(database.batchablecontext bc)
{
string state = 'select id,name from account';
return database.getquerylocator(state);
}
global void execute(database.batchablecontext bc, list<account> scope)
{
list<account> acc = new list<account>();
for(account a:acc)
{
 a.Site = 'No nedd';
 acc.add(a);
 }
update acc;
}
    
global void finish(database.batchablecontext bc)
{
   
}
}

executing using this:  
1)  Accbtchstate BC = new Accbtchstate();
Database.executeBatch(BC,1000);

please identify problem and guide me how to exactly execute a batch apex.
* i tried all the source to execute.
Best Answer chosen by Anudeep B
Bhanu MaheshBhanu Mahesh
Hi Anudeep,

Scope is an optional parameter for Execute method.

If it is defined and we are passing any number, then the execute method will process that number of records from the list returned by Start method in chunks

If Scope is not defined, then salesforce by default execute in chunks of 200 records at a time.

Scope let us define how many records to be executed at each time from the list returned by Start method. Maximum value of scope is 2000 in batch apex

Regards,
Bhanu Mahesh
 

All Answers

Bhanu MaheshBhanu Mahesh

Hi Anudeep,

You are iterating over a new list which does not have any elements.
 You have to iterate over scope
Add Site field in query

The class will be as below
global class Accbtchstate implements database.batchable<sobject>
{
	global database.querylocator start(database.batchablecontext bc)
	{
		string state = 'select id,name,Site from account';
		return database.getquerylocator(state);
	}
	global void execute(database.batchablecontext bc, list<account> scope)
	{
		list<account> acc = new list<account>();
		for(account a:scope)
		{
			a.Site = 'No nedd';
			acc.add(a);
		}
		if(acc.size() > 0){
			update acc;
		}
	}
		
	global void finish(database.batchablecontext bc)
	{
	   
	}
}

executing this in Developer console:  
Accbtchstate BC = new Accbtchstate();
Database.executeBatch(BC,1000);

Regards,
Bhanu Mahesh
Anudeep BAnudeep B
Thnak you Bhanu,

i have a doubt we doing operation on Account object, why do we go to scope.
Actually what is the purpose of "scope" in batch apex, i tied to know that but it not get any claryfication for me please help me on this..
Thanks.
Bhanu MaheshBhanu Mahesh
Hi Anudeep,

Scope is an optional parameter for Execute method.

If it is defined and we are passing any number, then the execute method will process that number of records from the list returned by Start method in chunks

If Scope is not defined, then salesforce by default execute in chunks of 200 records at a time.

Scope let us define how many records to be executed at each time from the list returned by Start method. Maximum value of scope is 2000 in batch apex

Regards,
Bhanu Mahesh
 
This was selected as the best answer
Anudeep BAnudeep B
The program has success fully updated Thanks, and i need little more clarity on scope and list<sobject> in batch apex. i am really confused... with this... could you help me out...