How to repeat action(s) in your Zap for a variable number of values

How to repeat action(s) in your Zap for a variable number of values

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

39 replies

Userlevel 1
Badge

RisoSystems -  Thanks.  That seems like an excellent and workable solution. I can use the output record number from the trigger step to know what record I am on.

The problem is my input to the trigger step is a variable number of lines so I don’t have a number to compare it to. It would change every time the ZAP was run. The Only Continue IF trigger only allows an entered specific value in the if statement.

Userlevel 3
Badge +1

Chrisboat - I’m not sure I see that as a problem. When you perform the step that retrieves the variable number of items (presumably in an array), just add a variable to the output that reflects the size of the array:

var someArray = [‘John’, ‘Mary’, ‘Fred’, ‘pizza’];

var arraySize = someArray.length;

var arrayLimit = arraySize - 1;

output = [{someArray: someArray, arrayLimit: arrayLimit}];

I’m showing all my steps here, obviously this could be written more economically - but in the filter statement compare the arrayLimit variable to the index variable and you’re done.

Userlevel 1
Badge

I have been using your code very successfully but have found a problem. The input data is as shown below. The problem is that the last entry in Quantity and Flavour gets associated with the first none entry and not with the Product Foil as it should. Output is shown as well.

Your comments would be appreciated.

 

Userlevel 3
Badge +1

Chrisboat - 

Could you please give me a mockup of the desired output based on the data provided?

Is this what you’re expecting (less headers):
 

Need Update Quantity Flavour
none    
none    
Foil   vanilla
none 6  
Userlevel 1
Badge

Thank you very much for the quick reply.

What is happening is that if the value of the CSV line has nothing between the commas the code step takes the next value it can find.

So in this case the quantity and flavour were both moved to record 1 when they were actually associated with record 3. 

Sorry the 3rd image I provided above only made it more confusing.

What I need is:

 

 

Product Quantity Flavour
none    
none    
Foil 6 vanilla
none    

 

Thanks again

Chris

Userlevel 1
Badge

thought this may be even clearer of what I want.

logs

0

INFO [ { Product: '', recordNumber: 1, Quantity: '', Flavour: '' }, { Product: '', recordNumber: 2, Quantity: '', Flavour: '' }, { Product: 'Foil', recordNumber: 3, Quantity: '6', Flavour: 'Vanilla' }, { Product: '', recordNumber: 4, Quantity: '', Flavour: '' } ]

Userlevel 3
Badge +1

Chrisboat,

I think the problem is in your manipulation of the data in your code step. I suggest you copy your code to a safe place, and replace it with the code below.

This code creates an array of objects which should cause another fork. Each fork would receive one object with 3 properties, product, quantity, flavour. From these objects you should be able to produce the lines you want in your spreadsheet.

Obviously I can’t run this myself, but if you paste it in (with the substitutions at the top) it should work fine.

// these were for my syntax testing only. Use the inputData variables in your code:
// 
// Let Product = inputData.Product;
// Let Quantity = inputData.Quantity;
// Let Flavour = inputData.Flavour;
//
let Product = ['none', 'none', 'Foil', 'none'];
let Quantity = [null, null, 6, null];
let Flavour = [null, null, 'vanilla', null];
//
// You can see below where I'm going with this. It's not the only way to do it, but it should work.
//
var LineItem = {};
var LineOuts = [];

let LineCount = Product.length;

for (var i = 0; i < LineCount; i++){  
    LineItem.product = Product[i];
  LineItem.quantity = Quantity[i];
  LineItem.flavour = Flavour[i];
  LineOuts[i] = LineItem;
  LineItem = {};
    }

output = [{LineOuts : LineOuts}];

 

 

Userlevel 3
Badge +1

Chrisboat,

One other thing. If the subsequent step doesn’t handle the objects well, then JSON.stringify the object before placing it in the array:

for (var i = 0; i < LineCount; i++){  
    LineItem.product = Product[i];
  LineItem.quantity = Quantity[i];
  LineItem.flavour = Flavour[i];
  LineOuts[i] = JSON.stringify(LineItem);
  LineItem = {};
    }

The variable would have to be parsed before use in the spreadsheet:

let LineItemString = inputData.LineItem;
let LineItem = JSON.parse(LineItemString);

 

Userlevel 1
Badge

HI,

This message is for Tim. I think there is confusion that my most recent post was dealing with a fork within a fork. Sorry RicoSystems for causing the confusion. I need to get this issued with Tim’s fork code corrected first then I will get back to fork within a fork.

 

Tim’s fork code issue.

I have been using your code very successfully but have found a problem. The input data is as shown below. The problem is that the first non blank entry in Quantity and Flavour gets associated with the first record Product ‘none’ entry and not with the Product ‘Foil’ in the third record as it should. Output is shown as well.

What is happening is that if the value of the CSV line has nothing between the commas the code step takes the next value it can find.

So in this case the quantity and flavour were both moved to record 1 when they were actually associated with record 3. 

What I need is:

 

 

Product Quantity Flavour
none    
none    
Foil 6 vanilla
none  

 

 

what the output should look like:

logs

0

INFO [ { Product: '', recordNumber: 1, Quantity: '', Flavour: '' }, { Product: '', recordNumber: 2, Quantity: '', Flavour: '' }, { Product: 'Foil', recordNumber: 3, Quantity: '6', Flavour: 'Vanilla' }, { Product: '', recordNumber: 4, Quantity: '', Flavour: '' } ]

Input data, output data of what happens now:

Userlevel 3
Badge +1

Chrisboat,

Actually the same code approach I posted is applicable to both situations. But I’ll bow out until/unless I see my name at the top of a post.

 

Userlevel 1
Badge

RisoSystems,

I must apologize. My last post may not have been said correctly.

When I realized the recent responses to this current issue were from you (thought it was Tim I should have looked) and then saw that you were talking about fork within a fork I just assumed that your reply was about my earlier topic about double forks. I am very appreciative of the help you provided. Most of this javascript code is over my head.

So please don’t think I don’t want the help. I just thought I was confusing everyone and did not want to burden you. It seemed to me that my current problem with no values between the commas was with Tim’s code so that is why I specifically asked him. If you want to help I will truly appreciate it. 

This is what my input data actually looks like. I run this code step then I run yours.

When I tried your code with my data this is what I got. 

Thank you

Chris

Userlevel 1
Badge

RicoSystems,

What I got   (the difference in they input data was that only the Quantity has commas without data in between.0

 

Userlevel 3
Badge +1

Chris,

One of the little quirks of Zapier and object and arrays is that in some cases you lose the ability to parse things correctly if they are not communicated as JSON strings. Nulls and string values with internal commas can be particularly difficult.

To that end, I think you’ll have better luck if you use the code I sent you most recently, applying the JSON.stringify fix to create an array of JSON strings. That will still trigger the fork, but each fork will get a single JSON string which can be parsed into the line item object you need. If you go back up to your original inputs - the three or four arrays that you concatenate, then you’ll be on track. If that fork is driven by an array of JSON strings, then the subsequent fork structure can operate the same way, with a filter step to separate the first fork from the subsequent fork(s).

Bear in mind the overall limitations that Tim mentioned.

If you’d find it helpful to continue this either via email or chat, just Google me - I’m easy to find.

Joey (aka RisoSystems)

Userlevel 3
Badge +1

Chris,

PS: Remember that a string is treated like an array - you have to parse the thing back to an array of objects before an index will work the way you need.

ie:

If StringValue = ‘ “none”, “something”, “something else”, “something, or maybe nothing” ‘ then StringValue[14] == “h”

If StringArrayValue = [“none”, “something”, “something else”, “something, or maybe nothing”] then StringArrayValue[2] == “something else”