+ Start a Discussion
A Raj 9A Raj 9 

Hi All, I wrote this Batch code, and is not showing any error and also it is running anonymously, but record is not getting updated

Hi All, I wrote this Batch code, and is not showing any error and also it is running anonymously, but record is not getting updated

public class fieldupdate1 implements Database.Batchable<SObject>{
    public Database.querylocator start(Database.BatchableContext bc){
        string query = 'select id, name, industry from account';
        return database.getquerylocator(query);
        }

    public void execute(Database.BatchableContext bc, list<Account> scope){
        list<Account> accs = new list<account>();

        for(Account a : scope){
            
            for(integer i=25;i<30;i++){
                if(a.name == 'Test Batch'+i){a.industry='Agriculture';}
            accs.add(a);
            }
            
            }
        update accs;
        }
    
    public void finish(Database.BatchableContext bc){}
    
    }
Syed Insha Jawaid 2Syed Insha Jawaid 2
Hi Raj

Did you check in debug what does the following variables contain:
1. scope
2. accs

Cheers!!!!
TruptiTrupti
You are adding account records in list after If which is wrong. 
As per your code if Account name is 'Test Batch'25(OR 26 OR 27 OR 28 OR 29)  then you are updating Industry to 'Agriculture'


Please try below code:

public class fieldupdate1 implements Database.Batchable<SObject>{
    public Database.querylocator start(Database.BatchableContext bc){
        string query = 'select id, name, industry from account';
        return database.getquerylocator(query);
        }

    public void execute(Database.BatchableContext bc, list<Account> scope){
        list<Account> accs = new list<account>();
        for(Account a : scope){
            for(integer i=25;i<30;i++){
                if(a.name == 'Test Batch'+i){a.industry='Agriculture';
                 accs.add(a);
            }
           }
         }
        update accs;
        }
    public void finish(Database.BatchableContext bc){}
 }
   
A Raj 9A Raj 9
Thanks Syed & Trupti for your response. I got it, we have to give 'accs.add(a)' before close of 'for loop' which has scope.
like below

***************************
for(Account a : scope){
            for(integer i=25;i<30;i++){
                if(a.name == 'Test Batch'+i){a.industry='Agriculture';
            }
           }
         accs.add(a);
         }

*************************
Thank you.
TruptiTrupti
If solution which I provided solved your problem,could you marked it as Best Answer.

Thanks