Looking for PowerObjects? Don’t worry, you’re in the right place! We’ve been part of HCL for several years, and we’ve now taken the final step in our acquisition journey: moving our website to the HCL domain. Nothing else is changing – we are still fanatically focused on Microsoft Business Applications!

PowerObjects Blog 

for Microsoft Business Applications


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

Post Author: Joe D365 |

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) {
Xrm.Page.getControl("requiredattendees").addPreSearch(Filter);
}
}

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 = "" + "" + "";
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!

Joe CRM
By Joe D365
Joe D365 is a Microsoft Dynamics 365 superhero who runs on pure Dynamics adrenaline. As the face of PowerObjects, Joe D365’s mission is to reveal innovative ways to use Dynamics 365 and bring the application to more businesses and organizations around the world.

One comment on “Filtering PartyList Fields with JavaScript: The addPreSearch Method Joins the Party”

  1. Hello Joe

    I followed you all the way to the last line where you addCustomFilter to "optionalattendees". When and where did the field "optionalattendees" come in?

    I am trying to filter an attendees party list much in the same way. Contacts are attached to different accounts and I want the party list to only show contacts related to the specified account field called Client. I'm attempting to adapt your code to mine but "optionalattendees" threw me at the end. Could you clarify that for me?

    Thank You,
    Carlos

PowerObjects Recommends