Created
September 25, 2017 16:59
-
-
Save coder36/cf405cc1abcb6771ae67770d1afc22ca to your computer and use it in GitHub Desktop.
Apex random string generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
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