Different Ways to Create a Unique Random String within your Zap

  • 26 August 2019
  • 5 replies
  • 20567 views

Userlevel 7
Badge +9

Creating a random string to be used as an order/invoice ID (or anything else for that matter) is something that can be done pretty easily. Here's @nicksimard's approach:

Pick a Formatter step and choose Numbers -- Spreadsheet-Style Formula. Then use RANDBETWEEN:

The odds of ever having a duplicate are extremely low. If you wanted to, you could create two of these steps then combine the mapped fields, to make it even more unique, but there's probably no need.

You could also create a Pick From List, choose random and have some preset options to choose from that you can add to the randomly generated number.

Like this:

OR, yet another way is to either use a field from the Trigger (or prior step) on its own, or combined with the randomly generated string:

 

Hope this gives you some inspiration for how to randomly generate a string! Are any of y'all generating or using random strings within your Zap and if so, how are you using them? We'd love to hear about your use cases!

 


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

5 replies

Userlevel 3

If "unique" is more important than "actually random", I've used the datetime of the zap firing as a reference. If you write {{zap_meta_timestamp}} into the field it will include the time at which the zap fired in unix time, which is a number like this: 1567154862 (the time I'm writing this). Unless your zap was to fire twice in the same second, this will be unique-to-you.


Zapier team, you could add "generate a UUID" to the Formatter's skills, perhaps?


Userlevel 7
Badge +9

@SimonJP wow, that's a great and super simple approach - thanks for sharing! We do have an open feature request for this already (in the form of a random number generator) so I can add you to that and update this thread should it get implemented. Not sure how well an exact UUID would work if the Zap gets turned off and loses and UUIDs it's already given/stored, so perhaps the random number generator would suffice. Thanks again for offering up a new workaround to this!


Userlevel 4
Badge +1

@SimonJP @jesse if you need a UUID, you can copy and paste this into Code by Zapier - Run JavaScript:

//from https://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript

function uuidv4() {

 return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {

  var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);

  return v.toString(16);

 });

}

return [{uuid: uuidv4()}];

@jesse yes, the random number generator will more than suffice (also from Stack Overflow):

"the annual risk of a given person being hit by a meteorite is estimated to be one chance in 17 billion, which means the probability is about 0.00000000006 (6 × 10−11), equivalent to the odds of creating a few tens of trillions of UUIDs in a year and having one duplicate."

(... that said, per the author of the function above "Be aware that UUID uniqueness relies heavily on the underlying random number generator (RNG). The solution above uses Math.random() for brevity, however Math.random() is not guaranteed to be a high-quality RNG." So it may not be quite that good but probably good enough!)


Userlevel 7
Badge +10

Fantastic stuff @john_fohrman


Userlevel 7
Badge +9

Hi friends, I wanted to swing by and share some good news! 🚨

If you’d rather not fuss with the code step, a new feature in Formatter now generates a random number for you! 🎉

 

Thanks for sharing your ideas here in community and happy zapping! ⚡️