How to persist values on runtime in SysLastValue?

Sometimes we need to store values on runtime which are not really worthy of storing in database. Examples could be the fiscal calendar year and period entered by a particular user last time during the session. In many situations, the user would like to know what values he entered last time while doing some processing. Since this information is not required to be persisted in database therefore we should have some way to store them either in some session type variable or cache. Fortunately in AX we have a pack/unpack pattern along with a table called SysLastValue that stores such information. To illustrate the concept, lets create a very simple form with two unbound controls.

  • Gender combo box hooked to gender base enum. AutoDeclaration = Yes
  • Date of birth: dateEdit control, extended with TransDate EDT. AutoDeclaration = Yes

Now we want to persist the values of both of these controls in SysLastValue table. Here are the steps to do that.

  • Declare variables in the class declaration and define macros to store them into container.

public class FormRun extends ObjectRun{

// form level variables to store control values.
Gender      gender;
TransDate   DOB;

#define.CurrentVersion(1)
#localmacro.CurrentList
gender,
DOB
#endmacro
}

  • Write the following methods at the form level. They return application and client level information to the SysLastValue framework. This information is needed to uniquely identify the last value stored in the SysLastValue table.

dataAreaId lastValueDataAreaId(){    return curext(); }

private identifierName lastValueDesignName(){    return ”; }

private identifierName lastValueElementName(){    return this.name(); }

private UtilElementType lastValueType(){    return UtilElementType::Form; }

private userId lastValueUserId(){    return curUserId(); }

  • Add the pack/unpack methods.

public container pack(){

gender = cmbGender.selection();
DOB = str2Date(dateOfBirth.valueStr(), 1);
return [#CurrentVersion,#CurrentList];

}

public boolean unpack(container packedClass){

Integer     version     = conpeek(packedClass,1);

switch (version)    {

case #CurrentVersion:

[version,#CurrentList] = packedClass;

break;

default:

return false;

}

return true;
}

  • Override the close and init methods of the form to save and retrieve values from SysLastValue table. There is a system class xSysLastValue which contains some static methods to perform these operations.

public void close(){

super();
xSysLastValue::saveLast(this);

}

public void init(){

super();
xSysLastValue::getLast(this);
cmbGender.selection(gender);
dateOfBirth.dateValue(DOB);

}

  • One last thing. In order to initialize the default values for the first time form executed in the current session, we need to provide a method called initParmDefault() to do such settings. This is the required method, otherwise the framework complains at the time form initialization.

public void initParmDefault()
{

gender = gender::Unknown;

}

Now suppose you run the form and entered the data as shown in the follow figure. Close the form and run it again, you’ll see these values already filled up.

The SysLastValue framework works well for providing runtime persistence in AX. The source code of this example can be found here. Happy Daxing!!

3 thoughts on “How to persist values on runtime in SysLastValue?

Add yours

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: