Back to Blog Index

Get last selected value on form control

Problem: Store last selection in the form by user

There was a requirement to store user’s last selection on the form – i.e. the site selected last time should be populated automatically when the user opens the same form again. So if the user selected siteInventory details inquiry Dynamics AX “E” the form should remember it the next time.

Solution: We can solve this problem by implementing standard Ax syslastvalue/Pack-Unpack methods. Here we store the value in variable for current user session and based on user, current extension etc. To develop this functionality add the following code on the form.

Code: Add these methods on the form and it will start working:

//class declaration

public class FormRun extends ObjectRun

{

    InventSiteId                    site;

    #define.CurrentVersion(1)

    #localmacro.CurrentList

    site

    #endmacro

}

 

//pack

public container pack()

{

    return [#CurrentVersion,#CurrentList];

}

 

// Unpack

public boolean unpack(container packedClass)

{

    int version;

    ;

    version = RunBase::getVersion(packedClass);

    if(version)

        {

            [version,#CurrentList] = packedClass;

            return true;

        }

        return false;

}

 

//Site sysLastValue: This method is use to initialize default value

Public void initParmDefault()

{

;

         site = “SiteA”;

}

 

//Site sysLastValue: This method is use to store dataAreaId for current user

public dataAreaId lastValueDataAreaId()

{

    return curext();

}

 

//Site sysLastValue: This method returns the name of caller

 public identifiername lastValueDesignName()

{

    return element.args().menuItemName();

}

 

//Site sysLastValue: This method returns the name of form/object

public identifiername lastValueElementName()

{

    return this.name();

}

 

//Site sysLastValue: This method returns the type of caller

public UtilElementType lastValueType()

{

    return UtilElementType::Form;

}

 

//Site sysLastValue: This method returns the name of current user

public userId lastValueUserId()

{

    return curuserid();

}

 

//Site sysLastValue: on form close save site value

public void close()

{

    site = StringEditSiteId.valueStr();

    xSysLastValue::saveLast(this);

    super();

}

 

//Site sysLastValue: on run assign value to form control

public void run()

{

    super();

    //get the last value stored in cache

    //here unpack method used

    xSysLastValue::getLast(this);

    //set the last user selection on the field- this is form control for siteId

    StringEditSiteId.text(site);

}

Written by: Amit Ahirrao • Senior Technical Consultant • Arbela Technologies