Skip to content

Instantly share code, notes, and snippets.

@doppiomacchiatto
Forked from coder36/apex random string
Created November 10, 2018 22:33
Show Gist options
  • Save doppiomacchiatto/c2d4ebfed5fd34d2833c7f7480880198 to your computer and use it in GitHub Desktop.
Save doppiomacchiatto/c2d4ebfed5fd34d2833c7f7480880198 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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment