• #15787
    Salesforce | Mohit Mohit #401

    How can we generate the random string in Salesforce?

    Hi All,

    How can we generate the random string in salesforce?

    I am in scenario in which I want to generate random string sequence in apex .

    Please give suggestion


    Salesforce | PRANAV PRANAV #679

    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.


Viewing 2 posts - 1 through 2 (of 2 total)

Please to reply to this topic.


We're not around right now. But you can send us an email and we'll get back to you, asap.


About Us

Forcetalks is a Salesforce collaboration platform for coders & developers, geeks & nerds, consultants & business heads, admins & architects, managers & marketers and of course the business owners. A community where you can learn from, where you can contribute to. For you. For Salesforce. Read More...

Copyright 2019 Forcetalks. All Right Reserved.

Log in with your credentials


Forgot your details?