Search code examples
axaptadynamics-ax-2012x++

How to get a list of Companies User Access in Ax 2012?


In Ax 2009, to get a list of companies by user I get it of this way:

static container getAllCompanies()
{
     Container companies;
     DataArea dataArea;
     CompanyDomainList companyDomainList;
     AccessRightsList  accessRightsList;
    UserGroupList        userGroupList;


//select all the companies in which current user’s access level is higer than NoAccess
     while select id,name from DataArea
           Exists join companyDomainList
           where companyDomainList.companyId == dataArea.Id
           join accessRightsList
           where accessRightsList.domainId   ==  companyDomainList.domainId && accessRightsList.accessType > AccessType::NoAccess
           join userGroupList
           where userGroupList.groupId       == accessRightsList.groupId && userGroupList.userId == curuserid()
     {
          companies += [dataArea.name,dataArea.Id];
     }

     return companies;
}

but in Ax 2012, I don't have the CompanyDomainList table, how can I get a list of companies that user has access?


Solution

  • User and copmany information is saved in the OMUserRoleOrganization table.

    UserId userId = curUserId();
    
    CompanyInfo companyInfo;
    OMUserRoleOrganization oMUserRoleOrganization;
    container result;
    
    while select companyInfo
        exists join oMUserRoleOrganization
            where oMUserRoleOrganization.OMInternalOrganization == companyInfo.RecId
                && oMUserRoleOrganization.User == userId
    {
        result += [companyInfo.Name, companyInfo.DataArea];
    }
    
    if (!result)
    {
        // no specific company for user --> all
        while select companyInfo
        {
            result += [companyInfo.Name, companyInfo.DataArea];
        }
    }