Month: August 2014

AX 2012: Get SID of Logged-In User – whoami

Posted on Updated on

Open command prompt and run the following command to get the SID of the current logged-in user:

Untitled

AX 2012: Using Temporary Table as Form’s Datasource

Posted on Updated on

First add a method on the form to populate records in the temporary table:

public LabelsTable populateRecords()
{
    SysDictTable    dictTable = new SysDictTable(tableNum(PurchLine));
    SysDictField    dictField;
    TreeNode        treeNode;
    LabelsTable     labelsTableLocal;                 // Temporary table (InMemory)
    FieldId         fieldId = dictTable.fieldNext(0);

    while (fieldId)
    {
        dictField = dictTable.fieldObject(fieldId);

        if (dictField.isSql() && !dictField.isSystem() && dictField.name() != "Modified")
        {
            treeNode = dictField.treeNode();
            labelsTableLocal.Field = dictField.name();
            labelsTableLocal.Label = treeNode.AOTgetProperty("Label");
            labelsTableLocal.insert();
        }

        fieldId = dictTable.fieldNext(fieldId);
    }
    
    return labelsTableLocal;
}

 

Then override form’s init() method and add the following code to display the temporary table data on the form:

public void init()
{
    super();
    
    LabelsTable.setTmpData(element.populateRecords());
}

AX 2012: Loop through all the fields of a table in X++

Posted on

This is how we can loop through all the fields of a table in X++:

static void Job1(Args _args)
{
    SysDictTable    dictTable = new SysDictTable(tableNum(PurchLine));
    SysDictField    dictField;
    TreeNode        treeNode;
    FieldId         fieldId = dictTable.fieldNext(0);

    while (fieldId)
    {
        dictField = dictTable.fieldObject(fieldId);

        if (dictField.isSql() && !dictField.isSystem())
        {
            treeNode = dictField.treeNode();
            info(strFmt("%1 | %2 | %3",
                dictField.name(),                                               // Field name
                treeNode.AOTgetProperty("Label"),                               // Label id
                SysLabel::labelId2String(treeNode.AOTgetProperty("Label"))));   // Label string
        }

        fieldId = dictTable.fieldNext(fieldId);
    }
}

AX 2012: evalBuf – Evaluate string expression in X++

Posted on Updated on

evalBuf Function is a very strong API in X++. We can quite easily evaluate complex algebraic expressions given in string and the result is also given back in string. This API should be used along with CodeAccessPermission. The downside of using this API is that the code using this function is not supported in CIL.

This is how we can use this API

static void JobEvalBufDemo(Args _args)
{
    ExecutePermission perm;
    str strCodeToExecute = "2 + (5*10)";
    str strResult;
    ;

    perm = new ExecutePermission();

    if (perm != null)
    {
        perm.assert();				// Grants permission
        strResult = EvalBuf(strCodeToExecute);
        CodeAccessPermission::revertAssert();	// Revoke permission
    }

    info(strFmt("Result is: %1", strResult));
    // info: "Result is: 52"
}