Sometimes what is so easy in other languages and scripting tools can be a real pain in Apex and Formula Fields. I am talking about random character generation. Not sure why this is not a method but have used this trick a couple of times and in doing some reading it actually taking about 1/10 the computing power to generate versus creating a long string of characters and randomly picking out of it which seems to be a popular programing pattern.
Blob blobKey = crypto.generateAesKey(128);
String updateKey = EncodingUtil.base64encode(blobKey);
updateKey = updateKey.left(10);
Quick easy and simple. I added the third line to get a more manageable size of code but it’s a great way to stamp a key on records when doing parent/child updates where you want to make sure that all the records are actually getting updated.
And as always this is the source URL: https://salesforce.stackexchange.com/a/99390
(Update 2019): Another method:
Datetime uniqueDate = dateTime.now();
String uniqueUser = uniqueDate.format(‘MMddyyyHHmmss’);
Internet started as a place of knowledge, I’m just paying it forward.