Saturday, April 17, 2010

Create RLS via X++

// Sample code of how to create RLS via X++
static void mydSXP_TestInsertRLS(Args _args)
{
    Query                   q;
    SysQueryRun             queryRun;
    sysRecordLevelSecurity  sysRecordLevelSecurity,sysRecordLevelSecurity_old;
    ;

    q = new Query();

    q.addDataSource(tablenum(CustTable));
    SysQuery::findOrCreateRange(q.dataSourceNo(1),fieldnum(CustTable,CustGroup)).value(queryValue(10) +','+queryValue(20)+','+queryValue(30));

    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        =   77;
    sysRecordLevelSecurity.companyId    =   'SJH';
    sysRecordLevelSecurity.groupId      =   'SJH_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();
        }
    }

}

No comments:

Post a Comment