Skip to main content

Notifications

Dynamics 365 Community / Forums / Finance forum / How to get the active ...
Finance forum
Suggested answer

How to get the active security role on the table ?

Posted on by 25

Hi, I need to get the current security role that controls the current table

example :

"Social Table" exist on "Social Salary Role"

I need x++ code to show the "Social Salary Role" when the user open the "Social Table" 

 select securityRole
        join securityUserRole
            where securityUserRole.SecurityRole == securityRole.RecId
        join userInfo
            where userInfo.id == securityUserRole.User
             &&    userInfo.id == curUserId()

this code get all role that associate with current user

what condition I have to add to select the only active role

  • Suggested answer
    nmaenpaa Profile Picture
    nmaenpaa 101,154 on at
    RE: How to get the active security role on the table ?

    First of all, you're not opening tables, you're opening forms.

    Forms are opened by menu items, which are linked to privileges. And you can find the related roles by using the SystemSecurityPermissionEntity like I mentioned in my very first reply. This entity also contains the related security role.

    Then you can use SystemSecurityUserRoleEntity to find the roles of any (also current) user.

    I'm sorry but I don't think that I can provide any more help than I did in my first message. I hope you can find the solution by going through the already provided information one more time.

  • Oabida Profile Picture
    Oabida 25 on at
    RE: How to get the active security role on the table ?

    No,

    ("Social Table" exist on "Social Salary Role")

    I need this information from the code.

    when I open Any Table I want this Information

    "Which role is associated with the table I opened"

  • nmaenpaa Profile Picture
    nmaenpaa 101,154 on at
    RE: How to get the active security role on the table ?

    Sorry, I don't understand.

    Or do you just mean how to get the selected role (record) in the "Assign users to roles" form? So is your question: "how to get the selected record from the current form's data source"?

  • Oabida Profile Picture
    Oabida 25 on at
    RE: How to get the active security role on the table ?

    "Social Table" exist on "Social Salary Role"

    - pay group 'A' assigned to "Social Salary Role"

    - pay group 'B' assigned to "system user"

    when I open "Social Table" it fetches the record that has

    A & B pay group it's supposed to fetch only record that

    has 'A' pay group only

    "Social Table" is the current table

    and "Social Salary Role" is the current table role

  • nmaenpaa Profile Picture
    nmaenpaa 101,154 on at
    RE: How to get the active security role on the table ?

    Hi,

    and what do you mean by "current table role"?

  • Oabida Profile Picture
    Oabida 25 on at
    RE: How to get the active security role on the table ?

    sorry again let start from the beginning

    I need to create a Button Assign Pay group like

    Assign organization (Standard from Microsoft)

    everything works fine with the new button but it fetches

    all pay groups from all roles, I need the select statement to fetch only the pay group that is associated with the current table role.

    77485.Untitled.png

  • Suggested answer
    nmaenpaa Profile Picture
    nmaenpaa 101,154 on at
    RE: How to get the active security role on the table ?

    Hi,

    there's no function that would return "current role" of the user. And there can't be - the user can have (and usually must have) multiple role assigned (System user role is mandatory, and usually they have also other roles assigned).
    I think you might need to go back to your actual business requirement and start again from there. And if you need help, don't hesitate to share the business requirement with us. Thanks!

  • Oabida Profile Picture
    Oabida 25 on at
    RE: How to get the active security role on the table ?

    I apologize for the lack of clarity. The code works, but it gives the same results as before. I need to know the "RecId" for the role currently in use

    is there a code

    like curExt() or curUserId()

    but for the avtice Role

    I use this code on security policy to filter data so I need to know which role run this class

  • Suggested answer
    nmaenpaa Profile Picture
    nmaenpaa 101,154 on at
    RE: How to get the active security role on the table ?

    Hi Oadiba,

    I don't know what your code is supposed to do. Or if you have something else before this code.

    But let's take a look at the code that you shared.

    When you debug it, you will notice that SystemSecurityPermissionEntity.SecurityRoleName is empty because you haven't selected anything from SystemSecurityPermissionEntity.

    So your code is asking for all SecurityRole records where SecurityRole.Name is empty. Therefore it's not returning anything.

     

    If you need support in writing select statements and joins , I suggest to start here: https://docs.microsoft.com/en-us/dynamicsax-2012/developer/select-statements (including the other pages that it links to). But once you're familiar with select statements, you will find the data that you're looking for by using the entities that I mentioned.

     

  • Oabida Profile Picture
    Oabida 25 on at
    RE: How to get the active security role on the table ?

    Hi, thanks for the reply,

    I tried this code but it doesn't work :

    select firstonly securityRole

           where SystemSecurityPermissionEntity.SecurityRoleName == securityRole.name

           join securityUserRole

               where securityUserRole.SecurityRole == securityRole.RecId

           join userInfo

               where userInfo.id == securityUserRole.User

                &&    userInfo.id == curUserId()

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,802 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 229,133 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,154

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans