• #19740
    Salesforce | Saurabh Saurabh #255

    Can we generate the random string or random password using Apex?

    Can we generate the random string or random password using Apex?


    Salesforce | Suraj Suraj #149

    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);

    Salesforce | Parul Parul #3


    public String testGenerate(Integer getLength)

    String charString = ‘[email protected]#$%^&*()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.

Viewing 3 posts - 1 through 3 (of 3 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?