Nintex Workflow – Random Number UDA
Sticking with the No-Code standard, I really wanted an out of the box Nintex Workflow User Defined Action that would generate me a random number. Some of you may have seen a previous post I did which connected to SQL server to do it. But that requires you to have some connectivity to SQL, which not all users have. That kind of takes away from Nintex’s motto of “Workflow for Everyone”. Another solution, would be to build an Inline function for Nintex Workflow. That would actually be quite easy. You could do with code and write your own C# and deploy the assembly. But again, this would require development skills, not to mention you’ll need to convince your SharePoint admin to deploy a solution to your farm.
While tinkering around the other day, I found another way I could do it. Now granted, this is not elegant. Not event remotely. But it does work since I’ve run it through a bunch of tests, even with extremely large numbers.
Since there’s no Random action or fn-Random Inline function, here’s a whacky way of doing it.
User Defined Action
Firstly, the User Defined Action will need to take two parameters. A number that specifies the limit of the random number set. So if you want a number between 0 and 9, then you set this number to be 10 (this is the count of the numbers we can use).
The second parameter is an output parameter, which is the random number.
The epiphany I had, was that there is an Inline function I can use, called fn-NewGuid(). Now this function returns a Guid, and for those that don’t know what that is, it looks something like this : 3F2504E0-4F89-41D3-9A0C-0305E82C3301 , and you can find more information here – What is a Guid?
The whole point of using Guids, is that each one is unique. If you noticed the Guid in the example above, it’s made up of numbers, alpha characters and dashes. If you remove all the alpha’s and dashes, you’re left with a large number. That’s what I use to start my random number calculation. so taking out the unnecessary characters in the example above, we end up with : 325040489413900305823301
Now that we have this number, we need to look at the max number count we are looking at, eg. 10. We figure out how many characters it is, in this case 2 characters, and we then pull out that many characters from our Guid number –> 32
We then take that number, and perform a MOD (MODular arithmetic). eg 32 mod 10 = 2 There’s our random number.
If our Guid number of 93829392823, then we’d do a 93 mod 10 = 3
I know.. I know… this is a little confusing. But it work. Trust me. 🙂
I won’t go through the actions in the UDA, because the above pretty much explains it. But here is what the UDA looks like :
Now that you see the UDA, how do you use it. Once you have downloaded the UDA below and published it, you’ll be able to use it any of your workflows. Simply drag the UDA onto the workflow open it up and put in the value you want and a variable to store the random number.
I created a workflow that not only shows how to use the UDA, but also runs it 20 times so we can test it out to make sure we get the results we want.
It’s not complex. We have a Loop action that iterates 20 times. Inside is a Math Calculation that increments the number for the Loop and the UDA that generates the random number. Finally, a Log action that logs the random number