How to get value based on a incoming document

Hi, I have the below input data


In the output I want the Parent account name based on the input so the output will be like

AccountName,AccountID,PAccountID, PAccountName

Please help me how can I achieve that?

In the above json content, child appears to be before parent. So we can use Script snap to solve this. We have to cache all the documents. Once all the input docs are received then we can write to the output view as desired.

Hey @the_pan_zone,

Here’s a pipeline that does exactly what you need:

Inherit Parent AccountName_2021_12_01.slp (5.7 KB)

If by any means, there is a mistake in your CSV sample, because as @smudassir said, the child appears before the parent, and you need it other way around, than you need to change the Mapper expression to :

$,index)=> index == 0 ? x.extend({"PAccountName":$input0[$input0.length -1].AccountName}) : x.extend({"PAccountName":$input0[index-1].AccountName}))


Thanks @bojanvelevski
it is working…Do you think it will work for .1 million records?

Hi @bojanvelevski ,

If I add one more row and change the source data

This time the code is not working

It’s hard to tell, theoretically it will work, but you’re sending 1 million records in one array, so I would say it depends on the records?

The solution is not hardcoded, it’s designed to dynamically add the new field:


Re-check the setup of the pipeline.

Hi @bojanvelevski ,

Please check the 3rd row in your result set.
Here PAccountName should be ABC instead of “new”. PAccountName should not be the next one it should depend on PAccountID. As PAccountId is same for both 3rd and 4th row so PAccountName should be also same i.e. Abc for both 3rd and 4th Row.

Ah, this clarifies things. It was a misunderstanding, the expression you need to use is the following:

$,index)=> x.extend({"PAccountName":$input0.filter(y=>y.AccountID == x.PAccountID)[0].AccountName}))

1 Like

Hi @bojanvelevski ,
its working fine now.
Thank you

Hi @bojanvelevski ,

if I change the source and put a null in one row

I am getting the following error in the expression
Failure: Cannot access element 0 of ‘$input0.filter(y=>y.AccountID == x.PAccountID)’, which is empty, Reason: Index was out-of-bounds, Resolution: Please check expression syntax and data types.

Expected output should be if there is not parent ID then there will be no parent name corresponding that row.
Can you please let me know if we can use any null handling here

Update the expression:

$,index)=> x.PAccountID == null || x.PAccountID == "" || x.PAccountID == "null" ? x : x.extend({"PAccountName":$input0.filter(y=>y.AccountID == x.PAccountID)[0].AccountName}))