web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Need help on filtering contents of Lookup using dynamic values of logged in user teams

(0) ShareShare
ReportReport
Posted on by

Need help on filtering contents of Lookup using dynamic values of logged in user teams. 

I am getting user team ids(and user team names) of logged in user. 

On the basis of this data I need to filter lookup field named UserTeam. 

Each logged in user should see only teams that it belongs to in UserTeam lookup field dropdown. 

Please help.

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    Hi  Karan,

    You can add one custom fetchXML or filter in the lookup. Try to make the filter from advanced find.

    www.magnetismsolutions.com/.../crm-2013-javascript-lookup-filtering-using-addcustomfilter

    Hope this helps.

  • Verified answer
    Shahbaaz Ansari Profile Picture
    6,211 on at

    Hi Karan,

    You need to write javascript code for this which you need to call on page load event.

    below is the code,

    *****************************************************************************

    function preFilterLookup() {  

    Xrm.Page.getControl("lookupschemaname").addPreSearch(function () {

        addLookupFilter();

      });

    }

    function addLookupFilter() {

     var teamid = guid//this is the team id which you have and need to pass in below

       if (email != null) {

           fetchXml = "<filter type='and'><condition attribute='teamid' operator='eq' value='" + teamid + "' /></filter>";  

    Xrm.Page.getControl("lookupschemaname").addCustomFilter(fetchXml);

       }

    }

    *****************************************************************************

    Please check below article on how to filter lookup field,

    www.magnetismsolutions.com/.../crm-2013-javascript-lookup-filtering-using-addcustomfilter

    In case you find any issue, Please let me know.

    If you find it helpful, Please mark as Verified.

    Best Regards,

    Shahbaaz

  • Community Member Profile Picture
    on at

    Hello @Shahbaaz,

    Thanks for your prompt response. I really appreciate your help.

    I have multiple team ids. Some user belongs to 2 teams, other belongs to 4 teams. I have exactly this concern that how I am going to use them in the filter?

  • gdas Profile Picture
    50,091 Moderator on at

    You can get user all  teams by user id-

    Xrm.Page.context.getUserId()

    getUserTeams (userid)

    In CRM 2016 ODATA v8.1:

    customer.crm.dynamics.com/.../teams$select=name&$expand=teammembership_association($filter=systemuserid eq 014853d6-4f9f-e611-80e6-c4346bacbcbc)

    Result:

    {

     "@odata.context":"customer.crm.dynamics.com/.../v8.1$metadata#teams(name,teammembership_association)","value":[

       {

         "@odata.etag":"W/\"596262\"","name":"Contoso","teamid":"56031a5c-4296-e611-80e4-c4346bad0070","ownerid":"56031a5c-4296-e611-80e4-c4346bad0070","teammembership_association":[

         ],"teammembership_association@odata.nextLink":"customer.crm.dynamics.com/.../teammembership_association$filter=systemuserid%20eq%20014853d6-4f9f-e611-80e6-c4346bacbcbc"

       }

     ]

    }

    Hope this helps.

  • Verified answer
    Shahbaaz Ansari Profile Picture
    6,211 on at

    You need to use In operator and a for loop for creating dynamics value for fetch xml.

    below is the code,

    **************************************************************

    function preFilterLookup() {  

    Xrm.Page.getControl("lookupschemaname").addPreSearch(function () {

        addLookupFilter();

      });

    }

    function addLookupFilter() {

     var teamid = guid//this is the team id which you have and need to pass in below

    var fetchvalue='';

    for (int i = 0; i < teamid.Count; i++)

    {

    //add all the teamid

    fetchvalue += '<value>' + teamid[i] + '</value>';

    }

       if (email != null) {

           fetchXml = "<filter type='and'><condition attribute='teamid' operator='in'>'"+fetchvalue+"'</filter>";  

    Xrm.Page.getControl("lookupschemaname").addCustomFilter(fetchXml);

       }

    }

    **************************************************************

    If you find it helpful, Please mark as Verified.

    Best Regards,

    Shahbaaz

  • Community Member Profile Picture
    on at

    Hello Shahbaaz,

    Thanks for detailing on that. I am actually going through the same approach. Not sure where I am doing a mistake. Here is my code.

    function preFilterLookup() {

       console.log('preFilterLookup');

       addPreFilter('userteamid', 'userteamid');

    }

    function addPreFilter(control_id, filterAttribute){

    var utms = getUserTeam();

    console.log("utms : "+ utms);

    var appender = "";

    for(var i=0; i < utms.length;i++){

    appender+="<value uitype='team'>"+utms[i]+"</value>";

    appender+="value='"+utms[i]+"'";

       }

    console.log("appender : " + appender);

    var fetchXml =

        "<filter type='or'>" +

               "<condition attribute='"+filterAttribute+"' operator='in' value=>" + appender + "</condition>" +

        "</filter>";

       console.log("filterXml : "+ filterXml);

    filterXml = "<filter type='or'><condition attribute='" + filterAttribute + "' operator='in' value='" + utms + "' /></filter>";

       console.log("Xrm.Page.getControl(control_id) : "+ Xrm.Page.getControl(control_id) + ", control_id : " + control_id);

       Xrm.Page.getControl(control_id).addCustomFilter(filterXml);

    }

  • Suggested answer
    Shahbaaz Ansari Profile Picture
    6,211 on at

    Please try below code

    **********************************************************

    function preFilterLookup() {  

    Xrm.Page.getControl("userteamid").addPreSearch(function () {

        addLookupFilter('userteamid', 'userteamid');

      });

    }

    function addLookupFilter(control_id, filterAttribute) {

    var utms = getUserTeam();

    console.log("utms : "+ utms);

    var appender = "";

    for(var i=0; i < utms.length;i++){

    appender+='<value>"'+utms[i]+'"</value>';

    }

    console.log("appender : " + appender);

    var fetchXml =

      "<filter type='or'>" +

            "<condition attribute='"+filterAttribute+"' operator='in'>" + appender + "</condition>" +

      "</filter>";

      Xrm.Page.getControl(control_id).addCustomFilter(filterXml);

    }

    **********************************************************

    If you find it helpful, Please mark as Verified.

    Best Regards,

    Shahbaaz

  • Suggested answer
    Shahbaaz Ansari Profile Picture
    6,211 on at

    make sure you are calling the “preFilterLookup” function on load of the form

  • Community Member Profile Picture
    on at

    H?!
     
    It seems like you have to create additional system view for team entity and set it to teams lookup field on your form.
    Here is an example:
     
    47527.Screenshot_5F00_1.png

    Hope it helps

    Please mark it As Answered or Verified if it did help you.

    Thanks

    Vlad
    dynamicalabs.com

  • Community Member Profile Picture
    on at

    After trying above code, my appender and fetchXML are as follows.

    appender : <value>"{8fb0f1ce-b94b-e711-80be-000c29f1ce86}"</value><value>"{a67ff0c2-2d65-e711-80bf-000c29f1ce86}"</value><value>"{5eec988c-4065-e711-80bf-000c29f1ce86}"</value><value>"{547a6d5d-6427-e811-80c5-000c29f1ce86}"</value>

    fetchXml : <filter type='or'><condition attribute='userteamid' operator='in'><value>"{8fb0f1ce-b94b-e711-80be-000c29f1ce86}"</value><value>"{a67ff0c2-2d65-e711-80bf-000c29f1ce86}"</value><value>"{5eec988c-4065-e711-80bf-000c29f1ce86}"</value><value>"{547a6d5d-6427-e811-80c5-000c29f1ce86}"</value></condition></filter>

    Can you suggest what could be wrong here?

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
ScottDurow Profile Picture

ScottDurow 2

#2
GJones Profile Picture

GJones 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans