Monday, July 27, 2015

Using SysLastValue in Ax 2012 R3



We can save some prompt values in forms without using a table, in this post I will show you how you can use SystemLastValue.

1. Design a form (jcSysLastValue) with an editable field, in this case I used am ImventLocationId field.


2. We have to edit the classDeclaration as follows:

public class FormRun extends ObjectRun
{

    InventLocationId   inventLocationId;
    InventLocationId   inventLocationId2;

    #define.CurrentVersion(1)
    #define.version1(1)
    #localmacro.CurrentList
        inventLocationId,
        inventLocationId2
    #endmacro
}


3. Write the method initParmDefault() to set a default Value

public void initParmDefault()
{
;
    inventLocationId = "001";
}



4.  Define the methods pack and unPack()

public container pack()
{
    return [#CurrentVersion,#CurrentList];
}



public boolean unpack(container packedClass)
{
    int version     = RunBase::getVersion(packedClass);

    switch (version)
    {
        case #CurrentVersion:
            [version,#CurrentList] = packedClass;
            return true;
        default :
            return false;
    }

    return false;
}


5.  Define the methods lastValue....

public dataAreaId lastValueDataAreaId()
{
    return curext();
}



public identifiername lastValueDesignName()
{
    return '';



public identifiername lastValueElementName()
{
    return formStr(jcSysLastValue);
}



public UtilElementType lastValueType()
{
    return UtilElementType::Form;


public userId lastValueUserId()
{
    return curuserid();
}


6. Getting saved values and setting them to the field.

public void run()
{
    super();
    xSysLastValue::getLast(this);
    inventLocationId_text.text(inventLocationId);
}  


7. Saving values to somewhere  

public void close()
{
    super();
    inventLocationId = inventLocationId_text.text();
    xSysLastValue::saveLast(this);
}



If you want to clear the values just delete de follow record and that's it.