Generating UUIDs In Apex (Spring ’24 Release)

The Spring ’24 release of Salesforce brought a noteworthy enhancement for Apex developers: the capability to generate Version 4 UUIDs (Universally Unique Identifiers). These UUIDs are crafted using a cryptographically robust pseudo-random number generator, guaranteeing distinct identifiers for your Salesforce objects. Here’s a brief tutorial on incorporating this feature into your Apex code.

Grasping the UUID Class

The recently introduced UUID class in Apex serves as your primary tool for generating and manipulating UUIDs. This class provides several methods for UUID operations.

  • randomUUID(): Generates a random UUID.
  • equals(obj): Compares the UUID instance with another object.
  • hashcode(): Returns the hashcode of the UUID.
  • fromString(string): Creates a UUID instance from a string representation.
  • toString(): Converts a UUID instance to its string representation.

Creating a UUID

To produce a UUID in Apex, utilize the randomUUID() method from the UUID class. Here’s an illustration.

UUID randomUuid = UUID.randomUUID();

This code snippet generates a UUID and logs it to the debug log. The resulting UUID is a 32-character hexadecimal string, providing a unique identifier for an object.

Converting UUID to String and Vice Versa

Converting UUIDs to strings or creating UUIDs from strings may be necessary. The UUID class offers the toString() and fromString(string) methods for these tasks.

// Convert UUID to String
String uuidStr = randomUuid.toString();

// Create UUID from String
UUID fromStr = UUID.fromString(uuidStr);

Practical Applications in Salesforce

UUIDs play a crucial role in ensuring unique identification in distributed systems such as Salesforce. They are particularly well-suited for situations where guaranteeing a record or object possesses a distinct identifier is vital, especially in integrations, data migrations, or custom development.


The incorporation of UUID generation in Apex introduces a potent tool for developers operating on the Salesforce platform. It simplifies procedures demanding unique identifiers and elevates data integrity across systems. Begin integrating UUIDs into your Apex code to leverage this robust feature.