Best answer

Reversing line item order using Javascript

  • 7 March 2024
  • 7 replies
  • 49 views

Userlevel 1

Hello!

When using Zapier tables, and the Zap function "Find Records - Output as Line Items", I am noticing that Zapier takes the records from the table in the reverse order, meaning that Line Item #1 is the last record in the selection (furthest down the table). This is a problem for me, as I want it to select line items in the opposite order, because those line items are being used in a looping zap that needs to run going down the table, not up it.

I asked the AI assistant, and they gave me the following Javascript code for Code by Zapier (where lineItems is the output of the Find Records as Line Items step).

 

const lineItems = inputData.lineItems;

const reversedLineItems = lineItems.reverse();

output = {reversedLineItems: reversedLineItems};

 

When I run this code, however, I get an error: Failed to run your JavaScript code TypeError: lineItems.reverse is not a function

The Zapier AI troubleshooting bot says that the error is happening because it’s not recognizing the output from the previous step (which is a comma delimited list of values) as an array. I’m not quite sure what to do to fix this, however. 

Help! 

Thank you so much in advance.

Rick 

icon

Best answer by Troy Tessalone 8 March 2024, 17:40

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.

7 replies

Userlevel 7
Badge +14

Hi @WellnessRoad 

Help articles to reference for using Code: https://zapier.com/apps/code/help

 

  • You can also define data fields to be provided to the code as strings with the Input Data fields.
    Provide a key and a value for each field.

 

 

Userlevel 1

Thanks Troy… 

So, I tried converting the line items output to a string (i.e. a comma delimited list of items), and now the reverse function “runs”, but doesn’t actually reverse the items, because it just sees one value, not a list of values. 

How can I get the function to recognize the separate values in comma delimited list, so that it can reverse them?

Sorry I’m unfamiliar with Javascript, as they Zapier AI bot is the one who suggested this solution to me. 

Thanks in advance,

Rick 

 

Userlevel 1

P.S. the help article you posted isn’t applicable in this case, as I’m try to reverse the order of the line items - not based on the values themselves (like in your example, A, B, C, etc), but based on the order that Zapier is turning them into line items. 

The one way I could see the solution in your article working, however, is if the code can sort the items in ascending date/time order (rather than alphabetical). Is this possible?

Userlevel 7
Badge +14

@WellnessRoad

For us to have full context, we will need to see screenshots with how the steps are outlined and configured along with examples of the data being input into the Zap Code step.

Userlevel 1

Troy, thanks so much for your willingness to take a look at this issue!! 

Here are screenshots that show the overview of zap steps, and the configuration of each step. 

The table itself is too large to take a screenshot of, but I am only taking two values from the table - the recordID, and a numeric field “calculated pay”. The data in the table is listed in order of date/time (there is a date/time column). 

In summary, I have a Zapier table of all the sessions held by a given consultant in a week. The consultant gets a different pay rate for each sessions that occurred after the 20th session. Thus, the Zap pulls the sessions as line items, and then feeds them into a looping zap that updates the pay rate for each session after the 20th one.

The issue is that during the “Find records and output as line items” step, Zapier records the line items in reverse order (the last session first). Whereas, I need the line items in the chronological order.

If you need any other data to make sense of this, please let me know. 

Thank you

Userlevel 7
Badge +14

@WellnessRoad 

You probably do not need the Formatter steps.

 

 

From the related guide make sure to split the “lineItems” with the function: .split(“,”)

https://community.zapier.com/featured%2Darticles%2D65/how%2Dto%2Dsort%2Dan%2Darray%2Dof%2Dline%2Ditems%2Dusing%2Dcode%2Dby%2Dzapier%2D13152

 

 

Userlevel 1

Thank you Troy!! yes that worked.