Sunday, July 28, 2013

Record Level Security in Dynamics AX 2009

Looks like accessing record level security programatically in Dynamics AX 2009 is not that easy, mainly to mention about retrieval of restrictions. 

Before going into deep into this, I assume you might know basics of record level security configured in AX 2009. 


I would show you here on how to access the queries (Record level securities) defined in the Record Level security form. 


Finding it the hard way :


1. The Starting point is the Sys table - SysRecordLevelSecurity.
2. I quickly created a view to see what's in the table. 
    GroupId  tabId companyId  restriction RecId

But if you see here the restriction field does not show up. This is because it is a ranged query that is set in a container assigned to the Datasource as a Query when you setup using the form. When you debug it would show as a binary data type (BLOB) . 


So how to access this, 

There is no straight forward way to access this. Since its a query range attached in the datasource, you have to retrieve it by accessing from the datasource itself .  

Check out my code below. 


Query query = new Query();
SysQueryRun queryRun;
SysRecordLevelSecurity   sysRecordLevelSecurity;
anytype d;

appl.setDefaultCompany('ceu');

while select  * from sysRecordLevelSecurity {

        queryRun = new SysQueryRun(sysRecordLevelS0ecurity.Restriction);

        info(strFmt('Sec params: %1 Group name:%2 Table Name:%3',
                        queryRun.query().dataSourceNo(1).range(1).value() ,
                        sysRecordLevelSecurity.groupId,
                        tableId2Name(sysRecordLevelSecurity.tabId))
                        );

        /* clear the obj to release blob */
        queryRun=null;
}



This would give you the permission as a string delimited by periods. 

On how to create/ setup the record level security by code, there is a lot of code floating on the net. 

No comments:

Post a Comment