In my case, I need to add custom filtering when I select a lookup it will populate another lookup. when the user will click on the search icon it will show only filter data.
I was following: https://docs.microsoft.com/en-us/powerapps/developer/model-driven-apps/clientapi/reference/controls/addpresearch
sample code:
var demoLAB = demoLAB || {};
var classId;
demoLAB.Stuedent = {
Form: {
Load: function (executionContext) {
'use strict';
var formContext = executionContext.getFormContext();
this.attachEvents(executionContext);
},
attachEvents: function (executionContext) {
'use strict';
var formContext = executionContext.getFormContext();
var form = demoLAB.Stuedent.Form;
// Student Change Event
formContext.getAttribute("demo_studentId").addOnChange(form.studentOnChange);
},
studentOnChange: function (executionContext) {
'use strict';
var formContext = executionContext.getFormContext();
var form = demoLAB.Stuedent.Form;
if (formContext.getAttribute("demo_studentId").getValue() != null) {
var studentId = formContext.getAttribute("demo_studentId").getValue()[0].id.slice(1, -1);
// Retrive current student current class
Xrm.WebApi.retrieveRecord("student", studentId, "?$select=_classId_value").then(
function success(studentResult) {
classId = studentResult._classId_value;
// Add presearch for teacher
formContext.getControl("demo_teacherId").addPreSearch(form.filterTeacher);
},
function (error) {
}
);
}
},
// Call back function for teacher
filterTeacher: function (executionContext) {
'use strict';
var formContext = executionContext.getFormContext();
var teacherFilter = "<filter type='and'><condition attribute='demo_classId' operator='eq' value='" + classId + "'/></filter>";
formContext.getControl("demo_teacherId").addCustomFilter(teacherFilter, "teacher");
},
}
};
Comments
Post a Comment