cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

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

patan
New Contributor III

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.

1 ACCEPTED SOLUTION

del
Contributor III

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

View solution in original post

2 REPLIES 2

del
Contributor III

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

patan
New Contributor III

@del thank you for the solution.