RunBase How to enable the caching mechanism (pack/unpack)
How to Enable Caching (pack / unpack) in RunBase
This article also applies to Jobs implemented using the RunBaseBatch framework.
Business Background
In D365 Finance & Operations, RunBase is commonly used to implement parameterized business operations or batch Jobs. To ensure a good user experience and functional completeness, caching (pack / unpack) must be enabled in the following scenarios:
- When the Records to include control is required, allowing users to enter filter criteria for business tables (i.e., enabling Query);
- When the Job needs to run in a new session (RunInNewSession);
- When the Job must support Run in Batch execution.
From an implementation perspective, caching is essentially the serialization and deserialization of RunBase member variables. These member variables typically correspond to the parameters exposed to users in the Dialog.
This article demonstrates the simplest and standard implementation approach to correctly enable caching in a RunBase Job, while avoiding common implementation pitfalls.
Key Implementation
Step 1: Use macros to manage member variables
1 | protected TransDate gTransDate; // Member variable |
Notes:
- Using macros significantly improves maintainability
- When parameters change, only
#CurrentListneeds to be updated - container-type variables must be handled separately and cannot be placed directly in the macro list
Step 2: Override the pack() method
1 | public container pack() |
Implementation details:
- The return value must be a container
- It is recommended to always store the version number first to support future extensions
Step 3: Override the unpack() method
1 | public boolean unpack(container _packedClass) |
Notes:
RunBase::getVersion()is used to safely parse the cache structure- This is a key mechanism for supporting Job upgrades and backward compatibility
Special Case: Caching with QueryRun
When a Job enables the Records to include control, a
QueryRunmember variable is typically introduced.
SinceQueryRuncannot be stored directly using macros, it must be handled separately.
Step 1: Declare member variables
1 | protected TransDate gTransDate; |
Step 2: Override the pack() method
1 | public container pack() |
Key points:
QueryRunmust be packed separately usingpack()- The order must be consistent with the
unpack()method
Step 3: Override the unpack() method
1 | public boolean unpack(container _packedClass) |
Notes:
- During unpack, both
QueryandQueryRunmust be reconstructed - Existing object instances must not be reused
Result Validation
- Open the Job;
Enter parameter values and click OK;
After successful execution, reopen the Job. The parameter values should default to the values entered during the previous run;
These results indicate that:
- pack / unpack is working correctly
- parameters are correctly restored in new session and Batch scenarios
Summary
- RunBase does not automatically enable caching;
pack / unpackmust be explicitly implemented by the developer; - Objects that need to be cached are typically member variables exposed in the Dialog;
- Using macros to manage cached fields is strongly recommended to improve maintainability;
QueryRunis a special case and must be packed and unpacked separately;- If a Job supports RunInNewSession / RunInBatch / Records to include, caching is mandatory rather than optional.