โ03-06-2023 04:11 PM
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)
โ03-08-2023 02:35 AM
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.
Regards,
Viktor
โ03-08-2023 04:43 AM
@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})
Kinds&Regards
Patan
โ03-08-2023 07:45 AM
@patan this โKSCHLNโ needs to be a dynamic value?
Regards,
Viktor
โ03-08-2023 09:00 PM
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})