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
SFDC Coder 8SFDC Coder 8 

Getting Null value while fetching Day of Week

Hi All,
I am getting Null value while fetching value for day of week.
Here is my code.
global class batchInsert Implements Schedulable, Database.Batchable<sObject>{

	private Date d;
	
	public batchInsert(Date d) {
        d = (Date)d;
    }
    
    
	global database.queryLocator start(Database.BatchableContext BC) {
	    Datetime dt = d; 
	    String dayOfWeek = dt.format('EEEE');
	    ...
	} 

	global void execute(Database.BatchableContext BC, list <Obj1__c> scope) {
	    List <Obj2__c> creList = new List<Obj2__c>();
	    Datetime dt = (DateTime)d; 
	    String dayIs = dt.format('EEEE');
	    ...
	}


I am not getting value at Line 12 for dayOfWeek (String dayOfWeek = dt.format('EEEE');)

Please suggest me what change I need to do.
Thanks in Advance

bob_buzzardbob_buzzard
You'll need to implement Database.stateful to be able to access instance variables:

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch_interface.htm

search for Using State in Batch Apex.
Amit Chaudhary 8Amit Chaudhary 8
Try to move your date method inside the Batch job like below
global class batchInsert Implements Schedulable, Database.Batchable<sObject>{

	
   
	global database.queryLocator start(Database.BatchableContext BC) {
		Date d = System.today();
	    Datetime dt = d; 
	    String dayOfWeek = dt.format('EEEE');
	    ...
	} 

	global void execute(Database.BatchableContext BC, list <Obj1__c> scope) {
	    List <Obj2__c> creList = new List<Obj2__c>();
	    Datetime dt = (DateTime)d; 
	    String dayIs = dt.format('EEEE');
	    ...
	}