Monday, May 14, 2018

Multiselect lookup AX 2012

In this post will share code used to create multiselect lookup on runbase dialog.

class MultiSelectLookupExample extends RunBaseBatch
{
    // Dialog fields, groups and controls
    DialogField                 dlgFileName;
    DialogGroup                 dlgGrpFile, dlgGrpRoles;
    FormBuildControl            fbc;
    FormBuildStringControl      fbsc;
    SysLookupMultiSelectCtrl    sysms;

    // Packed variables
    FilenameOpen                fileName;
    container                   conRoles;
    container                   selectedRoles;

    #define.CurrentVersion(1)
    #define.Version1(1)
    #localmacro.CurrentList
        fileName,
        conRoles,
        selectedRoles
    #endmacro
}

public Object dialog()
{
    DialogRunbase       dialog = super();
    #resAppl
    xSysLastValue::getLast(this);
    dialog.addImage(#ImageEmployee);

    dlgGrpFile = dialog.addGroup("@SYS4000240");
    dlgFileName = dialog.addFieldValue(extendedTypeStr(FilenameOpen), this.parmFileName());

    dlgGrpRoles = dialog.addGroup("@SYS336367");
    fbc = dialog.formBuildDesign().control(dlgGrpRoles.name());
    fbsc = fbc.addControl(FormControlType::String, "@GLS221111");
    fbsc.width(400);
    fbsc.label("@GLS221111");

    return dialog;

}

public void dialogPostRun(DialogRunbase dialog)
{
    FormRun                 fr;
    container               con;
    super(dialog);

    fr = dialog.dialogForm().formRun();
    if (fr)
    {
        sysms = SysLookupMultiSelectCtrl::construct(fr, fr.design().control(fbsc.id()), queryStr(AXPSecRoleQuery), false, this.parmRolesSelect());
        con = conIns(con, 1, this.parmRolesSelect());
        con = conIns(con, 2, this.parmSelectedRoles());
        sysms.set(con);
    }
}

public boolean getFromDialog()
{
    boolean ret;

    ret = super();

    fileName = dlgFileName.value();
    this.parmRolesSelect(sysms.get());
    this.parmSelectedRoles(sysms.getSelectedFieldValues());
    return ret;
}

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

public boolean unpack(container packedClass)
{
    Version version = RunBase::getVersion(packedClass);
;
    switch (version)
    {
        case #CurrentVersion:
            [version, #CurrentList] = packedClass;
            break;
        default:
            return false;
    }

    return true;
}

public container parmSelectedRoles(container   _selectedRoles = selectedRoles)
{
    selectedRoles = _selectedRoles;
    return selectedRoles;
}

public FilenameOpen parmFileName(FilenameOpen   _fileName = fileName)
{
    fileName = _fileName;
    return fileName;
}

public container parmRolesSelect(container _conRoles = conRoles)
{
   conRoles = _conRoles;
   return conRoles;
}


public void run()
{
    #OCCRetryCount
    

    try
    {
        // Your logic goes here
        xSysLastValue::saveLast(this);
    }
    catch (Exception::Deadlock)
    {
        retry;
    }
    catch (Exception::UpdateConflict)
    {
        if (appl.ttsLevel() == 0)
        {
            if (xSession::currentRetryCount() >= #RetryNum)
            {
                throw Exception::UpdateConflictNotRecovered;
            }
            else
            {
                retry;
            }
        }
        else
        {
            throw Exception::UpdateConflict;
        }
    }
    catch (Exception::DuplicateKeyException)
    {
        // retry in case of an duplicate key conflict
        if (appl.ttsLevel() == 0)
        {
            if (xSession::currentRetryCount() >= #RetryNum)
            {
                throw Exception::DuplicateKeyExceptionNotRecovered;
            }
            else
            {
                retry;
            }
        }
        else
        {
            throw Exception::DuplicateKeyException;
        }
    }
}


No comments:

Post a Comment