Best answer

Flattening Line Items Using a JS Code Step

  • 26 August 2019
  • 1 reply
  • 4331 views

Userlevel 4
Badge +5

Is it possible to flatten line items within a Zap?


icon

Best answer by jesse 27 August 2019, 20:37

View original

This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

1 reply

Userlevel 7
Badge +9

Ultimately there isn't a great way to deal with this, but if you're up for it, this can be achieved in 1 step using a few lines of code.

Let's say I have this set of Line Items here: 

line items for Sam _ Zapier Google Chrome, Today at 7.27.12 pm.pngTo convert those into single usable fields, you'll first want to set up your Input Data. On the left you'll want to represent what each group of line items fields are: 

line items for Sam _ Zapier Google Chrome, Today at 7.36.01 pm.pngNext, using some code we can create title for the new fields, generating a new array: 

SnappyApp SnappyApp, Today at 7.29.00 pm.pngThe text version of that code is:

output = {}

const names = inputData.name.split(',') // giving a title to the new fields

names.forEach(name => output[name] = {})

// each line here iterates over each field, splitting it by the comma and adding it to the output array.

inputData.name.split(',').forEach((name, i) => {output[names[i]].name = name})

inputData.price.split(',').forEach((price, i) => {output[names[i]].price = price})

inputData.qty.split(',').forEach((qty, i ) => {output[names[i]].qty = qty})

output = [output];

You can experiment with this to get slightly different outcomes, or you can literally copy and paste that code into any code step and it'll iterate over those arrays. Using the above example, when the code is tested the output is: 

line items for Sam _ Zapier Google Chrome, Today at 7.31.37 pm.pngWhich appears in the following steps like so: 

line items for Sam _ Zapier Google Chrome, Today at 7.31.58 pm.pngHope that helps!