Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jongpie/apexuuid
A small library to provide UUID generation and validation in Apex
https://github.com/jongpie/apexuuid
apex apex-guid apex-uuid forcedotcom guid salesforce uuid uuid4
Last synced: 21 days ago
JSON representation
A small library to provide UUID generation and validation in Apex
- Host: GitHub
- URL: https://github.com/jongpie/apexuuid
- Owner: jongpie
- License: mit
- Created: 2017-02-06T08:10:50.000Z (over 7 years ago)
- Default Branch: main
- Last Pushed: 2024-05-01T23:03:41.000Z (6 months ago)
- Last Synced: 2024-05-02T05:43:06.046Z (6 months ago)
- Topics: apex, apex-guid, apex-uuid, forcedotcom, guid, salesforce, uuid, uuid4
- Language: Apex
- Homepage:
- Size: 56 MB
- Stars: 65
- Watchers: 8
- Forks: 23
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Apex UUID
Provides a way to generate a [UUID (Universally Unique Identifier)](https://en.wikipedia.org/wiki/Universally_unique_identifier) in Salesforce's Apex language. This uses Verion 4 of the UUID standard - more details available [here]()
## Unlocked Package (no namespace) - v2.1.0
[![Install Unlocked Package in a Sandbox](./images/btn-install-unlocked-package-sandbox.png)](https://test.salesforce.com/packaging/installPackage.apexp?p0=04t4x000000NYNEAA4)
[![Install Unlocked Package in Production](./images/btn-install-unlocked-package-production.png)](https://login.salesforce.com/packaging/installPackage.apexp?p0=04t4x000000NYNEAA4)# Getting Started
## Generating & Using A UUID
To generate a UUID, simply instantiate a new instance. The string value can be retrieved with `getValue()`
```java
Uuid myUuid = new Uuid();
String myUuidValue = myUuid.getValue();
```You can use the UUID value as a unique ID, generated by Apex. This lets you do some powerful things in Apex when your objects have external ID fields to store the UUID value.
It's best if you create a custom field on your object to store the UUID value with these properties: Text(36) (External ID) (Unique Case Insensitive).
In the code samples below, the field name Uuid\_\_c is used as an example field name.
> #### Example: Using a code-generated external ID (such as a UUID), we can create multiple accounts and related contacts with only 1 DML statement
```java
List recordsToCreate = new List();
// Create 10 sample accounts
for(Integer accountCount = 0; accountCount < 10; accountCount++) {
// Create a new account & set a custom external ID text field called Uuid__c
Account newAccount = new Account(
Name = 'Account ' + accountCount,
Uuid__c = new Uuid().getValue()
);
recordsToCreate.add(newAccount);// For each sample account, create 10 sample contacts
for(Integer contactCount = 0; contactCount < 10; contactCount++) {
// Instead of setting contact.AccountId with a Salesforce ID...
// we can use an Account object with a Uuid__c value to set the Contact-Account relationship
Contact newContact = new Contact(
Account = new Account(Uuid__c = newAccount.Uuid__c),
LastName = 'Contact ' + contactCount
);
recordsToCreate.add(newContact);
}
}
// Sort so that the accounts are created before contacts (accounts are the parent object)
recordsToCreate.sort();
insert recordsToCreate;// Verify that we only used 1 DML statement
System.assertEquals(1, Limits.getDmlStatements());
```### Using a UUID's string value
If you already have a UUID as a string (previously generated in Apex, generated by an external system, etc), there are 3 static methods to help work with the string value.
1. **Do I have a valid UUID value?**
This checks if the string value matches the regex for a UUID v4, including hyphens (but it is case-insensitive)
```java
Boolean isValid = Uuid.isValid(myUuidValue);
```2. **I have a UUID value but need to format it**
This returns a formatted string that follows the UUID pattern 8-4-4-4-12 in lowercase. If an invalid string is provided, a UuidException is thrown.
```java
String formattedUuidValue = Uuid.formatValue(myUnformattedUuidValue);
```3. **I have a UUID value, how can I use it to construct a UUID?**
This will automatically format the value for you, but the intial value must be a valid (unformatted) UUID string
```java
Uuid myUuid = Uuid.valueOf(myUuidValue);
```