Forum Discussion

patan's avatar
patan
New Contributor III
3 years ago

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)

5 Replies

  • viktor_n's avatar
    viktor_n
    Contributor II

    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

    • patan's avatar
      patan
      New Contributor III

      @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

  • alchemiz's avatar
    alchemiz
    Contributor III

    Hi @patan,

    Good day, instead of using "this" use "root" 😄

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

    Thanks,
    EmEm

    • patan's avatar
      patan
      New Contributor III

      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})