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

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

filtering a lookup field

(0) ShareShare
ReportReport
Posted on by 2,665

Hi,

I am trying to filter a lookup field using javascript PreSearch event.
I want the lookup to filter the current user's teams(that he belongs to). So far, I've tried this but I don't see the filtering done in the lookup field. Could someone please look into this and guide me. Thanks.

Here is my script:

function getUserTeam()
{
debugger;
addEventHandler();
}

function addEventHandler() {
    // add the event handler for PreSearch Event
    Xrm.Page.getControl("new_userteam").addPreSearch(addFilter()
{
var TeamId= getTeamId();
 
    //create a filter xml
   var filter = "<filter type='and'><condition attribute='teamid' operator='eq' value='" + TeamId+ "'/></filter>";
 
    //add filter
    Xrm.Page.getControl("new_userteam").addCustomFilter(filter);

});
}


function getTeamId(){
    var myTeamId = null;
    var myTeamName = null;
 
    var guid = "[A-z0-9]{8}-[A-z0-9]{4}-[A-z0-9]{4}-[A-z0-9]{4}-[A-z0-9]{12}";
 
    var serverUrl = Xrm.Page.context.getClientUrl();
    var userId = Xrm.Page.context.getUserId();
    userId = userId.match(guid);
    userId = userId.input; 
 
    // Retrieve TeamIds of the Teams Current SystemUser belongs to
    var userTeamsQuery = "TeamMembershipSet?$select=TeamId&$filter=SystemUserId eq guid'"+ userId +"'";
    var userTeams = makeRequest(userTeamsQuery);
 
    var teamQuery = null;
    var teams = null;
 
    if(userTeams !== null){
 
        for(var i = 0; i< userTeams.length; i++){
 
            userTeams[i] = userTeams[i].TeamId;
 
           
            var teamId = "{" + userTeams[i] + "}";
            return teamId;
    }
  }
}

function makeRequest(query) {
    var serverUrl = Xrm.Page.context.getClientUrl();
 
    var oDataEndpointUrl = serverUrl + "/XRMServices/2011/OrganizationData.svc/";
    oDataEndpointUrl += query;
 
    var service = GetRequestObject();
 
    if (service != null) {
        service.open("GET", oDataEndpointUrl, false);
        service.setRequestHeader("X-Requested-With", "XMLHttpRequest");
        service.setRequestHeader("Accept", "application/json, text/javascript, */*");
        service.send(null);
 
        var retrieved = JSON.parse(service.responseText).d;
 
        var results = new Array();
        for (var i = 0; i < retrieved.results.length; i++) {
            results.push(retrieved.results[i]);
        }
 
        return results;
    }
    return null;
}


function GetRequestObject() {
    if (window.XMLHttpRequest) {
        return new window.XMLHttpRequest;
    } else {
        try {
            return new ActiveXObject("MSXML2.XMLHTTP.3.0");
        } catch (ex) {
            return null;
        }
    }
}


*This post is locked for comments

I have the same question (0)
  • Suggested answer
    MilindP Profile Picture
    1,019 on at
    RE: filtering a lookup field

    See this community.dynamics.com/.../166943

  • Verified answer
    Aiden Kaskela Profile Picture
    19,696 on at
    RE: filtering a lookup field

    Hi,

    You can do this without custom code. If I understand, you have a lookup to Teams, and you want to only show teams the person belongs to (via new_userteam)

    1. Create a new view on team entity, link to user team, link to User, and add a filter where the User "Equals Current User". This view will return the teams he's a part of. 
    2. Edit the Team lookup on the form
        1. In the Display tab, Additional Properties section, change the Default View to your new query
        2. You can toggle the View Selector option if you want to force them to stay on the view you created

     

    Hope this helps Roxanna. If so, I would appreciate it if you would mark this as the verified answer. 

     

    Thanks! Aiden

  • crmprogrammer2013 Profile Picture
    2,665 on at
    RE: filtering a lookup field

    Thanks Aiden. It worked like a charm.

    How did I miss such a nice workaround? Then in what scenarios custom code approach will be used? Just curious.

  • Aiden Kaskela Profile Picture
    19,696 on at
    RE: filtering a lookup field

    Glad it worked! I guess you would use that code if you needed to build up a query that wasn't possible to build a view for, like if you had to pull in fields from your current record and inject it as a condition in the query. I haven't done that before so its just a guess.

  • crmprogrammer2013 Profile Picture
    2,665 on at
    RE: filtering a lookup field

    Hey thanks man. Appreciate it! :)

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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
HR-09070029-0 Profile Picture

HR-09070029-0 2

#1
UllrSki Profile Picture

UllrSki 2

#3
ED-30091530-0 Profile Picture

ED-30091530-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans