โ03-23-2022 08:00 AM
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.
Solved! Go to Solution.
โ03-23-2022 09:55 AM
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
โ03-23-2022 09:55 AM
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
โ03-24-2022 01:12 AM
@del thank you for the solution.