function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
PrateekPrateek 

Auto Generate password

I want to generate a 8-character auto password in my standard object, when i will save my record.

Please help me to find out the solution of this.

Thanks in advance!

NaishadhNaishadh

do you want to generate any random password or some specific one?

PrateekPrateek

i want to generate a random password.

Pradeep_NavatarPradeep_Navatar

To generate random password use cryto class in apex and use cryto.getrandominteger() gives the random number.

NaishadhNaishadh
public class PasswordGenerator {
    public static String getPassword(Integer len) {
        Blob blobKey = crypto.generateAesKey(128);
        String key = EncodingUtil.convertToHex(blobKey);
        
        System.debug(key);
        return key.substring(0,len);
    }
    
    public static testMethod void testPG() {
        System.debug(PasswordGenerator.getPassword(8));
    }
}