Note: This release is in preview. Features described here don’t become generally available until the latest general availability date that Salesforce announces for this release. Before then, and where features are noted as beta, pilot, or developer preview, we can’t guarantee general availability within any particular time frame or at all. Make your purchase decisions only on the basis of generally available products and features.

UUID Class

Contains methods to randomly generate a version 4 universally unique identifier (UUID), compare UUIDs, and convert UUID instance to a string.

Namespace

System

Usage

The UUID is generated using a cryptographically strong pseudo-random number generator and is represented as 32 hexadecimal values.

UUID Methods

The following are methods for UUID.

equals(obj)

Compares a UUID instance with the specified object and returns true if both are equal. Otherwise, returns false.

Signature

public Boolean equals(Object obj)

Parameters

  • obj: Type: Object The UUID object to be compared.

Return Value

Type: Boolean

Example

1
2// UUIDs are equal when all the characters in the UUID are the same
3String uuidStr = '707b2538-98bb-41e7-95e3-1d77bf42b102';
4UUID fromStr = UUID.fromString(uuidStr);
5UUID fromStr2 = UUID.fromString(uuidStr);
6Assert.isTrue(fromStr.equals(fromStr2));
7
8// A UUID is never equal to a String or any non-UUID object
9Assert.isFalse(fromStr.equals(uuidStr)); 
10

fromString(str)

Converts a 32 character hexadecimal string representation of a UUID to a UUID instance.

Signature

public static System.UUID fromString(String str)

Parameters

Return Value

Type: System.UUID

Example

1String uuidStr = '707b2538-98bb-41e7-95e3-1d77bf42b102';
2UUID fromStr = UUID.fromString(uuidStr);
3
4UUID.fromString(null); // Throws NullPointerException
5
6UUID.fromString(‘not a uuid’); // Throws IllegalArgumentException
7

hashCode()

Returns the hashcode corresponding to the UUID instance.

Signature

public Integer hashCode()

Return Value

Type: Integer

randomUUID()

A static method that randomly generates a version 4 UUID.

Signature

public static System.UUID randomUUID()

Return Value

Type: System.UUID

A 32 hexadecimal value of the UUID generated.

Example

1UUID randomUUID = UUID.randomUUID();
2system.debug(randomUUID); // Prints the UUID string that was randomly generated
3

toString()

Returns the string representation of the UUID instance.

Signature

public String toString()

Return Value

Type: String