Help ordering filtered lookup

Good Afternoon,

I am looking for a little help with additional requirements on a filtered lookup, my requirements are: -

  1. I need to order the Lookup to show items by Id with the latest at the top but the default orderBy functionality seems to always order ascending and I need it to order descending.

  2. I would like to catch if the filter lookup returns no results i.e. results = true or results = false.

My current code is: -

	// function to filter Maintenance Contracts to only show contracts for selected addresses
function filterContract () {
	var addressName = fd.field('Address_x0020_Name').value;
	if (addressName == '' || addressName == null) {
		fd.field('Maintenance_x0020_Contract').value = null;
		fd.field('Maintenance_x0020_Contract').disabled = true;
	} else {
		var addressNameId = fd.field('Address_x0020_Name').value.LookupId;
		fd.field('Maintenance_x0020_Contract').value = null;
		fd.field('Maintenance_x0020_Contract').filter = 'Address_x0020_NameId eq ' + addressNameId;
		fd.field('Maintenance_x0020_Contract').orderBy = "Id";
		fd.field('Maintenance_x0020_Contract').widget.dataSource.read();
		fd.field('Maintenance_x0020_Contract').disabled = false;
	}
}

Hopefully you can assist with the above.

Thanks

Dear @Tony_Duke,
For the lookup to use descending order, please, try:
fd.field('Lookup').orderBy = { field: 'Id', desc: true };

As for checking the number of values - that's a bit tricky... The operation to retrieve values takes time, you might be able to check it with slight delay:

fd.field('Maintenance_x0020_Contract').filter = 'Address_x0020_NameId eq ' + addressNameId;
fd.field('Maintenance_x0020_Contract').orderBy = "Id";
fd.field('Maintenance_x0020_Contract').widget.dataSource.read();
setTimeout(function(){ alert(fd.field('Maintenance_x0020_Contract').widget.dataSource.data().length) }, 1000);

Or use pnp-js and send requests to the source list for more reliable results.

Thanks Nikita,

Works great and the timeout is ideal for what I want it really is just an alert to notify users when there are no results.

Thanks again.

1 Like