Activity Forums Salesforce® Discussions Can we generate the random string or random password using Apex?

  • Suraj

    Member
    April 26, 2017 at 11:39 am

    Hi Saurabh,

    You can use this:

    Integer len = 10;
    Blob blobKey = crypto.generateAesKey(128);
    String key = EncodingUtil.convertToHex(blobKey);
    String pwd = key.substring(0,len);

  • Parul

    Member
    September 19, 2018 at 5:31 pm

    Hi,

    public String testGenerate(Integer getLength)

    {
    String charString = ‘!@#$%^&*()nopqrstuvwABCDPQRSTUVWXYZ0123456789abcdefghijkEFGHIJKLMNOlmxyz’;
    String randomNew = ”;
    while (randomNew .length() < getLength)

    {
    Integer changeInt = Math.mod(Math.abs(Crypto.getRandomInteger()), charString.length());
    randomNew += charString.substring(changeInt , changeInt +1);
    }
    return randomNew ;
    }

    Hope this helps.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos