Adding a role to all or multiple users using X++ Code [Dynamics AX 2012]


In AX 2012, we can add a role to all the users by running the automated role assignment job based on the rule which we define in the “Assign users to Role” form.

Navigation : System administration >> Setup >> Security >> Assign users to rolesassign users

And the code behind this is :

SysSecurityDynamicRoleAssignment::synchronize(args.record().RecId); // selected Role (this is Security Role table buffer) in the form

I tried to run the above code independently by passing the “Budget clerk” Security Role but did not work due to obvious reasons as it needed a rule assignment table buffer.

The equivalent code to assign a role to all users is below.

This doesn’t need to add any rules like what we do in the form.This is just an experiment to handle the roles through code and request to test the code before using it.

static void SR_AssignRoleToAllUsers(Args _args)


    SecurityRole        role;

    SecurityUserRole    userRole;

    boolean             added;

    UserInfo            userInfo;



    select role where role.Name == ‘Budget clerk’;

    while select userInfo


        select * from userRole

            where userRole.SecurityRole == role.RecId &&

                userRole.User ==;


        if (!userRole || (userRole.AssignmentStatus != RoleAssignmentStatus::Enabled))


            info(strFmt(‘Role %1 added to the user %2 successfully.’, role.Name,;


            userRole.User =;

            userRole.SecurityRole = role.RecId;

            userRole.AssignmentMode = RoleAssignmentMode::Manual;

            userRole.AssignmentStatus = RoleAssignmentStatus::Enabled;

            SecuritySegregationOfDuties::assignUserToRole(userRole, null);




            warning(strFmt(‘skipping – Role %1 to the user %2.’, role.Name,;





Happy dax6ng,

Sreenath Reddy


SysExcelWorksheetHelper and SysExcelHelper classes in AX 2012–Quickly create excel [X++]


SysExcelWorksheetHelper class in AX 2012 will help to quickly create an excel with the desired fonts, summation[sum ranges] features, auto fit columns, populate field names as headers from the tables etc.

SysExcelHelper class will help to create and manage the overall workbook. Remember the same thing can be achieved using SysExcel* classes.

Below is the sample code. This example displays Account Number, Currency, Customer group and BalanceMST fields and it will sum the BalanceMST fields and will displays as total at the end.

static void SR_SysExcelWorksheetHelper(Args _args)


    CustTable               custTable;

    SysExcelWorksheetHelper worksheetHelper;

    SysExcelHelper          sysExcelHelper;

    SysExcelWorksheet       worksheet;

    int                     currentRow = 1;

    int                     sumRow;

    str                     worksheetName;

    int                     redColor = WinAPI::RGB2int(255, 0, 0);

    SysExcelRange           range;

    COMVariant              cellValue = new COMVariant(COMVariantInOut::Out);

    str                     fileName;

    str attachmentPath      = "C:\\";


    // Sets the font color for a range of cells

    void setRangeFont(int _fromColumn, int _fromRow, int _toColumn, int _toRow, int _rgbIntColor)


        range = worksheetHelper.getWorksheetRange(_fromColumn, _fromRow, _toColumn, _toRow);

        worksheetHelper.setFontColor(range, _rgbIntColor);




    // Defines the columns of the spreadsheet






    worksheetName = "@SYS135880";


    sysExcelHelper = SysExcelHelper::construct();




    worksheet = sysExcelHelper.addWorksheet(worksheetName);


    worksheetHelper = SysExcelWorksheetHelper::construct(worksheet);


    // Populate the header row with the appropriate field labels and format the columns

    worksheetHelper.addColumnFromTableField(#AccountNum, tablenum(CustTable), fieldnum(CustTable, AccountNum));

    worksheetHelper.addColumnFromTableField(#Currency, tablenum(CustTable), fieldnum(CustTable, Currency));

    worksheetHelper.addColumnFromTableField(#CustGroup, tablenum(CustTable), fieldnum(CustTable, CustGroup));

    worksheetHelper.addColumn(#BalanceMST, "Balance MST", Types::Real);


    while select custTable


        currentRow ++;

        worksheetHelper.setCellValue(#AccountNum, currentRow, custTable.AccountNum);

        worksheetHelper.setCellValue(#Currency, currentRow, custTable.Currency);

        worksheetHelper.setCellValue(#CustGroup, currentRow, custTable.CustGroup);

        worksheetHelper.setCellValue(#BalanceMST, currentRow, custTable.balanceMST());


    if (currentRow > 1)


        sumRow = currentRow + 2;


        worksheetHelper.setCellValue(#BalanceMST, sumRow, "@SYS58659");


        worksheetHelper.sumRange(worksheetHelper.getWorksheetRange(#BalanceMST, 2, #BalanceMST, currentRow), #BalanceMST, sumRow);


        setRangeFont(#BalanceMST, 2, #BalanceMST, currentRow, redColor);


        cellValue = worksheet.cells().item(sumRow, #BalanceMST).value();

        if (cellValue.currency() > 0)


        setRangeFont(#BalanceMST, sumRow, #BalanceMST, sumRow, redColor);






    // Generate the file using the current UTC date time (without the ‘:’ character)

    // since it is not allowed for file names.

    fileName = strfmt(‘%1%2%3’, attachmentPath, strReplace(DateTimeUtil::toStr(DateTimeUtil::utcNow()), ‘:’, ), sysExcelHelper.getFileExtension());;






Happy Dax6ng,

Sreenath Reddy

SysFieldGroupLookup class in AX 2012–lookup from Table FieldGroups[X++]


As we all know that we have extensively used SysTableLookup in older versions of AX to get the custom lookups. This class is still available in AX 2012.

But, in AX 2012 a new class SysFieldGroupLookup has been introduced which will help to get the lookup from the field groups of the table. This is a very useful feature as most of the fields to be added to the lookup fields can be reduced and can be pulled from the Table >> field groups directly.

Let me explain with an example.

Create a form as shown below with simple StringEdit control and set the lookup button property to “Always”


Override the lookup method of the control and add the below code

public void lookup()


    SysFieldGroupLookup     oSysFieldGroupLookup;

    Query                   oQuery = new Query();



    oSysFieldGroupLookup = SysFieldGroupLookup::newParameters(tablenum(CustTable), this);


    oSysFieldGroupLookup.addLookupfield(fieldnum(CustTable, AccountNum));


    oSysFieldGroupLookup.parmCloseSelectFieldId(fieldnum(CustTable, AccountNum));




Save the changes. Open the form by using Ctrl + O. you will find that the lookup will show the fields/columns and data of all the customers from the field Group “salesOrder” of CustTable.


Try with different table and field groups. Nice feature. Lookup will also display the display/data methods in the field group.


Try with EPCreditInfo field group. I am looking into how to get the selected value of the data method in to calling control. No luck though. I don’t see any method to return to calling control the selected data method value.


Happy Dax6ng,

Sreenath Reddy