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
lavanya gottumukkalalavanya gottumukkala 

How to reset password for a user through apex code

Hi All,

We have one visualforce page.In that page there is one checkbox field related to contact object.What our requirement is when we click on that check box,the 'Active' checkbox in user object should be checked and reset password for that user.Can any one help me?

Thanks in advance
Himanshu ParasharHimanshu Parashar
Hi Iavanya,

I assume that you will get userid in apex and once you have that you can call

 
public void updateUser()
{
objUser.isactive=true;
update objUser;
System.resetPassword(userid,false);
}



Thanks,
Himanshu
ManojjenaManojjena
HI Lavanya,
Are you talking about Community/Portal user ? That when you click teh checkbox in contact then teh user related to contact will be active and it will send for reset password link to user ?

If yes then you need write logic in your VF page controller or extension to collect the checked contact Id and then Query from user to get the related users which are inactive .then you can follow what HImansu has posted .

Here one more thing I need to tell you that

public static System.ResetPasswordResult resetPassword(ID userId, Boolean sendUserEmail)
resetPassword method wil take id and boolean .You need to pass the Id of user and boolean if you make true then it will send email to the user inbox to reset the password .

If the contact is not related to community or portal then which user you want to do the action .

If you want to do with other approach you can check below links in appexchage there are two application to reset mass usser's password .

https://appexchange.salesforce.com/listingDetail?listingId=a0N30000003GwqQEAS
https://appexchange.salesforce.com/listingDetail?listingId=a0N3000000B4QBQEA3

Thanks
Manoj
No Name SFDCNo Name SFDC
Well written Manoj Jena.