0

We have a Account object and I did wrote a Batch class that will update the Account name with 'Mr.' in front of it. However it did not updated the Account records Name.

Account Batch Code:-

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){} 
}

Please advise.

Thank You,

2 Answers 2

2

You are instantiating a new list of Account records in your execute method.

As you can see, the execute method of the batch classes receive a list of records as parameter. The records you query in the start method.

So you just have to loop through the list received as parameter like this:

global void execute(database.BatchableContext bc,List<Account> Scope)
{
    for(Account a: Scope)
    {
        a.Name ='Mr.'+a.Name;
    }
    update Scope;
}

Is there a reason why you're updating all the Account names instead of adding a new formula field on the Account object? You can add a formula of type text with this code:

'Mr.' & Name
8
  • This is a UAT environment which has a 2000+ accounts and My lead requires a Batch class from me. ,Also I tried your code it still did not updated the Account name field in Account records Commented Feb 20, 2017 at 9:48
  • What are the logs of the execution of the batch? It should work. Commented Feb 20, 2017 at 9:49
  • I have a question here. Just saving the Batch class will run the batch right? (Updating the records) Commented Feb 20, 2017 at 9:52
  • No it doesn't. You can schecule a batch if you want to run it regularly. But in your case, you can just run it from the developer console like this. Commented Feb 20, 2017 at 9:55
  • This batch would keep adding Mr. everytime the batch runs and is not flexible. @MartinLezer mentioned to use a formula field is much better solution. If you still want to update it, you can create a formula field and update records where Name is not equal to the formula.
    – Raul
    Commented Feb 20, 2017 at 10:08
0

Update your class.

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){} 
}

And execute the Batch Class from Developer Console's 'Execute Anonymous' window.

Database.executeBatch(new AccountBatch());
2
  • I don't think your answer is very much different than of Martin's, He had already specified how to run the batch in his answer's comment.
    – Raul
    Commented Feb 20, 2017 at 10:27
  • @ForceMars This is also not working I tried and it did not updated Account records Commented Feb 20, 2017 at 10:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .