I am trying to perform a lookup and extend the object using the expression library

I wanted to perform a extend by performing a lookup using the expression library and it is giving me “this.comparator is undefined” error.

Below is my expression Library Content:

	embedbusinessUnit: x => x.extend({"Demo":this.comparator[0]}),
	embedbusinessUnit1: x => x.merge(this.comparator[0]),	
	"comparator": [
			"KSCHL": "EPE",
			"VKORG": "1000",
			"BusinessType": "E",
			"KSCHLN": "LPE",
			"VKORGN": "1000"
			"KSCHL": "EP0",
			"VKORG": "RG",
			"BusinessType": "N",
			"KSCHLN": "LP0",
			"VKORGN": "1000"

When I access the lib.businessUnitExpr.embedbusinessUnit($) gives me a comparator undefined error.
But when I access lib.businessUnitExpr.embedbusinessUnit1($) then it is able to identify the comparator

Any help would be appreciated.

BusinessUnitComparator.expr (382 Bytes)

Hi @patan,

The problem is occurring because you use brackets inside the function and try to access “comparator” with this keyword which not anymore refers to the root elements, but one scope above, which in this sample is the function scope.

Below is an image of the scopes in your config:

When you tried to access ‘comparator’ from the inside extend function, what happened in the background is that the compiler attempted to search for ‘comparator’ on the 2nd scope, In which didn’t found the element.

If you want to extend the element with a different name from the specified you can use sl.zipObject function. Use this one: x.extend(sl.zipObject(["Demo"],[this.comparator[0]]))
This will work because it is not created a new score.



@viktor_n Thank you for your response with a detailed explanation.

I wanted to extend only one field/property from the comparator array element based on the matching criteria as shown below:

embedbusinessUnit: x => x.extend({"Demo":this.comparator[0].KSCHLN})


@patan this ‘KSCHLN’ needs to be a dynamic value?


Yes, based on comparing with one of the properties in object ‘x’ and then I wanted to assign the property.

For example:

embedbusinessUnit: x => x.extend({"CompVKORG":this.comparator.filter(y=>(y.VKORG==x.VKORG))[0].KSCHLN})

Hi @patan,

Good day, instead of using "this" use "root" :smile:

	embedbusinessUnit: x => x.extend({"Demo":__root__.comparator[0]}),
	embedbusinessUnit1: x => x.merge(this.comparator[0]),	


1 Like