Improving perfomance in api.productToRelatedObjectsFilter()/api.customerToRelatedObjectsFilter usage

During development we where using “ToRelatedObjectsFilter” functions a lot to find connected contract line items.

 def filters = [
            api.customerToRelatedObjectsFilter("CTLI", customerGroup),
    ]

    if (productGroup) {
        filters << Filter.or(
                api.productToRelatedObjectsFilter("CTLI", productGroup),
                Filter.isNull("productGroup")
        )
    }
    List<Object> contractLineItems = streamUtils.stream('CTLI', null, ["outputsJson", "inputsJson", "lineId", "id", "clicId", "label"], filters)


Above code was working for pretty slow (120-180s) when productGroup filter included not only sku but some other fields as well:

image-20250715-094246.png

We found workaround for this issue by converting any filter into sku only filter:

ProductGroup buildNewProductGroup(ProductGroup productGroup) {
        List skus = libs.SharedLib.StreamUtils.stream('P', "sku", ["sku"], [productGroup.asFilter()]).collect { it.sku }
        ProductGroup productGroupNew = new ProductGroup()
        Map productGroupFilter = [
                _constructor: "AdvancedCriteria",
                operator    : "and",
                criteria    : [
                        [
                                fieldName   : "sku",
                                operator    : "inSet",
                                value       : skus,
                                _constructor: "AdvancedCriteria"
                        ]
                ]
        ]
        productGroupNew.setProductFilterCriteria(productGroupFilter)
        return productGroupNew
    }

and then using new filter productGroupNew in api.productToRelatedObjectsFilter("CTLI", productGroupNew).

As a result time of query was reduced into 1 second.

We believe this will happen as well for customerGroup hence we suggest to try this approach when you are facing performance issue when using “toRelatedObjectsFilter” functionality..