Introduction:
Recently we had a business requirement where client wants to filter PartyList field by selected account in regarding field on activity entity form.
As shown in below image, when we click on party list field, by default it shows data of many entities (i.e. Account, Contact, and User etc.)
Our client requirement is to filter PartyList field “Required Attendees” and show only Contact based on Account selected in “Regarding” field and that should be supported in UCI.
To achieve this we have developed JavaScript, which apply addPreSearch to PartyList field “Required Attendees”.
Here we used “addCustomFilter” function to apply custom condition and “setEntityTypes” function to set entity of contact type.
For reference please find JavaScript code as below:
function filterRequiredAttendees(executionContext) {
var functionName = “filterRequiredAttendees: “;
try {
// get formContext
var formContext = executionContext.getFormContext();
// Get requiredattendees
var regardingObject = formContext.getAttribute(“regardingobjectid”);
// Validate regarding field
if (!isValid(regardingObject)) {
return;
}
// Validate value of regardingObject
if (regardingObject.getValue() == null) {
return;
}
// Get requiredattendees control
var requiredAttendeesControl = formContext.getControl(“requiredattendees”);
// Validate requiredattendees field
if (isValid(requiredAttendeesControl)) {
// Add PreSearch
requiredAttendeesControl.addPreSearch(filterContactByAccount);
// Check if multiple type dropdowns enabled for this lookup
if (requiredAttendeesControl.getEntityTypes().length >= 1) {
// Set entity type to contact
requiredAttendeesControl.setEntityTypes([‘contact’]);
}
}
}
catch (e) {
Xrm.Navigation.openAlertDialog({ confirmButtonLabel: “Ok”, text: e.message, title: functionName });
}
}
function filterContactByAccount(executionContext) {
var functionName = “filterContactByAccount: “;
try {
// get formContext
var formContext = executionContext.getFormContext();
var regardingObjectValue = formContext.getAttribute(“regardingobjectid”).getValue();
// Validate Regarding has a value
if (regardingObjectValue != null) {
// Get Account GUID
var regardingID = regardingObjectValue[0].id;
var filterCondition = “<filter type=’and’>” + “<condition attribute=’parentcustomerid’ operator=’eq’ value='” + regardingID + “‘ />” + “</filter>”;
formContext.getControl(“requiredattendees”).addCustomFilter(filterCondition, “contact”);
}
}
catch (e) {
Xrm.Navigation.openAlertDialog({ confirmButtonLabel: “Ok”, text: e.message, title: functionName });
}
}
function isValid(attributes) {
var functionName = “isValid >>”;
try {
if (attributes != null && attributes != undefined && attributes != “” && attributes != “undefined” && attributes != “null”) {
return true;
}
else {
return false;
}
} catch (e) {
Xrm.Navigation.openAlertDialog({ confirmButtonLabel: “Ok”, text: e.message, title: functionName });
}
}
Add this scripts as web resources, add these libraries to the activity entity form, then we have to set the Form Properties, set “filterRequiredAttendees” function “OnLoad” of the form, and “OnChange” of the “Regarding” field.
Deploy and publish the changes. Refresh (Ctr+F5) the activity entity form and we are good to filter partylist field.
Now PartyList field “Required Attendees”, shows only Contact based on Account selected in “Regarding” field.
Conclusion:
Using the “addPreSearch”, “addCustomFilter”, and “setEntityTypes” functions we can filter PartyList fields that supports on UCI too.
Dynamics 365 for Customer Engagement apps version 9.x provided the following client APIs, which are not available in the previous versions.
1. getEntityTypes – Gets the types of entities allowed in the lookup control
Syntax: formContext.getControl(arg).getEntityTypes();
2. setEntityTypes – Sets the types of entities allowed in the lookup control.
Syntax: formContext.getControl(arg).setEntityTypes([entityLogicalNames]);
Hi,
I have tried the same in my custom entity but customfilter is not working in UCI.
Can you please help with this!!
Thanks.
Could you please let us know the actual issue with an error message that you faced during this so that we can try to replicate it and provide you solution.
Thanks!
Hi,
I have added 3 lookup types, for example [“contact”, “account”, “systemuser] and applied presearch and custom filter only Contact based on Account selected in “Regarding” field.(As explained in article)
formContext.getControl(arg).setEntityTypes( [“contact”, “account”, “systemuser] );
But default entity showing account instead contact. But If I select contact from the 3 entities, filter is applied.
****Problem: Default entity type is not showing contact.
**But in Web Interface, deault entity type showing contact.
Hi,
We have tried the same code in trial environment for ‘Appointment’ and ‘Phonecall’ activity entity and it is working as expected.
When ‘Regarding’ field contains ‘Account’ entity then it displays related ‘Contact’ record in ‘Required Attendees’ lookup field as mentioned in the blog.
It would be much better if you could provide more details on this so that we can help.
We also have a query:
Have you ever registered ‘filterRequiredAttendees’ function on onLoad event of the form as mentioned in the blog?
Thanks!