jongpie / apexuuid Goto Github PK
View Code? Open in Web Editor NEWA small library to provide UUID generation and validation in Apex
License: MIT License
A small library to provide UUID generation and validation in Apex
License: MIT License
Hello,
Great class!
I have been testing its implementation to chain creation of sObjects in one DML statement using an External ID and followed your documentation example with Account and Contact but my attempts are generating a DML exception.
static void testInsertChainedSObjectsInOneDML() {
Exception dmlex;
List<SObject> recordsToCreate = new List<SObject>();
// 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();
try {
insert recordsToCreate;
} catch(DMLException ex) {
dmlex = ex;
}
System.assertEquals(null, dmlex, 'Expected no DML exception when chaining insert with UUID as external ID');
// Verify that we only used 1 DML statement
// System.assertEquals(1, Limits.getDmlStatements(), 'Expected only one DML statement');
}
UUIDTest.testInsertChainedSObjectsInOneDML System.AssertException: Assertion Failed: Expected no DML exception when chaining insert with UUID as external ID: Expected: null, Actual: System.DmlException: Insert failed. First exception on row 10; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [UUID__c]: [UUID__c]
It works if I insert in separate DMLs the lists I gathered for each sObject type, but, not with single one.
Any ideas?
Currently, it calls String.isBlank(guid)
instead of reusing the Guid.isEmpty
method
The current implementation of the code uses v4
of the UUID standard, which is still a very reliable manner of generating a randomized, unique ID. However, there are several newer versions of UUIDs, each with their own use cases. The specs for the newer versions aren't fully finalized, but seem to be I'd like to expand the current code to provide a way to specify which version of UUID is generated.
Some resources that discuss the differences & benefits of some of the new versions:
At the moment, I'm considering these versions:
UUID
value when the same inputs are usedThere are a few other official & unofficial versions, but these seem like the most useful ones to focus on.
The implementation is not really conform with RFC 4122.
It doesn't not define a version nor a variant.
I guess it would be best do make it a version 4 variant 1 uuid. This will allow 122 or even 123 random bits.
If you are interested how it's done. Python has "ok readable" implementation of UUID version 4, see https://github.com/python/cpython/blob/master/Lib/uuid.py
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.