You need to sign in to do that
Don't have an account?
Sumeet Kore 1
Update Account's Owner id
Hi,
Here is my code and I am getting error for DMLException and cannot change the setup object
trigger TransferInactiveUsers on User (after update) {
Integer count = 0;
List<ID> userids = new List<ID>();
For (User usr : Trigger.new){
if (Trigger.oldMap.get(usr.Id).IsActive == true)
if(Trigger.newMap.get(usr.Id).IsActive == false)
userids.add(usr.Id);
}
List<Account> acctList=[SELECT ID FROM Account WHERE OwnerId IN :userids];
for(Account ac : acctList)
{
ac.OwnerId = System.Label.Owner_ID;
count++;
}
update acctList;
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setSubject('Owners changed for records ');
mail.setPlainTextBody('The Total number of records whose owner changed are :'+count);
mail.toAddresses = new String[] { 'Label.Email_addr_one'};
Messaging.SingleEmailMessage[] messages = new List<Messaging.SingleEmailMessage> {mail};
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);
}
}
Here is my code and I am getting error for DMLException and cannot change the setup object
trigger TransferInactiveUsers on User (after update) {
Integer count = 0;
List<ID> userids = new List<ID>();
For (User usr : Trigger.new){
if (Trigger.oldMap.get(usr.Id).IsActive == true)
if(Trigger.newMap.get(usr.Id).IsActive == false)
userids.add(usr.Id);
}
List<Account> acctList=[SELECT ID FROM Account WHERE OwnerId IN :userids];
for(Account ac : acctList)
{
ac.OwnerId = System.Label.Owner_ID;
count++;
}
update acctList;
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setSubject('Owners changed for records ');
mail.setPlainTextBody('The Total number of records whose owner changed are :'+count);
mail.toAddresses = new String[] { 'Label.Email_addr_one'};
Messaging.SingleEmailMessage[] messages = new List<Messaging.SingleEmailMessage> {mail};
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);
}
}
Please try the following code:
trigger TransferInactiveUsers on User (after update) {
Integer count = 0;
List<ID> userids = new List<ID>();
For (User usr : Trigger.new){
if (Trigger.oldMap.get(usr.Id).IsActive == true)
if(Trigger.newMap.get(usr.Id).IsActive == false)
userids.add(usr.Id);
}
TransferInactiveUsers.TransferInactiveUsersHandler(userids);
}
Public class TransferInactiveUsers{
@future
public static void TransferInactiveUsersHandler(List<Id> userId){
Integer count = 0;
List<Account> acctList=[SELECT ID FROM Account WHERE OwnerId IN : userId];
list<Account> account_List = new list<Account>();
for(Account ac : acctList)
{
ac.OwnerId = '00590000001DtwV';
account_List.add(ac);
count++;
}
update account_List;
}
}
Explanation:
DML operations on certain sObjects, sometimes referred to as setup objects, can’t be mixed with DML on other sObjects in the same transaction. This restriction exists because some sObjects affect the user’s access to records in the org. You must insert or update these types of sObjects in a different transaction to prevent operations from happening with incorrect access-level permissions. For example, you can’t update an account and a user role in a single transaction. However, deleting a DML operation has no restrictions.
Please let me know if this solution works.
All Answers
Please try the following code:
trigger TransferInactiveUsers on User (after update) {
Integer count = 0;
List<ID> userids = new List<ID>();
For (User usr : Trigger.new){
if (Trigger.oldMap.get(usr.Id).IsActive == true)
if(Trigger.newMap.get(usr.Id).IsActive == false)
userids.add(usr.Id);
}
TransferInactiveUsers.TransferInactiveUsersHandler(userids);
}
Public class TransferInactiveUsers{
@future
public static void TransferInactiveUsersHandler(List<Id> userId){
Integer count = 0;
List<Account> acctList=[SELECT ID FROM Account WHERE OwnerId IN : userId];
list<Account> account_List = new list<Account>();
for(Account ac : acctList)
{
ac.OwnerId = '00590000001DtwV';
account_List.add(ac);
count++;
}
update account_List;
}
}
Explanation:
DML operations on certain sObjects, sometimes referred to as setup objects, can’t be mixed with DML on other sObjects in the same transaction. This restriction exists because some sObjects affect the user’s access to records in the org. You must insert or update these types of sObjects in a different transaction to prevent operations from happening with incorrect access-level permissions. For example, you can’t update an account and a user role in a single transaction. However, deleting a DML operation has no restrictions.
Please let me know if this solution works.