Sunday, June 23, 2019

Saving last value on the form

We can save last value on the form in AX so that when next time form will open it will show last saved value in the certain control:

We will take example to save value of below mark control on inventory counting form:


Below are the steps to enable this functionality:
1. Add below code snippet in class declaration:

AllOpenPosted allOpenPostedTrans
#define.CurrentVersion(1)
#localmacro.CurrentList
allOpenPostedTrans
#endmacro

2. Add below methods on the InventJournalTable form:

//To save default value in case if no last value available
public void initParmDefault()
{
allOpenPostedTrans= AllOpenPosted::All;
}

//To convert object to container
public container pack()
{
return [#CurrentVersion, #CurrentList];
}

//To convert container to object
public boolean unpack(container _packedClass)
{
int version = RunBase::getVersion(_packedClass);
switch (version)
{
case #CurrentVersion:
[version, #CurrentList] = _packedClass;
return true;
default:
return false;
}
return false;
}

//To save last design name
public IdentifierName lastValueDesignName
{
return element.args().menuItemName();
}

//To save last value
public IdentifierName lastValueElementName
{
return this.name();
}

//To save valueType in this case Form
public UtilElementType lastValueType()
{
return UtilElementType::Form;
}

//To store User Id
public UserId lastValueUserId()
{
return curUserId();
}

//To save Legal entity details
public DataAreaId lastValueDataAreaId()
{
return curext();
}
3. Add below code in Run(above Super) and Close(last in method) form's method:
Run:
xSysLastValue::getLast(this);
AllOpenPosted.selection(allOpenPostedTrans );


Close:
allOpenPostedTrans = AllOpenPosted.selection();
xSysLastValue::saveLast(this);


Compile and open form change value in the "show " control and close form next time form will open with last value in the show field.

I hope this will help !!!!











Create dialog run time and update respective record on Dialog

Below are the method to create Dialog dynamically, inter value, fetch value from dialog and update dialog field automatically :

class DialogDemo extends RunBase
{
    DialogField     fieldAccount;
    DialogField     fieldName; 
    DialogField     fieldGroup;
    VendAccount     vendAccount;
    VendName        vendName;
    VendGroupId     vendGroupId;
 
 }

//Convert object to container
    Public Container pack()
    { 
        return conNull();
    }

//Convert container to object
  Public boolean unpack(container _packedClass)
    {
        return true;
    }

// Dialog layout
Object dialog()
{
Dialog          dialog;
dialog = super();
dialog.caption("Customer information");
dialog.allowUpdateOnSelectCtrl(true);
fieldAccount=dialog.addField(Extendedtypestr(VendAccount),"Vendor Account");
fieldName=dialog.addField(Extendedtypestr(VendName));
fieldName.enabled(false);
dialog.addTabPage("Details");
fieldgroup=dialog.addField(Extendedtypestr(vendGroupId));
fieldgroup.enabled(false);
return dialog;

}

//Fetch value from Dialog
public boolean getFromDialog()
{
    vendAccount=fieldAccount.value(); 
    vendName=fieldName.value();
    vendGroupId=fieldGroup.value();
    return super();
}

public static void main(Args _args)
{
    DialogDemo dialogDemo= new DialogDemo();
    if(DialogDemo.prompt())
    {
    DialogDemo.run();
    }
}
//field value assignment
public void dialogSelectCtrl()
{
VendTable   vendTable;
vendTable=vendTable::find(fieldAccount.value());
fieldName.value(vendTable.name());
fieldGroup.value(vendTable.VendGroup);

}

//To execute logic
Public void run()
{
    info(strFmt("Vend Account: %1", vendAccount));
    info(strFmt("Vend Name: %1", vendName));
    info(strFmt("Vend Group: %1", vendGroupId));
}