Tips about Run Javascript in Code by Zapier

  • 25 February 2023
  • 0 replies
  • 122 views

I wanted to share this code tidbit because it took me hours to work out what was happening. It has to do with  where the zapier Javascript is expecting semi-colons, especially in looping.

My code takes comma-delimited input variables, splits them into arrays, and then puts the elements of the array back together to return JSON. Notice especially where the semi-colons are, since either leaving them out or putting them in the wrong place will cause the Zapier action to fail. Code:

var descrline = inputData.descrline.split(',')
var qtyline = inputData.qtyline.split(',')
var refline = inputData.partrefline.split(',')
let gChildren = []
var i = 0 ;
for(i; i < descrline.length; i++){
gChildren.push({description: descrline[i], quantity : qtyline[i], partReference: refline[i]})
}
return {gChildren}

referring to these 2 lines:

var i = 0 ;
for(i; i < descrline.length; i++)

Normally, in JavaScript, I would put them in one line like this:
for(var i = 0 ; i < descrline.length; i++;)

In Zapier, you can’t. The declaration of the variable can’t happen in the for clause, it has to be separate.

One other change: no “;” after the  “++”

I hope this helps others figure out the syntax of the JavaScript!


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