Activity Forums Salesforce® Discussions How can we generate the random string in Salesforce?

  • PRANAV

    Member
    September 5, 2016 at 6:31 am

    Hi Mohit,

    Try this code

    public class generateRandomString {
    public static String generateRandomString(Integer len) {
    final String chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
    String randStr = '';
    while (randStr.length() < len) {
    Integer idx = Math.mod(Math.abs(Crypto.getRandomInteger()), chars.length());
    randStr += chars.substring(idx, idx+1);
    }
    return randStr;
    }
    }

    Hope this helps you.

    Thanks

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos