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
nbansal10nbansal10 

Reset password for multiple users through Apex

Hello Guys,

 

I want to reset password and send an email for multiple users with no login history through apex. The code which I have written for even one user gives System.LimitException: DML currently not allowed. I am stuck on this issue for a while. Please help me out on this.

 

Apex code:

public class UserList{

public List<User> usr{get;set;}
public Datetime lastlogin;
public Id usrId;
public String username;
public UserList()
{
usr=[select id,name,LastLoginDate, Username from User where Username='xxx@rediffmail.com'];

for (User u : usr) {
lastlogin = u.LastLoginDate;
system.debug('@@@@@lastlogin' + lastlogin);
usrId = u.Id;
system.debug('@@@@@usrId' + usrId);
username = u.Username;
system.debug('@@@@@username'+ username);
if(lastlogin==null) {
system.resetPassword(usrId, true); //getting exception on this line, please help me out...!!


}

}
}

}

}

 

 

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox
You can't perform any DML in a page's controller's constructor. Move your logic to a separate function, and call that function from the page's "action" attribute. Also, filter your user query by "isactive = true and lastlogindate = null" to avoid querying users that are inactive or have logged in.

All Answers

sfdcfoxsfdcfox
You can't perform any DML in a page's controller's constructor. Move your logic to a separate function, and call that function from the page's "action" attribute. Also, filter your user query by "isactive = true and lastlogindate = null" to avoid querying users that are inactive or have logged in.
This was selected as the best answer
nbansal10nbansal10

How stupid of me.. How could I forget the basic thumb rule.. Thanks man,,!!

nbansal10nbansal10
I got stuck on this again.. I want to pass email ids from a csv file to Username in the query. Still getting the DML exception, please guide from where should I call this method.. See code snippet..
public class uploadCSVcontroller {

public Blob contentFile { get; set; }
public String nameFile { get; set; }
public Integer rowCount { get; set; }
public Integer colCount { get; set; }

//User related variables
public List<User> usr{get;set;}
public Datetime lastlogin;
public Id usrId;
public String username;
public boolean isactive;

public List<List<String>> getResults() {
List<List<String>> parsedCSV = new List<List<String>>();
rowCount = 0;
colCount = 0;
if (contentFile != null){
String fileString = contentFile.toString();

parsedCSV = parseCSV(fileString, false);
system.debug('@@@@parsedCSV.get(0): '+parsedCSV.get(0));
system.debug('@@@@parsedCSV: '+parsedCSV);
rowCount = parsedCSV.size();
for (List<String> row : parsedCSV){
if (row.size() > colCount){
colCount = row.size();
}
}
}
return parsedCSV;
}


public static List<List<String>> parseCSV(String contents,Boolean skipHeaders) {
List<List<String>> allFields = new List<List<String>>();

contents = contents.replaceAll(',"""',',"DBLQT').replaceall('""",','DBLQT",');
contents = contents.replaceAll('""','DBLQT');
List<String> lines = new List<String>();
try {
//lines = contents.split('\n'); //correction: this only accomodates windows files
lines = contents.split('\r'); // using carriage return accomodates windows, unix, and mac files
} catch (System.ListException e) {
System.debug('Limits exceeded?' + e.getMessage());
}
Integer num = 0;
for(String line: lines) {
// check for blank CSV lines (only commas)
if (line.replaceAll(',','').trim().length() == 0) break;

List<String> fields = line.split(',');
List<String> cleanFields = new List<String>();
String compositeField;
Boolean makeCompositeField = false;
for(String field: fields) {
if (field.startsWith('"') && field.endsWith('"')) {
cleanFields.add(field.replaceAll('DBLQT','"'));
} else if (field.startsWith('"')) {
makeCompositeField = true;
compositeField = field;
} else if (field.endsWith('"')) {
compositeField += ',' + field;
cleanFields.add(compositeField.replaceAll('DBLQT','"'));
makeCompositeField = false;
} else if (makeCompositeField) {
compositeField += ',' + field;
} else {
cleanFields.add(field.replaceAll('DBLQT','"'));
}
}

allFields.add(cleanFields);
}
if (skipHeaders) allFields.remove(0);
return allFields;
}

public void resetpwd() {
system.debug('@@@@@ in resetpwd');
List<List<String>> parsedCSV = new List<List<String>>();
if (contentFile != null){
String fileString = contentFile.toString();
parsedCSV = parseCSV(fileString, false);
system.debug('@@@@parsedCSV in resetpwd: '+parsedCSV);
}
Integer count = 0;
system.debug('@@@@parsedCSV.size(): '+ parsedCSV.size());
while(count<parsedCSV.size()) {

usr=[select id,name,LastLoginDate, isactive, Username from User where Username= :parsedCSV.get(count)];
system.debug('_____________________'+usr);

for (User u : usr) {
lastlogin = u.LastLoginDate;
system.debug('@@@@@lastlogin' + lastlogin);
usrId = u.Id;
system.debug('@@@@@usrId' + usrId);
username = u.Username;
system.debug('@@@@@username'+ username);
isactive = u.isactive;
if(lastlogin==null && isactive == true) {

system.resetPassword(usrId, true);
}
}
}
}
}