You need to sign in to do that
Don't have an account?
Siva Admin
Test Class No coverage Help
HI am a newbee to SF. Kindly suggest what's wrong with the below test class.
@IsTest
public class testclassownerassignmanager{
static testmethod void metest(){
Profile pro = [SELECT Id FROM Profile WHERE Name='Standard User'];
User testUserA = new User(
Alias = 'standard', Email='standarduser@testorg.com',
EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = pro.Id,
TimeZoneSidKey='America/Los_Angeles',
UserName='testUserA@testorganise.com');
insert testUserA ;
User testUserB = new User(
Alias = 'standard', Email='standarduser@testorg.com',
EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = pro.Id,
TimeZoneSidKey='America/Los_Angeles',
UserName='testUserB@testorganise.com');
testUserB.ManagerID = testUserA.id;
insert testUserB;
List<Account> acclist= new List<Account>();
for(Integer i=0;i<=10;i++){
account a = new account(Name='Test' + i, ownerid=testUserB.id);
acclist.add(a);
List<Account> TobUpdateAcclist= new List<Account>();
for(Account ac: acclist){
if (ac.owner.isactive == false )
{
ac.ownerid=testUserB.managerid;
TobUpdateAcclist.add(a);
}
}
test.starttest();
if (Test.isRunningTest()) {
System.runAs(new User(Id = Userinfo.getUserId())) {
update TobUpdateAcclist;
}
} else {
update TobUpdateAcclist;
}
test.stoptest();
List<Account> updatedaccs = [select id,name from Account where ID IN: TobUpdateAcclist];
system.assertEquals(a.owner.Managerid, a.ownerid);
}}}
@IsTest
public class testclassownerassignmanager{
static testmethod void metest(){
Profile pro = [SELECT Id FROM Profile WHERE Name='Standard User'];
User testUserA = new User(
Alias = 'standard', Email='standarduser@testorg.com',
EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = pro.Id,
TimeZoneSidKey='America/Los_Angeles',
UserName='testUserA@testorganise.com');
insert testUserA ;
User testUserB = new User(
Alias = 'standard', Email='standarduser@testorg.com',
EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = pro.Id,
TimeZoneSidKey='America/Los_Angeles',
UserName='testUserB@testorganise.com');
testUserB.ManagerID = testUserA.id;
insert testUserB;
List<Account> acclist= new List<Account>();
for(Integer i=0;i<=10;i++){
account a = new account(Name='Test' + i, ownerid=testUserB.id);
acclist.add(a);
List<Account> TobUpdateAcclist= new List<Account>();
for(Account ac: acclist){
if (ac.owner.isactive == false )
{
ac.ownerid=testUserB.managerid;
TobUpdateAcclist.add(a);
}
}
test.starttest();
if (Test.isRunningTest()) {
System.runAs(new User(Id = Userinfo.getUserId())) {
update TobUpdateAcclist;
}
} else {
update TobUpdateAcclist;
}
test.stoptest();
List<Account> updatedaccs = [select id,name from Account where ID IN: TobUpdateAcclist];
system.assertEquals(a.owner.Managerid, a.ownerid);
}}}
Please find below Test class for your Trigger and Handler class.
Let us know if that helps you.
Best Regards,
BALAJI
All Answers
What issue you have been faced in above code?
Thanks!
Force Techie
What is the error message you receive when trying to test this class? Also, if there is a trigger that goes along with it, may you post that as well?
Thank you,
Esther Onema
public class AccountHandler {
@future
public static void updateAccounts(List<ID> recordIds) {
List<Account> accts = [SELECT ownerid,owner.ManagerId FROM Account WHERE Id IN :recordIds];
List<Account> accts1 =new List<Account>();
for (Account acc:accts ){
acc.ownerid = acc.owner.ManagerId;
accts1.add(acc);
}
update accts1 ;
}
}
Hi Siva,
Kindly post your trigger to get a better help.
Best Regrads,
BALAJI
trigger RecordOwnerChangeEx on User (after update) {
list<account> lstAcc=new list<account>();
List<ID> recordIds=new List<ID> ();
for(Account acc : [select id, ownerid, owner.isActive, owner.ManagerId from account where ownerId IN: trigger.new]){
if(acc.owner.isActive == false && acc.owner.ManagerId != null){
//acc.ownerid = acc.owner.ManagerId;
lstAcc.add(acc);
recordIds.add(acc.id);
}
}
if(!lstAcc.isEmpty()){
AccountHandler.updateAccounts(recordIds);
}
}
Please find below Test class for your Trigger and Handler class.
Let us know if that helps you.
Best Regards,
BALAJI
Can you please try to change the test clas name from 'testclassownerassignmanager' to some other like 'classownerassignmanager_test'.