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
Krishnan MishraKrishnan Mishra 

Why my list is blank in the finish()?

I am trying to change the "status" field(custom) of the contact object by using a batch. If I update the fields in the execute method then it gets updated but if I try to update it in the finish method then my list is empty. I am not getting this!
 
global class BatchNSchedulable implements Database.Batchable<sObject>{
	global list<contact> con=new list<contact>();
	String query = 'SELECT id,name,status__c FROM contact WHERE CreatedDate= Yesterday';
	global Database.QueryLocator start(Database.BatchableContext bc){
		System.debug('In the start');
		 return (Database.getQueryLocator(query));
	}
	global void execute(Database.BatchableContext bc,list<sObject> li){
		System.debug('In the execute');
		con = (list<contact>)li;
		System.debug(con.size());	
		for(contact ct:con){
			ct.put('status__c','None');
		}
		System.debug('con in execute is '+con);
	}
	global void finish(Database.BatchableContext bc){
		System.debug('con is '+con);
		update con;
		System.debug('In the finish');
	}

}

 
Best Answer chosen by Krishnan Mishra
v varaprasadv varaprasad
Hi Krishnan,

Please add Database.stateful in class 

global class bigObject implements database.Batchable<Sobject>,Database.Stateful {

}

More info : 

https://salesforce.stackexchange.com/questions/175355/database-stateful-when-to-use-it

Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com​

 

All Answers

Maharajan CMaharajan C
Hi Krishnan,
'
Try the below simple code:

global class BatchNSchedulable implements Database.Batchable<sObject>{

        global database.QueryLocator start(database.BatchableContext bc){
        System.debug('In the start');
        string query = 'SELECT id,name,status__c FROM contact WHERE CreatedDate= Yesterday';
        return database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext bc,list<Contact> scope){
        System.debug('In the execute');
        
        for(Contact cont:scope)
        {
        con.status__c='None';
        }
        update scope;
        
    }
    global void finish(Database.BatchableContext bc){
        
    }

}

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Raj.
Krishnan MishraKrishnan Mishra
Hi Raj,
Thank you for the answer but my question is why I can't update the list in the finsih method so that my number of apex method execution per day remain 1. My code is working fine if I update my field in execute method but list is blank in finish method, why?
v varaprasadv varaprasad
Hi Krishnan,

Please add Database.stateful in class 

global class bigObject implements database.Batchable<Sobject>,Database.Stateful {

}

More info : 

https://salesforce.stackexchange.com/questions/175355/database-stateful-when-to-use-it

Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com​

 
This was selected as the best answer
Maharajan CMaharajan C
Hi Krishnan,

To achieve that you have to use the Database.stateful to maintain the State of the list all the Transaction.

And also please refer the below updated code:

global class BatchNSchedulable implements Database.Batchable<sObject>, Database.Stateful{
    global list<contact> con;
    
    global BatchNSchedulable (){
    con = new list<contact> ();
    }
    
    String query = 'SELECT id,name,status__c FROM contact WHERE CreatedDate= Yesterday';
    global Database.QueryLocator start(Database.BatchableContext bc){
        System.debug('In the start');
         return (Database.getQueryLocator(query));
    }
    
    global void execute(Database.BatchableContext bc,list<Contact> scope){
        System.debug('In the execute');
        
        for(Contact cont:scope)
        {
        Contact c = new Contact (Id = cont.Id, status__c = 'None');
        con.add(c);
        }
        
        System.debug('con in execute is '+con);
    }
    global void finish(Database.BatchableContext bc){
        System.debug('con is '+con);
        System.debug('con size is '+con.size());
        update con;
        System.debug('In the finish');
    }

}

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Raj.