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
rao venkyrao venky 

Batch apex Sample code

Hai,
 Batch apex code for account filedupdate: Getting below error,can anybody help me?
[Error] Error: Compile Error: Argument type of global method must also be global: List<Account> at line 9 column 17

CODE:
global class Accountupdate implements Database.Batchable<sObject>
{
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        String query = 'SELECT Id,Name,Phone FROM Account ';
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List<Account> scope)
    {
        for (Account a : scope)
        {
           a.Phone='123';
        }
        update a;
    }  
    global void finish(Database.BatchableContext BC)
    {
    }
}
kaustav goswamikaustav goswami
One issue that I can see in your code is in the line "update a". This will be a problem as the variable a will not be recognized and is outside the scope.

Other than that there whould not be any problem.

Try and save the code using the latest version of the API.

Thanks,
Kaustav
Amit Chaudhary 8Amit Chaudhary 8
Please try below code:-
global class Accountupdate implements Database.Batchable<sObject>
{

	global Database.QueryLocator start(Database.BatchableContext BC)
	{
        String query = 'SELECT Id,Name,Phone FROM Account ';
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List<Account> scope)
    {
        for ( Account a : scope)
        {
           a.Phone='123';
        }
        update scope;
    }  
    global void finish(Database.BatchableContext BC)
    {
    }
}

Please mark this as best solution if this will help you.
Thanks,
Amit Chaudhary
 
ManojjenaManojjena
Hi Venky ,

In your code only one issue is that you are updating the account variable whose scope is inside for loop only you will not get the variable out side the loop .

I am in doubt how you got that error ,All syntax are correct apart from that . Instaed of var a you can update scope else you can create blank list of account above the loop and add each account in the list and updat ethe list .Better you can updat he scope .
 You can modify your execut emethod like below .
 
global void execute(Database.BatchableContext BC, List<Account> scope){
       for (Account a : scope){
           a.Phone='123';
       }
     update scope;
}
/////////////////////////OR///////////////////////
global void execute(Database.BatchableContext BC, List<Account> scope){
       List<Account> accList=new List<Account>();
        for (Account a : scope){
           a.Phone='123';
           accList.add(a);
        }
        update accList;
    }

For  some tips and some more information about batch apex you can check the link below .

http://manojjena20.blogspot.in/2015/06/batch-apex-in-depth.html   

Let us know if it helps .
Thanks 
Manoj



 
Eswar Prasad@Sfdc11Eswar Prasad@Sfdc11
HI Rao venky,

Pls see below link batchapex with update of accountfield nice post by infallible blog.we will get idea
http://www.infallibletechie.com/2013/01/simple-batch-apex-example-in-salesforce.html

If you get the answer, please mark it as the correct answer. It will be a help to others who are facing the same problem later.

Regards
Eswar Prasad

 
Mahesh Dhara 9Mahesh Dhara 9
always update list not reference variable update a is wrong update scope then u will get success
Shiva RajendranShiva Rajendran
Hi rao venky,

    global void execute(Database.BatchableContext BC, List<Account> scope)
    {
        for (Account a : scope)
        {
           a.Phone='123';
        }
/*       update a;  // modify this code

*/
update scope;

    }  

This code will work .
Thanks and Regards,
Shiva RV
Mradula Jain 5Mradula Jain 5
global void execute(Database.BatchableContext BC, List<Account> scope) 
you are getting error in this line, instead of List<Account> scope try List<sObject> scopec and then inside the method typecast the list like below:
List<Account> accList = (List<Account>) scope;
for (Account a : accList)

try this.
Mradula Jain 5Mradula Jain 5
Try this:

global class Accountupdate implements Database.Batchable<sObject>
{
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        String query = 'SELECT Id,Name,Phone FROM Account ';
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List<sObject> scope)
    {
        List<Account> accList = (List<Account>) scope;
        for (Account a : accList)
        {
           a.Phone='123';
        }
        update a;
    }  
    global void finish(Database.BatchableContext BC)
    {
    }
}
Mahesh Dhara 9Mahesh Dhara 9
your updating particular record....not updating List.....all the records in list only correct yout statement update accList
Mradula Jain 5Mradula Jain 5
Agreed, he can use the list and can add the record into it and should update list to update all record in single shot but my concern was to focus on the error he is getting for sObject typecasting. 
sadikvali shaiksadikvali shaik
Hi Rao,
Try this one:
global class Accountupdate implements Database.Batchable<sObject>
{
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        return Database.getQueryLocator('SELECT Id,Name,Phone FROM Account ');
    }

    global void execute(Database.BatchableContext BC, List<Account> scope)
    {
      list<account> acc = new list<account>(); 
for (Account a : scope)
        {
           a.Phone='123';
           acc.add(a);
        }
        update acc;
    }  
    global void finish(Database.BatchableContext BC)
    {
    }
}

Hope this help you if it works please mark this as solved.
Thanks and Regards,
Sadik