Requirement: To filter a Lookup field options dependent on the record ownership for Lookup entity as the current user or his/her teams.
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!
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);
}
});
}
Hi Susan, i see that fetchxml string is empty in your script?
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.
Hi Ali,
Depends on where you are using it – what is the requirement?
Regards,
Deepesh