Cascading Lookups | Select Item if there's only one

Hello,

I have implemented Cascading Lookups and it is working well.

In some instances, there is only one result when the dependent Lookup is filtered and the customer would like it if that single item was automatically selected for the user.

I've tried both of the following methods (commented out currently) and neither of them work:

function filterChildren(parent) {
var parentId = parent && parent.LookupId || parent || null;
fd.field('childLookup').filter = 'ParentLookup/Id eq ' + parentId;
fd.field('childLookup').widget.dataSource.read();
if(fd.field('childLookup').widget.dataSource.data().length == 1){
// fd.field('childLookup').value = fd.field('childLookup').widget.dataSource.data()[0];
// fd.field('childLookup').widget.selectedIndex = 1;
}
}

Is there a way make this work?

Hello @ShareSquared,

You can set the value of the lookup field only by its ID.

You can use PnPjs, the basic get() action, to check if there is only one dependent value and return its ID, which you can use to set the field value.

Does that mean that this should work?

fd.field('childLookup').value = fd.field('childLookup').widget.dataSource.data()[0].Id;

Hello @ShareSquared,

Yes, that also should work like this:

    fd.field('childLookup').widget.dataSource.read().then(function() {
    fd.field('childLookup').value = fd.field('childLookup').widget.dataSource.data()[0].Id;
    });