Skip to main content

Record level security (AX 2009)

Hi, Let me walk-through how we can create record level security through code.

When you create record level security from front end, sometimes wizard doesn't show you the exact form or I can say it doesn't capture the actual table query.

So in that case for some tables or form, we can write the job in AOT and executing it results in creating record in record level security form.

static void RecordLevelSecurityEx(Args _args)
{
    Query                   q;
    SysQueryRun             queryRun;
    sysRecordLevelSecurity  sysRecordLevelSecurity,sysRecordLevelSecurity_old;
    ;

    q = new Query();

    q.addDataSource(tablenum(SalesQuotationTable));
    SysQuery::findOrCreateRange(q.dataSourceNo(1),fieldnum(SalesQuotationTable,Createdby)).value('Admin');

    queryRun    =   new SysQueryRun(q);

    // Need to have a RecordSortList and insert will happen for one time only, loop of companies is needed
    sysRecordLevelSecurity.clear();
    sysRecordLevelSecurity.TabId        =   1967;
    sysRecordLevelSecurity.companyId    =   'ARSP';
    sysRecordLevelSecurity.groupId      =   'TEST';
    sysRecordLevelSecurity.restriction  =   queryRun.pack();

    // TODO - Check how to update
    // Need to check for the primark key combination
    // TabId,groupId,companyId
    if(sysRecordLevelSecurity.validateWrite())
    {
        select firstonly sysRecordLevelSecurity_old
        where sysRecordLevelSecurity_old.tabId  ==  sysRecordLevelSecurity.tabId    &&
        sysRecordLevelSecurity_old.companyId    ==  sysRecordLevelSecurity.companyId    &&
        sysRecordLevelSecurity_old.groupId      ==  sysRecordLevelSecurity.groupId;

        if(sysRecordLevelSecurity_old)
        {
            ttsbegin;
            sysRecordLevelSecurity_old.selectForUpdate(true);
            sysRecordLevelSecurity_old.restriction  =   sysRecordLevelSecurity.restriction;
            sysRecordLevelSecurity_old.write();
            ttscommit;
        }
        else
        {
            sysRecordLevelSecurity.selectForUpdate(true);
            sysRecordLevelSecurity.write();
        }
    }

}



Happy Daxing JJ

Comments