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

Is there a way to insert a row number while mapping an object array?

wcl3y2
New Contributor III

I am creating an integration that will take JSON from our CRM and create invoices in our ERP. One of the required fields is line number for each of the invoice lines. I am trying to generate this in the mapper snap, and I cannot figure out a way. I am really hoping that I donโ€™t have to resort to using a script.

1 ACCEPTED SOLUTION

tstack
Former Employee

I think you can use the .map() method on Array objects to do this since the callback will be passed the current index into the array. The following expression does this:

$lines.map((elem, index) => { lineNumber: index + 1, ...elem })

The .map() method will iterate over each element in the $lines array and execute the given callback function (the part that starts with (elem, index) =>. The callback function creates a new object with the new lineNumber property and then uses the spread operator (...elem) to add in the properties from the original array element.

Hereโ€™s a pipeline that demonstrates this:

Community7085_2020_03_16.slp (3.6 KB)

View solution in original post

6 REPLIES 6

Ajay_Chawda
Contributor

Hi,
you can use snap.in.totalCount function to get row number.
AC new pipeline 0_2020_03_15.slp (4.5 KB)

wcl3y2
New Contributor III

Hello,

I have tried this, but it doesnโ€™t work. I have a mapper where the mapping root is a nested object array. When I use the property that you talk about, it increments for each invoice, not each invoice line within an invoice.

For instance, I have a JSON document with 3 invoices with 5 lines each. I have a separate mapper snap for the line item mapping. When I use the snap.in.totalCount property in the line item mapper, it assigns 1 to each line on the first document, 2 to each line on the second document, and 3 to each line on the third document. I want it to assign line numbers 1, 2, 3, 4, and 5 within each invoice.

Hi,
can you share input and expected output that your are looking.

Regards,
Ajay Chawda

wcl3y2
New Contributor III

Sure, here is a simplified version:

Input:

[{
โ€œinvoiceNumโ€: โ€œ12345โ€,
โ€œtotalโ€: 100,
โ€œlinesโ€: [{
โ€œqtyโ€: 1,
โ€œunitโ€: 50,
โ€œextendedโ€: 50
},
{
โ€œqtyโ€: 2,
โ€œunitโ€: 25,
โ€œextendedโ€: 50
}],

},
{
โ€œinvoiceNumโ€: โ€œ12346โ€,
โ€œtotalโ€: 150.56,
โ€œlinesโ€: [{
โ€œqtyโ€: 1,
โ€œunitโ€: 50.56,
โ€œextendedโ€: 50.56
},
{
โ€œqtyโ€: 1,
โ€œunitโ€: 50,
โ€œextendedโ€: 50
},
{
โ€œqtyโ€: 2,
โ€œunitโ€: 25,
โ€œextendedโ€: 50
}],
}]

Output:

[{
โ€œinvoiceNumโ€: โ€œ12345โ€,
โ€œtotalโ€: 100,
โ€œlinesโ€: [{
โ€œlineNumberโ€: 1,
โ€œqtyโ€: 1,
โ€œunitโ€: 50,
โ€œextendedโ€: 50
},
{
โ€œlineNumberโ€: 2,
โ€œqtyโ€: 2,
โ€œunitโ€: 25,
โ€œextendedโ€: 50
}],

},
{
โ€œinvoiceNumโ€: โ€œ12346โ€,
โ€œtotalโ€: 150.56,
โ€œlinesโ€: [{
โ€œlineNumberโ€: 1,
โ€œqtyโ€: 1,
โ€œunitโ€: 50.56,
โ€œextendedโ€: 50.56
},
{
โ€œlineNumberโ€: 2,
โ€œqtyโ€: 1,
โ€œunitโ€: 50,
โ€œextendedโ€: 50
},
{
โ€œlineNumberโ€: 3,
โ€œqtyโ€: 2,
โ€œunitโ€: 25,
โ€œextendedโ€: 50
}],
}]