Tuesday 14 August 2018

Get a list of Security Roles with Duties from Dynamics AX using code X++


Following code can be used to obtain a list of all security roles along with the associated duties from Dynamics AX using X++ code:

static void JZS_ExtractRoleswithDuties(Args _args)
{

    CommaIO                       commaIO;
    FileName                        fileName;
    InventTable                     inventTable;
    SecurityRole            SecurityRole;
    SecurityRoleTaskGrant grantTable;
    str DutyName;
    SecurityTask            securityTask, securityTaskDef;
    ;

    fileName       = WINAPI::getTempPath() + "Security Roles" + ".csv";
    commaIO      = new CommaIO(fileName,'W');


    commaIO.write("Security Role","Description","Duty name","Duty AOT name","Duty description");

    while select SecurityRole order by Name
        join grantTable where grantTable.SecurityRole == SecurityRole.RecId
        join securityTask where securityTask.RecId == grantTable.SecurityTask
        && SecurityTask.Type == SecurityTaskType::Duty

    {
       select securityTaskDef where securityTaskDef.RecId == securityTask.RecId;
     
       DutyName = securityTaskDef.Name ;
       // info(strFmt("%1", DutyName ));

        commaIO.write(SecurityRole.Name+"("+SecurityRole.AotName+")", SecurityRole.Description, securityTaskDef.Name ,securityTaskDef.AotName, securityTaskDef.Description);
    }

    WINAPI::shellExecute(fileName);
}

No comments:

Post a Comment