=====================================
Guides, or Globally Unique Identifiers, are 128-bit numbers used to identify information in computer systems. In Apex, generating a GUID/UUID can be achieved through various methods. This article will provide an overview of the different approaches, including built-in methods and custom implementations.
Starting from Spring '24, Salesforce provides a built-in UUID
class with a method to generate random UUIDs.
UUID.randomUUID()
method to generate a random UUID instance.toString()
method.UUID uuIdV4 = UUID.randomUUID();
String uuIdV4Str = UUID.randomUUID().toString();
For versions prior to Spring '24, you can use a custom implementation to generate a GUID/UUID.
One approach is to use the Crypto.generateAESKey()
method to generate a random 128-bit key, which can be converted to a hexadecimal string and formatted as a GUID/UUID.
Blob b = Crypto.generateAESKey(128);
String h = EncodingUtil.convertToHex(b);
String guid = h.substring(0, 8) + '-' + h.substring(8, 12) + '-' + h.substring(12, 16) + '-' + h.substring(16, 20) + '-' + h.substring(20);
Another approach is to create a custom GuidUtil
class that generates a GUID/UUID using a cryptographically secure random number generator.
public class GuidUtil {
private static List<String> hexMap = new List<String> {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
};
public static String NewGuid() {
// Generate 128 random bits
String randomStringAsHex = EncodingUtil.convertToHex(Crypto.generateAESKey(128));
// Set the version and variant
String versionHexBits = randomStringAsHex.substring(14, 16);
String variantHexBits = randomStringAsHex.substring(18, 20);
Integer versionIntBits = convertHexToInt(versionHexBits);
Integer variantIntBits = convertHexToInt(variantHexBits);
Integer versionShiftedIntBits = versionIntBits & 15 | 64;
Integer variantShiftedIntBits = variantIntBits & 63 | 128;
String versionShiftedHexBits = convertIntToHex(versionShiftedIntBits);
String variantShiftedHexBits = convertIntToHex(variantShiftedIntBits);
// Format the GUID/UUID
String guid = randomStringAsHex.substring(0, 8) + '-' + randomStringAsHex.substring(8, 12) + '-' + versionShiftedHexBits + randomStringAsHex.substring(14, 16) + '-' + variantShiftedHexBits + randomStringAsHex.substring(18, 20) + '-' + randomStringAsHex.substring(20);
return guid;
}
static Integer convertHexToInt(String hex) {
Integer d0 = hexMap.indexOf(hex.substring(1, 2));
Integer d1 = hexMap.indexOf(hex.substring(0, 1));
Integer intval = d0 + (d1 * 16);
return intval;
}
static String convertIntToHex(Integer intval) {
String hs0 = hexMap.get(intval & 15);
String hs1 = hexMap.get(((intval >> 4) & 15));
return hs1 + hs0;
}
}
Method | Description | Security |
---|---|---|
Built-in (Spring '24 and later) | Uses the UUID class to generate a random UUID |
High |
Custom (Crypto.generateAESKey()) | Uses the Crypto.generateAESKey() method to generate a random 128-bit key |
Medium |
Custom (GuidUtil Class) | Uses a custom implementation with a cryptographically secure random number generator | High |
In conclusion, generating a GUID/UUID in Apex can be achieved through various methods, including built-in and custom implementations. The choice of method depends on the specific requirements and security needs of your application.
UUID
class (Spring '24 and later) for simplicity and securityMath.random()
for generating GUID/UUIDs due to its lack of security and uniqueness guarantees