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
GunnarGunnar 

Batchable Interface class - properties not persisting?

In the START, I'm setting values on some properties.

But in EXECUTE, the values are back to default - null.

 

I'm assuming my instantiated class stays in memory between calls to EXECUTE???

 

Here is the top part of the class...

 

global class clsMyBatchClass implements Database.Batchable<sObject>
{

// --------------------------
// PROPERTIES
// --------------------------

private map<Integer, String> mapMonths;
private map<String,Integer> mapPeriods;
private map<Integer,String> mapPeriodsBack;
private String strThisPeriod;
private Integer intThisPeriodIndex;


//-----------------------------------------------------------------
global Database.QueryLocator start(Database.BatchableContext bcObj) // QueryLocator represents a server-side cursor
//-----------------------------------------------------------------
{

system.debug('IN Start');
String strQuery = 'SELECT ID FROM Account';
LoadMap();

 

strThisPeriod = date.today().Year() + '-' + mapMonths.get(date.today().Month());

 

- - - -

There is a private method 'Load Map'. It will get a value to the string 'ThisPeriod'.

However, in EXECUTE, the maps won't work and I have to LoadMap on every EXECUTE.

 

Any idea what I'm doing wrong??

Best Answer chosen by Admin (Salesforce Developers) 
GunnarGunnar

Well, isn't that deceiving. 

Simply implementing an interface - causes destruction of all your properties?

 

pfft. ...

 

More specifically in the docs: If you don't specifyDatabase.Stateful, all static and instance member variables are set back to their original values.

 

Here is the definition of a 'class', by wikipedia.

 

In object-oriented programming, a class is a construct that is used to create instances of itself – referred to as class instances, class objects, instance objects or simply objects. A class defines constituent members which enable its instances to have state and behavior.[1] Data field members (member variables or instance variables) enable a class instance to maintain state. Other kinds of members, especially methods, enable the behavior of class instances. Classes define the type of their instances.

 

 

STATE AND BEHAVIOR.

 

Sounds like STATE went absent when implementing this interface.

 

I'm expecting classes to persist their properties.

 

So, it is no longer a class, but some thing else.

 

I had a bunch of 'preprocessing' to do. So now, I'll do it a 1,000 times. I'm NOT implementing yet another interface to get the stupid first interface to do what it's supposed to do.

All Answers

sfdcfoxsfdcfox

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_batch_interface.htm

 

You have to specify the Database.Stateful interface to persist the heap between calls to execute().

GunnarGunnar

Well, isn't that deceiving. 

Simply implementing an interface - causes destruction of all your properties?

 

pfft. ...

 

More specifically in the docs: If you don't specifyDatabase.Stateful, all static and instance member variables are set back to their original values.

 

Here is the definition of a 'class', by wikipedia.

 

In object-oriented programming, a class is a construct that is used to create instances of itself – referred to as class instances, class objects, instance objects or simply objects. A class defines constituent members which enable its instances to have state and behavior.[1] Data field members (member variables or instance variables) enable a class instance to maintain state. Other kinds of members, especially methods, enable the behavior of class instances. Classes define the type of their instances.

 

 

STATE AND BEHAVIOR.

 

Sounds like STATE went absent when implementing this interface.

 

I'm expecting classes to persist their properties.

 

So, it is no longer a class, but some thing else.

 

I had a bunch of 'preprocessing' to do. So now, I'll do it a 1,000 times. I'm NOT implementing yet another interface to get the stupid first interface to do what it's supposed to do.

This was selected as the best answer
sfdcfoxsfdcfox

Ironically, Database.Stateful is a zero-element interface; simply including it in the "implements" clause will suffice. That's the really odd part about it. You won't need to write any extra code or do anything extra, just include that magic interface. But, yes, I agree that this one specific type of interface would break all the normal rules of a class is incredibly odd.