Filtering PartyList Fields with JavaScript: The addPreSearch Method Joins the Party

PartyList fields can be a very powerful component of activities in Dynamics CRM. Otherwise known as the Activity Party, the PartyList controls what you see in the To, From, and CC lookup fields of an activity. Sometimes you need a little more control over the UI/UX surrounding the PartyList! Currently, there is no out-of-the-box method to filter the PartyList fields in CRM 2015, so you have to utilize JavaScript instead. In today's blog, we will show you how you can filter PartyList fields using JavaScript and the addPreSearch method.

The business requirement we have today filters the PartyList field "Attendees" on a CRM appointment based on the "Regarding" field that is populated on an Account.

Specifically, we want to check the "Regarding" value on an Appointment, and apply the following filtering logic: If the value in "Regarding" is an Account, filter the "Attendees" field to only show Contacts who work at that Account as available options in the Contacts portion of Attendees.

As we mentioned before, there is no out-of-the-box method to filter the PartyList fields in CRM 2015, so we'll have to utilize JavaScript to achieve our goal instead.

The primary methods we invoke are the addPreSearch method combined with the addCustomFilter method. The actual script is fairly simple and should contain two functions. See below:

function FilterPartyList() {
    var Regarding = Xrm.Page.getAttribute("regardingobjectid").getValue();
//checking if regarding fields is empty before we apply the filter
    if (Regarding != null) {

function Filter() {

    var RegardingValue = Xrm.Page.getAttribute("regardingobjectid").getValue();

    //if Regarding has a value, proceed
    if (RegardingValue != null) {
        //used to retrieve Name of the Account held in the Regarding field
        var RegardingTextValue = RegardingValue[0].name;
        //GUID used in filter
        var RegardingID = RegardingValue[0].id;
        var plist_filter = "<filter type='and'>" + "<condition attribute='parentcustomerid' operator='eq' name='" + RegardingTextValue + "' value='" + RegardingID + "' />" + "</filter>";
        Xrm.Page.getControl("optionalattendees").addCustomFilter(plist_filter, "contact");

Once your scripts are in libraries, add them to the Appointment entity form. In the Form Properties area, we'll need to call the scripts. We'll want to call our FilterPartyList function OnLoad of the form, and OnChange of the "Regarding" field:

Control Event Library Function
Form OnLoad PartyList_Filter Library FilterPartyList
Regarding (field) OnChange PartyList_Filter Library FilterPartyList

This can be achieved in the Form Editor, in Form Properties. Once deployed and pubished, you'll want to be sure to test out your work:

1. If an Appointment is without "Regarding" populated, no filtering will be applied to the "Required" field and the user can select Contacts/Accounts freely:

PartyList Fields

When you click the "Regarding" field above, you will see a full list of Contact records to choose from.

PartyList Fields

2. If the Appointment has the "Regarding" field populated with a Contact, the filter will be applied. As you can see in the example below, Susan Burk works at A. Datum Corp.

PartyList Fields
When you click on the "Required" field shown above, and attempt to add a Contact to the PartyList, you will be prompted with a filtered list of Contacts who work at A. Datum Corp.

PartyList Fields

That's all there is to it! Be sure to check out our blog on using the addPreSearch/addCustomFilter to a regular look-up field and subscribe so you don't miss any of our great CRM blog content. Did you know that PowerObjects offers a wide variety of education and training classes, including Boot Camps for CRM 2016? Check out all of our events today!

Happy CRM'ing!