web
You’re offline. This is a read only version of the page.
close
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 905

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

I have the same question (0)
  • Suggested answer
    Bipin D365 Profile Picture
    28,983 Moderator on at

    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/

  • EBMRay Profile Picture
    905 on at

    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

  • Verified answer
    Bipin D365 Profile Picture
    28,983 Moderator on at

    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
    905 on at

    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
    Community Member Profile Picture
    on at

    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
    905 on at

    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

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Siv Sagar Profile Picture

Siv Sagar 93 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 74

#3
Martin Dráb Profile Picture

Martin Dráb 64 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans