Returns
| string | A new UUID string, unique on each call. |
Examples
getUUID returns a random universally unique identifier (UUID) as a string. Reach for it whenever you need a collision-free value: a unique temp file name, an externalId for a record (visitor, subscription), or an event / retry key. Because each call is unique, it removes the need to coordinate counters or risk overwriting another run’s data.
Its format is lowercase hexadecimal with the 8-4-4-4-12 form. It is a global function available in every server-side JavaScript context (workflow, JSSP, web app, typology, delivery), no arguments, no instantiation. Call it directly and use the returned string; every call yields a new value, so capture it in a variable if you need to reuse the same id twice.
// generate a UUID for a new recipient
const uuid = getUUID(); // de7fab86-e3f4-4f28-bd47-49a09e38a0bf
const recipient = NLWS.nmsRecipient.create({
recipient: {
email: uuid + "@instance.com",
account: uuid,
},
});
recipient.save();
// generate a UUID for a file
const rejectFilename = application.instanceVarDir + "/" + getUUID() + ".csv";Unit tests
Tested with acc by running
acc instance exec -f test/integration/getUUID.spec.js in a sandbox:
loadLibrary("/nl/core/shared/nl.js"); // import NL.assert
context.test1 = NL.assert(typeof getUUID === "function");
context.test2 = NL.assert(typeof getUUID() === "string");
context.test3 = NL.assert(getUUID().length === 36);
const uuidFormat = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
context.test4 = NL.assert(uuidFormat.test(getUUID()));
const uuid1 = getUUID(); // e793da02-4540-4598-9c6e-c25e24d63095
const uuid2 = getUUID(); // 613bb07e-c8df-47a9-861e-baabcefb3855
context.test10 = NL.assert(uuid1 !== uuid2);
var collisions = 0;
const uuid3 = getUUID();
for (var i = 0; i < 1000; i++) {
if (uuid3 === getUUID()) {
collisions++;
}
}
context.test11 = NL.assert(collisions === 0);See also
-
📕 instanceVarDir- The absolute path to the instance VAR folder, as a read-only string.