Question

Filtering lookups with esq

Hi,

I'm trying to set filters on lookups from an ESQ.

When spying on filterGroup in attribute , it's undefined.

		attributes: {
			....
			"CurrentModifiedLookupName": {
				dataValueType: this.Terrasoft.DataValueType.TEXT
			},
			"UsrStainlessSteelConnector": {
				dataValueType: this.Terrasoft.DataValueType.LOOKUP,
				lookupListConfig: {
					filter: function() {
						var filterGroup = this.getYesNoFilters("UsrStainlessSteelConnector");
						return filterGroup;
					}
				}
			}
		},
			getEsqMachineParam: function(callback) {
				var familyName = null;
				var subFamilyName = null;
				var subFamilyId = null;
 
				if(this.get("UsrMachineFamily")){
					familyName = this.get("UsrMachineFamily").displayValue;
				}
 
				if(this.get("UsrMachineSubFamily")){
					subFamilyName = this.get("UsrMachineSubFamily").displayValue;
					subFamilyId = this.get("UsrMachineSubFamily").value;
				}
 
				var fieldName = this.get("CurrentModifiedLookupName");
 
				var esq = Ext.create("Terrasoft.EntitySchemaQuery", { rootSchemaName: "UsrMachineProjectParam" });
				esq.addColumn("UsrDefaultNotAvailable");
				esq.addColumn("UsrDefaultIsIncluded");
				esq.addColumn("UsrFilterYesNo");
				esq.addColumn("Name");
				esq.filters.logicalOperation = this.Terrasoft.LogicalOperatorType.AND;
				esq.filters.add("UsrMachineSubFamily", Terrasoft.createColumnFilterWithParameter(
					Terrasoft.ComparisonType.EQUAL, "UsrMachineSubFamily", subFamilyId));
				esq.filters.add("Name", Terrasoft.createColumnFilterWithParameter(
						Terrasoft.ComparisonType.EQUAL, "Name", fieldName
					)
				);
 
				this.log2Console("fieldName=" + fieldName);
 
				esq.getEntityCollection(function(response) {
					if(callback) {
						if (response && response.success) {
							this.log2Console("response.collection=" + response.collection.count);
						}
						callback.call(this, response);
					}
				}, this);
			},
			getYesNoFilters: function(fieldName) {
				this.set("CurrentModifiedLookupName", fieldName);
				var response = null;
				Terrasoft.chain(
					function(next) {
						this.getEsqMachineParam(function(esqResult) {
							response = esqResult;
							next();
						});
					},
					function() {
						return this.onGetEsqMachineParamCompleted(response,this);
					},
					this
				);
			},
			onGetEsqMachineParamCompleted: function(response, scope) {
				if (response && response.success) {
					var item = response.collection.getByIndex(0);
					if(item.get("UsrFilterYesNo")) {
						var filterGroup = scope.Terrasoft.createFilterGroup();
						filterGroup.logicalOperation = scope.Terrasoft.LogicalOperatorType.OR;
						filterGroup.add("YesFilter", scope.Terrasoft.createColumnFilterWithParameter(
							scope.Terrasoft.ComparisonType.EQUAL,
							"Name",
							"Oui")
						);
						filterGroup.add("NoFilter", scope.Terrasoft.createColumnFilterWithParameter(
							scope.Terrasoft.ComparisonType.EQUAL,
							"Name",
							"Non")
						);
						return filterGroup;
					}
				}
			},

When spying on filterGroup in onGetEsqMachineParamCompleted method, filter is correct.

Am I doing something wrong ?

Like 0

Like

4 comments

See the following article: https://academy.bpmonline.com/documents/technic-sdk/7-13/using-filtrati…

Looking at your code, the only thing that jumps out is that under "lookupListConfig" you have "filter" where it should be "filters" with it's value as an array of filter functions.

Ryan

No, the filter work  fine, I think the this.getYesNoFilters("UsrStainlessSteelConnector"); is not waiting for the callback or is not returning the filterGroup at the right time .... Not sure of how to implement it.

Ah ok. I see (I hadn't read down far enough in your code). That is a problem, since the esq is asynchronous. I'm not sure that your use of chain will help here since it doesn't chain the return of the function itself. 

Maybe you could add an attribute on the page and prefetch the needed machine param value in the onEntityInitialized? Then in the function to get the filters for the lookup, you'd just need to read the attribute. Not ideal, since you'd get the value every time, whether the lookup was used or not, but it would work this way.

Ryan

The lookupListConfig.filter object doesn't work with asynchronous requests. Please transform the filter to a linear structure. Please note that you can use joins in the filtration. 

https://academy.bpmonline.com/documents/technic-sdk/7-13/using-filtrati…

https://academy.bpmonline.com/documents/technic-sdk/7-13/building-paths…

Show all comments