I have a use case where I have to invoke the one library function with another library function with in same expression library

I have created an expression library as below:

{
‘DEPT’:
{
“EG” : “Engineering”,
“HR” : “Human Resource”,
“FIN” : “FInance”
},
AddDescription : element => (element.filter(x=>x.sector==‘USA’).map(x=> x.extend({‘Description’:this.DEPT.get(x.deptId)})).filter(x=>x.get(‘Description’)!=null))
}

Below is the expression I am using in mapper:

lib.mapping_new.AddDescription($dept)

The exception I am getting:
Failure: this.DEPT is undefined., Reason: ‘DEPT’ was not found while evaluating the sub-expression ‘this.DEPT’, Resolution: Check the spelling of the property or, if the property is optional, use the get() method (e.g. this.get(‘DEPT’))

Check Expression_2022_03_23.slp (3.8 KB)

For Ex: for the below input Array:
{
“dept”: [ {“deptId”: “HR”,“sector”: “USA”},
{“deptId”: “FIN”,“sector”: “USA”},
{“deptId”: “DE”,“sector”: “EU”},
{“deptId”: “EG”,“sector”: “USA”},
{“deptId”: “HR”,“sector”: “EU”}]
}

I want the below output:

{ “dept”: [{“deptId”: “HR”,“sector”: “USA”,“Description”: “Human Resource”},
{“deptId”: “FIN”,“sector”: “USA”,“Description”:“FInance”},
{“deptId”: “EG”,“sector”: “USA”,“Description”:“Engineering”}]
}

I know if we have two different libraries we can invoke one function from another, but I wanted to achieve this with a single expression library.

In your code, try changing “this.DEPT” to “__parent__.DEPT”. I think “this” is scoped to the new object you’re creating, so you need to reference the object parent.

{
    "DEPT": {
        "EG": "Engineering",
        "HR": "Human Resource",
        "FIN": "FInance"
    },
    "AddDescription": element => (element.filter(x=>x.sector=='USA').map(x=> x.extend({"Description":__parent__.DEPT.get(x.deptId)})).filter(x=>x.get('Description')!=null))
}

reference: https://docs-snaplogic.atlassian.net/wiki/spaces/SD/pages/1439367/Object+Functions+and+Properties

2 Likes

@del thank you for the solution.