You need to sign in to do that
Don't have an account?
Febe Nagy
low code coverage help - account owner changed test
Hi,
Can someone help, I have a low code coverage and it is because I can't get the test class to check if the account owner is the same as the old account owner. Can someone tell me how can I do this when I'm only using the account object? Thank you for your help.
Here is my code trigger and test
Can someone help, I have a low code coverage and it is because I can't get the test class to check if the account owner is the same as the old account owner. Can someone tell me how can I do this when I'm only using the account object? Thank you for your help.
trigger AccOwnerChange on Account (before update) { if(ObjectHelper.isAccOwnerChangeExecuted){ return; } ObjectHelper.isAccOwnerChangeExecuted = true; Set<Id> setUserIds = new Set<Id>(); Map<Id, User> mapUserObj = new Map<Id, User>(); for(Account a : Trigger.new) { Account oldObj = Trigger.oldmap.get(a.id); if(a.ownerId != oldObj.ownerId){ setUserIds.add(a.OwnerId); setUserIds.add(oldObj.ownerId); } } system.debug('test ###' + setUserIds); if(setUserIds.size()>0){ List<User> lstUser = [select Id, Name, ManagerId from User where Id in :setUserIds AND isActive = true]; for(User usrObj : lstUser){ mapUserObj.put(usrObj.Id, usrObj); } } system.debug('test ###' + mapUserObj); for(Account a : Trigger.new) { Account oldObj = Trigger.oldmap.get(a.id); if(a.ownerId != oldObj.ownerId){ a.Previous_Account_Owner__c = oldObj.ownerId; if(mapUserObj.get(a.Previous_Account_Owner__c)!=null){ a.Previous_Account_Owner_s_Manager__c = mapUserObj.get(a.Previous_Account_Owner__c).ManagerId; } if(mapUserObj.get(a.ownerId) != null){ a.New_Account_Owner_s_Manager__c = mapUserObj.get(a.ownerId).ManagerId; } } } ********************************************************************************************** Test class: @isTest private class testAccOwner { public static testMethod void testAccOwnerChange() { test.startTest(); User testUser2 = [Select Id, ManagerId from User where IsActive = true limit 1]; User testUser3 = [Select Id, ManagerId from User where IsActive = true limit 1]; User testUser1 = new User(alias = 'TstUsr1',Firstname='tst1', email='newuserDP@testorg.com', emailencodingkey='UTF-8', lastname='Tst11', languagelocalekey='en_US', localesidkey='en_US', profileid = 'xxxx', timezonesidkey='America/Los_Angeles', username='newuser11@testorg.com', UserRoleId = 'xxxx',Country_picklist__c = 'UNITED STATES', Global_Region__c = 'US', DIRECTOR_NM__c=testUser2.id, ManagerId = testUser3.Id); insert testUser1; System.runAs(testUser1){ Account acc = new Account(Name = 'Febe01', OwnerId = testUser2.Id); insert acc; acc.OwnerId = testUser1.Id; //acc.Name = 'tester01'; //acc.OwnerId = 'xxxx'; //acc.Previous_Account_Owner__c = testUser2.Id; //acc.Previous_Account_Owner_s_Manager__c = testUser1.ManagerId; //acc.New_Account_Owner_s_Manager__c = testUser2.ManagerId; update acc; testUser1.Id = testUser3.Id; acc.OwnerId = testUser1.Id; update acc; update testUser1; } test.stopTest(); } }
Here is my code trigger and test
I think this is because of these lines
testUser1.Id = testUser3.Id;
acc.OwnerId = testUser1.Id;
update acc;
update testUser1;
Why you are updating testUser1 that too Id, you cannot update Id of any record.
So try removing these lines and run the test class again
testUser1.Id = testUser3.Id;
acc.OwnerId = testUser1.Id;
update acc;
update testUser1;
Regards,
Bhanu Mahesh
line 14. if(a.ownerId != oldObj.ownerId) (how to test this?)
line 21. if(setUserIds.size()>0) (I have 3 users so I thought that would covered this line but it doesn't, any suggestions?)
Thank you so much!!