Dynamics Online

Microsoft Business Apps and Microsoft Cloud

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!!

About fahahm

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

  1. Asking questions are genuinely nice thing if you are not understanding anything
    fully, but this piece of writing gives pleasant understanding even.

Comments are closed.