Thursday, 11 May 2017

How to get user based security roles using x++ code in Ax 2012 R3

Export user based roles in Ax 2012 R3

static void Export_Userbasedroles(Args _args)
{
    SysExcelApplication         xlsApplication;
    SysExcelWorkBooks           xlsWorkBookCollection;
    SysExcelWorkBook            xlsWorkBook;
    SysExcelWorkSheets          xlsWorkSheetCollection;
    SysExcelWorkSheet           xlsWorkSheet;
    SysExcelRange               xlsRange;
    CustTable                   custTable;
    int                         row = 1;
    str                         fileName;
    SecurityUserRole            _SecurityUserRole;
    SecurityRoleTaskGrant       _SecurityRoleTaskGrant;
    SecurityTask                _SecurityTask;
    SecurityRole                _SecurityRole;
    UserInfo                    _UserInfo;
    
    fileName = @"C:\Users\Rajendra\Desktop\Roles.xlsx";
    xlsApplication           = SysExcelApplication::construct();
    xlsWorkBookCollection    = xlsApplication.workbooks();
    xlsWorkBook              = xlsWorkBookCollection.add();
    xlsWorkSheetCollection   = xlsWorkBook.worksheets();
    xlsWorkSheet             = xlsWorkSheetCollection.itemFromNum(1);
    xlsWorkSheet.cells().item(row,1).value("User Role");
    xlsWorkSheet.cells().item(row,2).value("User Id");
    xlsWorkSheet.cells().item(row,3).value("Legal Entity");
    row++;

    while select _UserInfo
    {
        while select _SecurityUserRole where _SecurityUserRole.User==_UserInfo.id
        {
                while select _SecurityRole where _SecurityRole.RecId==_SecurityUserRole.SecurityRole
                {
                    xlsWorkSheet.cells().item(row,1).value(_SecurityRole.Name);
                    xlsWorkSheet.cells().item(row,2).value(_UserInfo.id);
                    xlsWorkSheet.cells().item(row,3).value(_UserInfo.company);
                    row++;
                }
        }
    }
   
    if(WinApi::fileExists(fileName))
        WinApi::deleteFile(fileName);
    xlsWorkbook.saveAs(fileName);
    xlsApplication.visible(true);

}

From reference 

1 comment: