You need to sign in to do that
Don't have an account?
balraj singh 33
Tracey MIXED_DML_OPERATION, DML operation on setup object is not permitted Error
i am trying to deactivate the users and send and email for the same , but i am getting the error.Please suggest
Try{
update Userdeactivated;
system.debug(Userdeactivated);
sendemailalert(Userdeactivated);
}
Catch(Exception e){
System.debug(e);
}
}
Public void sendemailalert(list<user> userr) {
Integer i =1;
Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
List<String> ToAddress = new List<String>();
String[] finalemail = a.email__c.split(',');
system.debug(finalemail.size());
for(Integer p = finalemail.size();p>0;p--){
Toaddress.add(finalemail[p-1]);
}
message.setToAddresses(Toaddress);
message.subject = 'Notification for the users deactivated Today';
String plainTextBody = 'List of the Users deactivated due to 90 days of inactivity: ';
for(Integer n = userr.size();n>0;n--){
plainTextBody += '\n'+i+')Name: '+userr[n-1].name+ ' ,UserName: '+userr[n-1].username+' \n';
i++;
system.debug(userr[n-1].name);
system.debug(userr[n-1].username);
}
message.setplainTextBody(plainTextBody);
Messaging.SingleEmailMessage[] messages = new List<Messaging.SingleEmailMessage> {message};
Messaging.SendEmailResult[] results = Messaging.sendEmail(messages);
if (results[0].success) {
System.debug('The email was sent successfully.');
}
else {
System.debug('The email failed to send: ' + results[0].errors[0].message);
}
Try{
update Userdeactivated;
system.debug(Userdeactivated);
sendemailalert(Userdeactivated);
}
Catch(Exception e){
System.debug(e);
}
}
Public void sendemailalert(list<user> userr) {
Integer i =1;
Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
List<String> ToAddress = new List<String>();
String[] finalemail = a.email__c.split(',');
system.debug(finalemail.size());
for(Integer p = finalemail.size();p>0;p--){
Toaddress.add(finalemail[p-1]);
}
message.setToAddresses(Toaddress);
message.subject = 'Notification for the users deactivated Today';
String plainTextBody = 'List of the Users deactivated due to 90 days of inactivity: ';
for(Integer n = userr.size();n>0;n--){
plainTextBody += '\n'+i+')Name: '+userr[n-1].name+ ' ,UserName: '+userr[n-1].username+' \n';
i++;
system.debug(userr[n-1].name);
system.debug(userr[n-1].username);
}
message.setplainTextBody(plainTextBody);
Messaging.SingleEmailMessage[] messages = new List<Messaging.SingleEmailMessage> {message};
Messaging.SendEmailResult[] results = Messaging.sendEmail(messages);
if (results[0].success) {
System.debug('The email was sent successfully.');
}
else {
System.debug('The email failed to send: ' + results[0].errors[0].message);
}
In short, you need to split the process. Deactivate the user like you are currently doing and send email using @future.
Mix dml error occurs when a nonsetup and setup object updated/inserted in the same transaction then mix DML occurs.
There you cannot perform DML on what Salesforce calls setup objects(User in this case) and non-setup object in the same context.
For mix, dml error uses the future method so it will solve the issue.
We can avoid this error using the future method.
Go through with this link, it will help you:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_dml_non_mix_sobjects.htm
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Deepali Kulshrestha.