Dynamically Filtering a Lookup on a Creatio Freedom UI Page

Filtering lookups on a Freedom UI page is something new in 8.0.10. However, even in 8.0.10 there are still some issues with filtering lookups. I do expect lookup filtering to improve for Freedom UI in versions to come, however, there is still a need to be able to dynamically filter a lookup with complex conditions at runtime. This article will outline how to do this for a lookup on Freedom UI pages, giving you full control over the conditions for the filter.

Note: When a lookup is opened on a Freedom UI page, the lookup makes a crt.LoadDataRequest request to obtain its data. This request can be handled/intercepted and filter conditions can be added so the data received by the lookup is filtered to match the provided conditions.

To filter a lookup on a Freedom UI page, we’ll need to know the attribute name for the lookup field in the model, and then we’re add a handler for any time anything on the page makes a request to load it’s datasource. If the requested datasource is for our lookup’s bound field, we’ll apply a filter to the request. In the example for this article, we’ll use a scenario of filtering the Case contact lookup to only show contacts for the selected Account on the Case. In 8.0.10, this can be implemented as a business rule, however, we’re just using this as an example (which also could be added to 8.0.9 now as well).

First, we’ll need to locate the lookup control so we can determine it’s bound attribute name. For the case page, we’ll see the following for the Contact lookup in the viewConfigDiff for the page:

Notice the name of the attribute the control is bound to (highlighted in the screenshot). This will correspond with the following attribute in the viewModelConfig, which shows it’s bound to the Contact column for the Case:

For the case page, the “LookupAttribute_c08bwtk” attribute is what we’ll need to look for when it requests that it’s data source is loaded. The name of the datasource will be the attribute name followed by “_List_DS”. For this attribute, we’ll be looking for the request to load the data for the datasource named “LookupAttribute_c08bwtk_List_DS”.

The request handler will look as follows (Note, be sure to also add the “@creatio-devkit/common” to the page as sdk):

{
	request: "crt.LoadDataRequest",
	handler: async (request, next) => {
		// filter the contact lookup for the account
					
		if(request.dataSourceName !== "LookupAttribute_ctwt6pv_List_DS") {
			return await next?.handle(request);
		}

		// get the account					
		const account = await request.$context.LookupAttribute_c08bwtk;
		if (account) {
			const filter = new sdk.FilterGroup();
			await filter.addSchemaColumnFilterWithParameter(sdk.ComparisonType.Equal, "Account", account.value);

			// note, these lines are only needed due to an issue with filters in Creatio-DevKit SDK
			// expected to be fixed in Creatio 8.1
			const newFilter = Object.assign({}, filter);
			newFilter.items = filter.items;

			request.parameters.push({
				type: "filter",
				value: newFilter
			});
		}
					
		return await next?.handle(request);
	}
}

The end result will be that the Contact lookup will only show the contact’s for the Account on the Case.

One side note: As of Creatio 8.0.10, lookups only request their data once. Meaning, the first time the user clicks the dropdown. So, if you set a filter, then the user changes the fields you’re using for the filter, it won’t fire it’s crt.LoadDataRequest request again, allowing you to apply the filter again based on the new values (it will still use the original values). I’m sure this will change in future versions, but this is how it works as of 8.0.10.

Want content like this delivered to your inbox? Sign up for our newsletter!
ABOUT THE AUTHOR

Ryan Farley

Ryan Farley is the Director of Development for Customer FX and creator of slxdeveloper.com. He's been blogging regularly about SalesLogix, now Infor CRM, since 2001 and believes in sharing with the community. His new passion for CRM is Creatio, formerly bpm'online. He loves C#, Javascript, web development, open source, and Linux. He also loves his hobby as an amateur filmmaker.

4 Comments

  1. Hi Ryan, thanks for this guide, it’s worth noting that with the somewhat recent ability to configure applications to open lookup modal windows from “dropdown” fields in Freedom UI, that the request for this is different (and actually uses _both_ the one stated in this guide when using that field as a combo box where you type data in and autocomplete, and a different request for when opening the modal window, so code duplication may be required to filter both!) The one to look for is crt.OpenLookupPageRequest, and the filtering would have to be different – either on request.itemAttributeName = or request.itemsAttributeName = _List.

    I would also be interested to know if you’ve found any workarounds for the refreshing of data – I wondered if adding a custom handler for crt.HandleViewModelAttributeChangeRequest to check if fields that impact the lookup filter have been changed and to executeRequest to reload the data source or reapply the filter in some way?

    Thanks for your articles!

    Reply
    • Thanks for that Harvey. I did know that the newer lookup dialogs did also request the datasource in the same way, but wasn’t aware yet that they data source requests were separate.
      I have not yet found a way to force the lookup to reload the data source yet. At least in current versions, once a lookup requests its data source, it does not trigger that again when re-opened and dependent values have changed. I’ve not found a way to force it to request that again once values change. It is a very big annoyance and hopefully will be changing in upcoming version soon. I do expect this will change, but I can’t find any way around it for now.
      Ryan

  2. Hi Ryan,
    thank you for your guides, they are always clear and informative!

    Do you have any news about the fact that lookups only request their data once?
    I think it is a serious limitation and I was wondering if in the meantime you had found another way to update the lookup or to filter it dynamically.

    Please let me know.
    Thanks!

    Luca

    Reply
    • I’ve not come up with anything yet, although I know that will be changing – it could be that it has already, I’ve not re-tested in 8.1.0, 8.1.1, or 8.1.2 to see if it’s still an issue.

Submit a Comment

Your email address will not be published. Required fields are marked *