Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Answered

Filter LookUp records based on current user signed in and specific security role

(0) ShareShare
ReportReport
Posted on by 879

Hello,

I would like to filter the lookup field to populate only the records that are related to the current manager signed in and based on a specific role assigned to him.
For example, Manager A has the security role A and the below team:
User A, User B, User C

Manager B has the security role A and the following team: User D, User E

When the form is loaded, and manager A is the user signed in, the only records that should be filtered in the lookup are User A, B, and C and he can't access User D or User E because those users do not belong to him.

In addition, if the current user has the system admin role he should be able to see and search for all the records.

Below is a screenshot that shows the manager field (LookUp on the user's table) assigned for each resource:
Resources-Managerss.pngResourceLookup.png

What's the best way to achieve the above requirement? Can please someone provide a sample code?

Any help is highly appreciated.

Best regards,
EBMRay

  • EBMRay Profile Picture
    879 on at
    RE: Filter LookUp records based on current user signed in and specific security role

    Hi ,

    Thank you so much for providing the updated code version.

    It was the brackets issue that is being returned from the current userID and now it is working perfectly.

    Best regards,

    EBMRay

  • Verified answer
    Community Member Profile Picture
    on at
    RE: Filter LookUp records based on current user signed in and specific security role

    Hi EBMRay,

    Please try this:

     

    function makeCondition(attribute, operator, value) {
        return "";
    }
    function makeFilter(type, body) {
        return ""   body   "";
    }
    function onLoad(executionContext) {
        let formContext = executionContext.getFormContext();
        let userSettings = Xrm.Utility.getGlobalContext().userSettings;
        const roles = userSettings.roles.getAll();
        let hasRole = false;
        roles.forEach(function (item) {
            console.log("role item ", item);
            if (item.id === "2239204d-437c-ec11-8d21-000d3ab2f7c8") {
                hasRole = true;
            }
        });
        if (hasRole) {
            const label = "cr884_";
            let customFilter = makeFilter("and", makeCondition("new_managername", "eq", userSettings.userId.replace(/({|})/gm, "")));
            console.log("customFilter ", customFilter);
            Xrm.Page.getControl(label   "resourceid").addPreSearch(() => {
                Xrm.Page.getControl(label   "resourceid").addCustomFilter(customFilter,"cr884_resource");
    
            });
        }
    }
    

    I didn't test it. Hope it could work for you.

  • EBMRay Profile Picture
    879 on at
    RE: Filter LookUp records based on current user signed in and specific security role

    Hi Bipin Kumar ,

    Thank you for providing those helpful documentation.

    I was able to retrieve the current user ID and the security role name needed. However, I am still stuck on the custom filter part to retrieve the records from the other table that belongs to the current user.

    Below is the code that I prepared:

    function makeCondition(attribute, operator, value) {
        return "";
    }
    function makeFilter(type, body) {
        return ""   body   "";
    }
    function onLoad(executionContext) {
        let formContext = executionContext.getFormContext();
        let userSettings = Xrm.Utility.getGlobalContext().userSettings;
        const roles = userSettings.roles.getAll();
        let hasRole = false;
        roles.forEach(function (item) {
            console.log("role item ", item);
            if (item.id === "2239204d-437c-ec11-8d21-000d3ab2f7c8") {
                hasRole = true;
            }
        });
        if (hasRole) {
            const label = "cr884_";
            let customFilter = makeFilter("and", makeCondition(label   "userid", "eq", userSettings.userId));
            console.log("customFilter ", customFilter);
            Xrm.Page.getControl(label   "resourceid").addPreSearch(() => {
                Xrm.Page.getControl(label   "resourceid").addCustomFilter(customFilter);
    
            });
        }
    }

    The FetchXML query that I exported from the advanced find that is hardcoded and which will fetch the records based on the value GUID provided and not based on current user signed in:

    
    
    
    
    
    
    
    

    What should be the updated/corrected code version to make the filter works as expected and retrieve the desired records based on the current user signed-in? Could you please update it?

    Looking forward to your response.

    Best regards,
    EBMRay

  • Verified answer
    Bipin D365 Profile Picture
    28,961 Moderator on at
    RE: Filter LookUp records based on current user signed in and specific security role

    Hi,

    You can use below code to get logged user id -

    var userSettings = Xrm.Utility.getGlobalContext().userSettings;

    var loggedInUserId=userSettings .userId;

    docs.microsoft.com/.../usersettings

    To get logged in user security roles - docs.microsoft.com/.../usersettings

    Please mark my answer verified if this is helpful!

    Regards,

    Bipin Kumar

    Follow my Blog: xrmdynamicscrm.wordpress.com/

  • EBMRay Profile Picture
    879 on at
    RE: Filter LookUp records based on current user signed in and specific security role

    Hello Bipin Kumar ,

    Thank you for providing the mentioned blog post.

    I will give it a try to use the addCustomFilter function. Could you please provide a resource on how to get the current user security role and how to get the current user signed in in the FetchXML query?

    After that, I will try to build my code and post it back in case I am stuck.

    I look forward to your response.

    Best regards,

    EBMRay

  • Suggested answer
    Bipin D365 Profile Picture
    28,961 Moderator on at
    RE: Filter LookUp records based on current user signed in and specific security role

    Hi,

    Have you tried using addcustomfilter to filter your lookup field ?

    www.inogic.com/.../

    Please mark my answer verified if this is helpful!

    Regards,

    Bipin Kumar

    Follow my Blog: xrmdynamicscrm.wordpress.com/

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,884 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,758 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Product updates

Dynamics 365 release plans