Skip to content

Instantly share code, notes, and snippets.

@coder36
Created September 25, 2017 16:59
Show Gist options
  • Save coder36/cf405cc1abcb6771ae67770d1afc22ca to your computer and use it in GitHub Desktop.
Save coder36/cf405cc1abcb6771ae67770d1afc22ca to your computer and use it in GitHub Desktop.
Apex random string generator
trigger ContactGuidGenerator on Contact (before insert) {
for( Contact c: Trigger.new ) {
final String chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
String guid = '';
while (guid.length() < 16) {
Integer idx = Math.mod(Math.abs(Crypto.getRandomInteger()), chars.length());
guid += chars.substring(idx, idx+1);
}
c.guid__c = guid;
}
}
@nivethx
Copy link

nivethx commented Oct 11, 2018

does this create duplicate values? how to make sure that you are not having duplicate values

@nivethx
Copy link

nivethx commented Oct 11, 2018

Maybe check c.guid__c if the created random string already exist and create a new random string or not depending on that, which would be better IMO

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment