You need to sign in to do that
Don't have an account?
Siva Admin
Help in the Test Class
Hi all I'm a newbee to SF, trying to get some hand on Test Classes. Have been trying to write a test class for it, but cannot.
trigger RecordOwnerChange on user (after update){
if (trigger.isafter & trigger.isupdate){
List<Id> recordIds = new List<Id>();
List<account> aclst = [select id, ownerid, owner.isActive, owner.Managerid from Account where OwnerId IN:trigger.new];
for (Account ac: aclst){
if (ac.owner.isActive == false && ac.owner.managerid != null){
recordIds.add(ac.id);
}
}
if(!recordIds.isEmpty()){
RecordOwnerChange_4Handler.updateaccounts(recordIds);
}
CLASS:
public class RecordOwnerChangeHandler {
@future
public static void updateaccounts(List<Id> recordIds){
List<Account> updateacclist = new List<Account>();
for (Account ac:[select ownerid, owner.Managerid from Account where Id IN:recordIds]){
ac.ownerid= ac.owner.managerid;
updateacclist.add(ac);
}
if(updateacclist.size()>0){
update updateacclist ;
}}}
kindly help me on this regards. Thanks.
trigger RecordOwnerChange on user (after update){
if (trigger.isafter & trigger.isupdate){
List<Id> recordIds = new List<Id>();
List<account> aclst = [select id, ownerid, owner.isActive, owner.Managerid from Account where OwnerId IN:trigger.new];
for (Account ac: aclst){
if (ac.owner.isActive == false && ac.owner.managerid != null){
recordIds.add(ac.id);
}
}
if(!recordIds.isEmpty()){
RecordOwnerChange_4Handler.updateaccounts(recordIds);
}
CLASS:
public class RecordOwnerChangeHandler {
@future
public static void updateaccounts(List<Id> recordIds){
List<Account> updateacclist = new List<Account>();
for (Account ac:[select ownerid, owner.Managerid from Account where Id IN:recordIds]){
ac.ownerid= ac.owner.managerid;
updateacclist.add(ac);
}
if(updateacclist.size()>0){
update updateacclist ;
}}}
kindly help me on this regards. Thanks.
With normal trigger, getting below error:
"Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger RecordOwnerChange caused an unexpected exception, contact your administrator: RecordOwnerChange: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 0012800000k4GFLAA2; first error: MIXED_DML_OPERATION, DML operation on setup object is not permitted after you have updated a non-setup object (or vice versa): Account, original object: User: []: Trigger.RecordOwnerChange: line 18, column 1"
with future method, it's working fine.