Creating UUIDs

🔡 Learn how to generate unique UUIDs for data identification.

UUIDs are unique strings of characters used as an identifier between data. These unique identifiers are generated in a way that makes them unlikely to collide with other UUIDs, and it looks something like this: 550e8400-e29b-41d4-a716-446655440000.

How to Create UUIDs?

We first check if a UUID field already exists in the current entry, and if it does, we skip generating a new UUID. If not, we generate a new UUID and update the entry with it.

See the example below:

async function script(C) {
  const currentEntry = await C.getCurrentEntry(); 
  const uuidValue = C.nanoid();  
  
  const uuidFieldFound = currentEntry.hasOwnProperty("uuid-field-internal-name-in-the-entry");
 		if (
   			uuidFieldFound == true &&
   			currentEntry["1614495-uuid"] &&
   			currentEntry["1614495-uuid"].length > 0
 		)
			{
  			return;
			}
		else {
  			const response = await C.updateEntries({
  				updates: [
  		 			{
  						value: { "uuid-field-internal-name-in-the-entry": uuidValue },
							recordInternalId: "record-internal-id",
							entryId: currentEntry.recordValueId,
			 			},
					],
				});
		return { uuidValue, currentEntry };
	}
}

Let's break down each component:

  1. Generate a New UUID

To create a new UUID, we use the C.nanoid() function. This function generates a fresh and unique UUID.

async function createUUID() {
  const uuidValue = C.nanoid();
  1. Check for Existing UUID Field

We check whether the UUID field already exists in the current entry. This is important to avoid overwriting existing UUIDs.

const uuidFieldFound = currentEntry.hasOwnProperty("uuid-field-internal-name-in-the-entry");
  1. Skip Generation if UUID Exists

If the UUID field is already exists, no need to generate a new one. In such cases, we exit the function.

if (uuidFieldFound == true && currentEntry["uuid-field-internal-name-in-the-entry"].length > 0) {
    return;
  }
  1. Update the Entry with the Generated UUID

When a new UUID is needed, we update the current entry with the newly generated UUID using the C.updateEntries() function.

const response = await C.updateEntries({
      updates: [
        {
          value: { "uuid-field-internal-name-in-the-entry": uuidValue },
          recordInternalId: "record-internal-id",
          entryId: currentEntry.recordValueId,
        },
      ],
    });
  1. Return the Generated UUID and Current Entry

Finally, we return an object containing the generated UUID (uuidValue) and the current entry.

return { uuidValue, currentEntry };

Output

The output of the create UUID function will be an object containing the generated UUID (uuidValue) and the current entry. You can use this UUID to identify and retrieve the associated information.

📌 Need Help?

If you require assistance or encounter any issues, please don't hesitate to contact us for further support.