Try for free Book a demo

Logic App Best Practices, Tips, and Tricks: #35 Generate a Unique Identifier

Microsoft Azure

6 Mins Read

generate unique identifier in logic apps

Welcome again to another Logic Apps Best Practices, Tips, and Tricks. In my previous blog posts, I talked about some of the essential best practices you should use while working with Azure Logic Apps. Check out these Logic App tips and tricks!

Today I will speak about another useful Best practice, Tips, and Tricks that you must consider while designing your business processes (Logic Apps): How to generate a Unique Identifier in Logic Apps, aka GUIDs.

generate unique identifier in logic apps

Generate a Unique Identifier in Logic Apps

There are so many scenarios that I faced in more than 18 years doing integration that required me to generate a unique id that I don’t know how to start explaining all of them. Unique identifiers are handy when an automation process needs to generate a unique reference “number” to identify an object or entity like a Customer account, a document id, and so on. Most of the time, we use GUIDs, which stands for a globally unique identifier, and it is usually a 128-bit text string that represents an identification (ID) that is unlikely ever to repeat or create a collision to address these scenarios unless requirements don’t allow us to use a GUID.

How does a GUID work?

GUIDs are constructed in a sequence of digits that equal 128 bits. The ID is in hexadecimal digits, which means it will use the numbers 0 through 9 and letters A through F. The hexadecimal digits are grouped in a format that is 36 characters long — 32 hexadecimal characters grouped as 8-4-4-4-12 and separated by four hyphens: {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}.

However, we can apply certain GUID format variations like:

  • Guid without {braces}: 00000000-0000-0000-0000-000000000000 – equivalent to C# code Guid.NewGuid().ToString(“D”)
  • Guid without braces and hyphens: 00000000000000000000000000000000 – equivalent to C# code Guid.NewGuid().ToString(“N”)
  • Guid separated by hyphens, enclosed in parentheses: (00000000-0000-0000-0000-000000000000) – equivalent to C# code Guid.NewGuid().ToString(“P”)
  • or even Guid with hexadecimal values: {0x00000000,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}} – equivalent to C# code Guid.NewGuid().ToString(“X”)

Working with Logic Apps is not so different from traditional programming languages. Maybe it is not as powerful as traditional programming languages like C#. Still, by default, depending on your requirements, of course, you don’t need to combine multiple expressions, use a support database to generate a unique identifier or GUIDs or even call Azure Functions.

Out-of-the-box inside Logic Apps, you can make use of two different strategies to “generate” a unique identifier:

  • Using the Logic App Run History Identifier or Run Id: Each workflow run generates a unique Run Id.
    • Note: When you view the Logic App run history, you can see that all the runs have their unique identifier. For example, the Run Id also is being passed to any child workflows for us to be able to trace it across these executions as well.
  • Or by using the guid() string function: This function will generate a globally unique identifier (GUID) as a string.

Logic App Run History Identifier

As I mentioned, each workflow run generates a unique Run Id that makes this strategy a perfect candidate to use when we need to generate a unique identifier. And we can easily access this information using the workflow() function. This function returns all the details about the workflow itself during run time, and one of these properties is the Run Id:

  • workflow()[‘run’][‘name’]

Logic App Run History Identifier

The only limitations of this approach are that:

  • Despite that will be a unique identifier, it is not a Guid.
  • If you have scenarios in which you need to generate a unique identifier for each record present in an array inside the same Logic App run, you cannot use this approach or strategy since the Run Id is unique for each run execution.

This is a sample of the run id extracted in runtime:

generate unique identifier in logic apps

guid()

Another option, of course, is to use the guid() function that will always generate a random GUID each time you run the function. As I mentioned above, there is, and we can actually use several deviations from the Guid format, but guess what? Logic Apps supports all of them!

The simple execution of this function is by using the following sentence without any parameter:

guid()

Retrieving a default grid

This is actually the same as using the following expression with the ‘D’ parameter:

guid('D')

Both these expressions will return a 32 digits separated by hyphens – 00000000-0000-0000-0000-000000000000 – in other words, a Guid without {braces}: 0c8d294b-ae98-4df1-ab04-144306b8af4a.

Now, if we want a simple 32 digits result – 00000000000000000000000000000000 – in other words, a Guid without braces and hyphens, we can make use of the same function but this time passing the ‘N’ value as a parameter:

guid('N')

retrieve a 32 digits separated by hyphens, enclosed in parentheses

The result will be something like this: 6141e6e25ace4b7f996b2f2a2c3ed063

However, if we want to retrieve a “default” guid – {00000000-0000-0000-0000-000000000000} – in other words, a 32 digits separated by hyphens, enclosed in braces, we can make use of the same function but this time passing the ‘B’ value as a parameter:

guid('B')

generate unique identifier in logic apps

The result will be something like this: {18b3a22a-69a3-4221-8773-55146a28d7ed}

Now, if we want to retrieve a 32 digits separated by hyphens, enclosed in parentheses – (00000000-0000-0000-0000-000000000000) –  we can make use of the same function but this time passing the ‘P’ value as a parameter:

guid('P')

retrieve a 32 digits separated by hyphens

The result will be something like this: (7a48c74b-c6b7-4ff0-8562-7ba9baef8b0b)

Finally, if we want to retrieve a Four hexadecimal values enclosed in braces, where the fourth value is a subset of eight hexadecimal values that is also enclosed in braces – {0x00000000,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}} –  we can make use of the same function but this time passing the ‘X’ value as a parameter:

guid('X')

retrieve a Four hexadecimal values

The result will be something like this: {0xa779ca69,0xbf45,0x47c2,{0x95,0x08,0x10,0xc6,0x7e,0x5c,0xf3,0xa7}}

In all cases, if you want to convert them to uppercase, call the toUpper() alongside the guid():

toUpper(guid())

calling the toUpper() alongside the guid()

Any other approach you want to implement, like:

  • YouTube like Guid
  • or Short Guid using tickets

The best option is to implement these capabilities inside an Azure Function and call it from our Logic App. You can find a sample of these Azure Functions on my GitHub page: Azure Functions To Generate GUIDs.

I hope you enjoy this developer tip and stay tuned for the following Logic App Best practices, Tips, and Tricks.

Related reading

This article was published on Jun 22, 2023.

Related Articles