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
krishnagkrishnag 

random password generation

hi ,

I want to create a random password and send to the users for their login in to my site.I have their salesforce email Ids.So i need to generate a random password.Can anybody share code if you can.

mtbclimbermtbclimber

Have you investigated System.resetPassword()? Does it not work for you, if so why not?

kbromerkbromer

You can convert a set of known values to a 'password' of sorts by using the EncodingUtil system class and the Crypto system class.  Here, I'm creating a random 'secret code', assigning it to a field on object obj called Secret_Code__c, based on the value of the name of the object obj and a random integer from the Crypto class, then converting it to hex:

 

 

obj.secret_code__c = EncodingUtil.convertToHex(Crypto.generateDigest('MD5',Blob.valueOf(obj.name + Crypto.getRandomInteger())));        

 

It's not exactly going to produce easily remembered passwords, but for initial passwords to be changed on login, it would work.  Page 374 in the Apex reference manual covers the crypto class.

We've used this in a Sites page so we can pass client "ID code" via a clear text URL. 

Pradeep_NavatarPradeep_Navatar

You can use crypto class to get random integer .

 

By the way salesforce generate random password at the time of creating a new user.              

 

Database.DMLOptions dlo = new Database.DMLOptions();

dlo.EmailHeader.triggerUserEmail= true;

Database.saveresult sr = Database.insert(u1,dlo);

 

Alternate options:

Integer Rnd = crypto.getRandomInteger();

 

Hope this helps.