Best answer

Line items as array in code step?

  • 13 August 2020
  • 3 replies
  • 3531 views

Userlevel 1

Hey there,

I’ve got a Zap that takes Shopify order data and routes that to an API, using a code step to properly format the data.

I need to be able to grab the line item fields and use them as arrays in my code step. Currently, I’m using the Input Data fields, and separating the line items by comma in JS, but I’d like to know if there is a way to get arrays of the line items as a direct input.

Separating by comma becomes problematic as soon as there are other fields like SKU or Variant, which might not have a value assigned, so Zapier removes the comma, and I’ll have an array of say, 5 products titles, and a SKU array that only has 4 items, because one of the products didn’t have one assigned in Shopify.

icon

Best answer by TimS 1 September 2020, 08:57

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.

3 replies

Userlevel 7
Badge +8

Hi there @dannymo - Thanks for posting this up! I see you had a conversation with Christian and Leo from Zapier Support. You're definitely asking great questions, but I'm afraid we have limited knowledge on this specifically. As this is an advanced feature, but your question is definitely valid, can you post this question on StackOverflow and tag "Zapier" to it?

We have a couple of developers there that should be able to help you out more:

https://stackoverflow.com/questions/tagged/zapier

And you had originally posted this topic in “Ask the Community” but I’ve moved it to Developer Discussion as mentioned by Leo. Hope this bump to the topic gets attention from our Experts!

Userlevel 7
Badge +3

Hi @dannymo ! Tim here from the Zapier Support team!

Just in case you haven’t found a good answer on Stack Overflow yet, I saw this and thought I could chime in.

There isn’t currently a way to input line items / arrays directly into a Code Step - as you’ve noticed we coerce all inputs to Strings. We do have a feature request we’re tracking to add support for sending Arrays in as they can be the most problematic for the reasons you mentioned.

In the meantime, here’s the approach I use to get my arrays into Code Steps without worrying about commas and null values being dropped:

Use the Formatter Utility Action Line Items to Text Action to convert the Line Items to a block of text with our own custom delimiters so we can more easily convert that back into the array in the Code Step.

Here’s how I set up my Line Items - I put the exact same delimiter inbetween each Line Item value like name, qty, etc. In this case ‘-----’. I also set the separator for each iteration of the arrays to ‘|||’

 

That gives us some values, and the one we’re interested in is the delimited full text value:

We input this single value into the Code Step and split it twice - first on the iteration delimiter and then on the value name delimiter.

If we log that to the console, we can see that we now have an array of arrays that we can work with.

Due to the nature of the Formatter Step processing the arrays properly, we won’t get any misaligned data if there were missing data points, and we can customize the delimiters to make sure they work with the values we’re getting. One last thing to note is that the individual elements are still all converted to strings, so to work with numbers, you’ll need to convert those back in your code.

Does that get you pointed in the right direction?

Userlevel 7
Badge +11

Thanks for offering that super thorough solution @TimS — much appreciated :)