{SCIRPTING TIP} Filter Lookup field based on ownership in Dynamics CRM

Requirement: To filter a Lookup field options dependent on the record ownership for Lookup entity as the current user or his/her teams.

clip_image001

Description: The lookup field should be filtered based on lookup entities record owned by the current user or his/her teams.

Code:

This was achieved by adding Pre-filter to the lookup field as below:

function preFilterLookup()

{

      Xrm.Page.getControl(“lookupfieldname”).addPreSearch(function ()

{

// Filter the fetchxml ownership

fetchXml = “<filter type=’and’><condition attribute=’ownerid’ operator=’eq-useroruserteams’/></filter>”;

// Apply the filter to the field

Xrm.Page.getControl(“lookupfieldname”).addCustomFilter(fetchXml);

});

}

Note: Applies to CRM 2013 and up.

Hope it helps and Happy CRMing!

4 thoughts on “{SCIRPTING TIP} Filter Lookup field based on ownership in Dynamics CRM

  1. Using something very similar to this and script worked in version 2015 Update 1 but with upgrade to 2016 it is not returning an error but it is not applying the custom filter. Are you aware of any issues with preSearch combined with CustomFilter ? Below is my code and departmetnObject is not null or undefined.

    function preFilterDepartmentLookup()
    {
    debugger;
    Xrm.Page.getControl(“vbs_ruledepartmentid”).addPreSearch(function () {

    var departmentObject = Xrm.Page.getAttribute(“vbs_ruledepartmentid”).getValue();

    // Get the lookup Object, and extract the Value (ID and Text)
    if(departmentObject != null){
    var departmentTextValue = departmentObject[0].name;
    var departmentID = departmentObject[0].id;

    // Filter the cities by State
    var fetchXml = “” +
    ” ” +
    ” ” +
    ” ” +
    ” ” +
    ” ” +
    “”;

    // Apply the filter to the field
    Xrm.Page.getControl(“vbs_vbsrulecategory”).addCustomFilter(fetchXml);
    }

    });
    }

  2. Hi. I’m new to CRM. I’d like to know if ‘useroruserteams’ is a default or not? Should I change it to the name of the team? Thank you.

Leave a comment