{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!