Skip to main content

Hello,

 

We have a use case were we have an order which contains order lines, and each of those order lines may or may not have 1 or more “applied discounts” in a nested array. For example, here is a snippet from the output order:

products:
1:
id: 79
code: a
base_total: $10
applied_discounts:
1:
amount: $1
2:
amount: $2

2:
id: 80
code: b
base_total: $5

applied_discounts:
1:
amount: $5

3:
id: 80
code: c
base_total: $10

applied_discounts:

When we use the loop from lines functionality, it would show the above input value as:

p“1”,”2”],“5”],null

However, when we actually loop though the lines, it treats it like “1,2,5,”. As you can imagine, this creates problems, because we only have 3 products, and each discount can easily become misaligned with it’s correct pairing.

 

Ultimately, we want to add values together for each product to get a total discount amount, then pass those values to line items, which seems to be achievable with some JavaScript. However, when we use JavaScript, the input value seems to not preserve the nested arrays, ie, no matter what we do, even though the applied discounts look like this when being selected for formatting using JavaScript:

The formatter seems to always see the input data like this:

How do we parse out each array for each product? Are we missing something? Is there a simpler solution?

 

Please help, thank you!

Hi @zap-ss-ad 

You’ll likely have to use a custom Code step to handle the nested data: https://zapier.com/apps/code/help


Hi @Troy Tessalone,

 

We are aware of this, however as mentioned, the input data for the line items does not seem to preserve the formatting.

 

Zapier shows the Product Applied Discounts Amount value will look like <"538.5000","1256.5000"], 0"295.0000"], null, n"119.7000","279.3000"] which would be great since it retains the individual product discounts. However, what it actually does is provide a string like 538.5000,1256.5000,295.0000,119.7000,279.3000. Which loses all context of which discounts are for which product.

How do we rectify this?


@zap-ss-ad 

Are you trying to use a Code step?


@Troy Tessalone yes, using javascript.


@zap-ss-ad 

You’ll have to try an advanced setup that involves chaining 2 Zaps together before you can use the JavaScript Code step.

Check this related topic:

 


FYI this thread did eventually get us to a solution, however, THERE SEEMS TO BE A MAJOR BUG in which the Raw “JSON” that is produced is malformed (there are single quotes where there should be double quotes). We were able to create a dirty workaround for this by using a replace method on the raw data string.

Hopefully Zapier addresses this, and/or simply creates an better solution to accessing nested arrays. It’s pretty frustrating having us use multiple zaps with multiple extra steps (and thus a higher cost) to achieve basic functionality.


@zap-ss-ad 

Best to submit feedback about issues/errors/bugs to Zapier Support: https://zapier.com/app/get-help


@zap-ss-ad 

Since others may run into this. There is a significantly easier way to reproduce raw json rather than sending to another zap with Webhooks and then fixing the malformed json string. 

See post here: