function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Harjeet Singh 28Harjeet Singh 28 

Help for test class coverage

Hi All,

I have developed an apex class whcih basically assign users to queue dynamically.I have a VF page which will launch from a custom VF tab.This VF page shows few users and with the checkbox.When checkbox against the username is marked and hit Save button then seleected users will go to the queue defined in class.
Below is my clas:
public class TodayTelesalesDetails {

public Date Today { 
    get { 
        return Date.today(); 
    }
}
public List<wrapAgent> wrapAgentList{get; set;}
public List<User> selectedAgents{get;set;}

public TodayTelesalesDetails (){
   
    if(wrapAgentList == null) {
        wrapAgentList = new List<wrapAgent>();

        for(User teleSalesAgent: [select Id, Name, Email from User where Name IN('Steve Waugh','Yuvraj Singh')]) {
            // As each agent is processed we create a new wrap object and add it to the wrapAgentList
            wrapAgentList.add(new wrapAgent(teleSalesAgent));
    }
}
}

public PageReference doFullSave(){

    set<id> userSetId = new set<id>();
    list<GroupMember> gmm = new list<GroupMember>();
    List<GroupMember> toBeDeleted= [SELECT Id, GroupId, UserOrGroupId FROM GroupMember where GroupId = '00G9D000000toPYUAY'];
    delete toBeDeleted;
    List<User> userlist=[select Id, Name from User where Name IN('Steve Waugh','Yuvraj Singh')];
   
    for(User u : userlist)
    { 
    system.debug('message3>>'+u);
         for(wrapAgent wrapAccountObj : wrapAgentList) {
             
            if(wrapAccountObj.selected == true) {
                userSetId.add(wrapAccountObj.teleAgent.id);
                
            }
        }

    }
      
    for(User us : [select id from User where id =: userSetId])
    {
       
        for(Group gg : [select id,name from Group where type = 'Queue' and Name = 'TeleSales Queue'])
        {
            GroupMember gm = new GroupMember();
            gm.groupId = gg.id;
            gm.UserOrGroupId = us.Id;
            gmm.add(gm);
        }
        
    }
            
     
    insert gmm;
    PageReference pageRef = new PageReference('/');
            pageRef.setRedirect(true);
            return pageRef;
    }
   
   
    
 public PageReference doCancel(){
         
         PageReference pageRef = new PageReference('/');
            pageRef.setRedirect(true);
            return pageRef;
    }


 public class wrapAgent{
    public User teleAgent {get; set;}//Please put the custom metadata names
    public Boolean selected {get; set;}


    //This is the contructor method. When we create a new object we pass a telesales metadata member that is set to the acc property. We also set the selected value to false
    public wrapAgent(User teleSalesAgent) {
        teleAgent = teleSalesAgent;
        selected = false;

    }
}
}
and below is my test class:
@isTest
public class Test_TodayTeleSalesDetails {

    public static testMethod void validateusersinqueue() {
    Group testGroup = new Group(Name='test group', Type='Queue');
	insert testGroup;

	System.runAs(new User(Id=UserInfo.getUserId()))
	{
    QueuesObject testQueue = new QueueSObject(QueueID = testGroup.id, SObjectType = 'Lead');
    insert testQueue;
	}
   	Test.startTest();
     TodayTelesalesDetails telesalesDetails=new TodayTelesalesDetails();
       telesalesDetails.doFullSave();
        telesalesDetails.doCancel();
    Test.stopTest();
}
}

I am not able to cover below lines of code:
for(User us : [select id from User where id =: userSetId])
    {
        system.debug('message6>>'+us);
        for(Group gg : [select id,name from Group where type = 'Queue' and Name = 'TeleSales Queue'])
        {
            GroupMember gm = new GroupMember();
            gm.groupId = gg.id;
            gm.UserOrGroupId = us.Id;
            gmm.add(gm);
        }
        
    }
Below is the screenshot for the same:
Lines of Code is not covering
My current code coverage stands at 73%

Many I request to help me on same to get a code coverage above 75%

Many thanks in advance

Thanks & Regards,
Harjeet
Best Answer chosen by Harjeet Singh 28
Ajay K DubediAjay K Dubedi
Hi Harjeet,

If you want to keep "selected" of wrapAgent as false and want to increase your code coverage then you can use Test.isRunningtest() in your code. You can update your code as below:
 
for(User u : userlist)
        { 
            system.debug('message3>>'+u);
            for(wrapAgent wrapAccountObj : wrapAgentList) {
                if(!Test.isRunningTest()){                          
                    if(wrapAccountObj.selected == true) {
                        userSetId.add(wrapAccountObj.teleAgent.id);
                    }
                }else{
                    userSetId.add(wrapAccountObj.teleAgent.id);
                } 
                
            }
            
        }

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com

All Answers

umesh kantumesh kant
Great.! I Like this blog..!
the provided code is very helpful for my task. Thanks for sharing this info.

Thanks
Umsh Kant
Wismad.com
Best Web Development Company (https://www.wismad.com)
 
Ajay K DubediAjay K Dubedi
Hi Harjeet,

I have tried your code in my Org and found that the value of the 'selected' of wrapper named wrapAgent is always set to false and due to this 'userSetId' set is not updating with the userId which you are further using in your 'For loop'. 
You can set it to true as I did or you can dynamically set the 'true' or 'false' for different scenarios as per your requirement. Below code is giving 92% code coverage in my Org. 
Also, In the test class you need to set the value of name as 'TeleSales Queue' instead of 'test group' while inserting the Group.

Apex class : 
public class TodayTelesalesDetails {
    
    public Date Today { 
        get { 
            return Date.today(); 
        }
    }
    public List<wrapAgent> wrapAgentList{get; set;}
    public List<User> selectedAgents{get;set;}
    
    public TodayTelesalesDetails (){
        
        if(wrapAgentList == null) {
            wrapAgentList = new List<wrapAgent>();
            
            for(User teleSalesAgent: [select Id, Name, Email from User where Name IN('Steve Waugh','Yuvraj Singh')]) {
                // As each agent is processed we create a new wrap object and add it to the wrapAgentList
                wrapAgentList.add(new wrapAgent(teleSalesAgent));
            }
        }
    }
    
    public PageReference doFullSave(){
        
        set<id> userSetId = new set<id>();
        list<GroupMember> gmm = new list<GroupMember>();
        List<GroupMember> toBeDeleted= [SELECT Id, GroupId, UserOrGroupId FROM GroupMember where GroupId = '00G9D000000toPYUAY'];
        delete toBeDeleted;
        List<User> userlist=[select Id, Name from User where Name IN('Steve Waugh','Yuvraj Singh')];
        
        for(User u : userlist)
        { 
            system.debug('message3>>'+u);
            for(wrapAgent wrapAccountObj : wrapAgentList) {
                
                if(wrapAccountObj.selected == true) {
                    userSetId.add(wrapAccountObj.teleAgent.id);
                    
                }
            }
            
        }
        
        for(User us : [select id from User where id IN : userSetId])
        {
            
            for(Group gg : [select id,name from Group where type = 'Queue' and Name = 'TeleSales Queue'])
            {
                GroupMember gm = new GroupMember();
                gm.groupId = gg.id;
                gm.UserOrGroupId = us.Id;
                gmm.add(gm);
            }
            
        }
        
        
        insert gmm;
        PageReference pageRef = new PageReference('/');
        pageRef.setRedirect(true);
        return pageRef;
    }
    
    
    
    public PageReference doCancel(){
        
        PageReference pageRef = new PageReference('/');
        pageRef.setRedirect(true);
        return pageRef;
    }
    
    
    public class wrapAgent{
        public User teleAgent {get; set;}//Please put the custom metadata names
        public Boolean selected {get; set;}
        
        
        //This is the contructor method. When we create a new object we pass a telesales metadata member that is set to the acc property. We also set the selected value to false
        public wrapAgent(User teleSalesAgent) {
            teleAgent = teleSalesAgent;
            selected = true;   // assigning value true
         // selected = false;
        }
    }
}


============================================================
Test class :
@isTest
public class Test_TodayTeleSalesDetails {
    
    public static testMethod void validateusersinqueue() {
        Group testGroup = new Group(Name='TeleSales Queue', Type='Queue');   //give the name as TeleSales Queue
        insert testGroup;
        
        Profile p = [SELECT Id FROM Profile WHERE Name='Standard User']; 
        User u = new User(Alias = 'standt', 
                          Email='standarduser@testorg.com', 
                          EmailEncodingKey='UTF-8', 
                          FirstName='Steve ', 
                          LastName= 'Waugh',
                          LanguageLocaleKey='en_US', 
                          LocaleSidKey='en_US', 
                          ProfileId = p.Id, 
                          TimeZoneSidKey='America/Los_Angeles', 
                          UserName='YATest@test.com');
        Insert u;
        
        Test.startTest();
        TodayTelesalesDetails telesalesDetails=new TodayTelesalesDetails();
        telesalesDetails.doFullSave();
        telesalesDetails.doCancel();
        Test.stopTest();
    }
}



I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com
Harjeet Singh 28Harjeet Singh 28
Hi @Ajay thanks for your response. I will definitely give it a try and if it works will mark your suggestion as the best answer
Just a side note:I always read your blog on Salesforce. Keep up the good work. Inspires a lot many people for doing good work in Salesforce
Harjeet Singh 28Harjeet Singh 28
Hi Ajay,

I tried the code as suggested by you. There are 2 aspects
1. If I marked "selected" of wrapAgent as true and ran the test class I got test coverage of 92%. But when I checked at the frontend I found that all the checkboxes were by default checked.Ideally it should not be the case.We need to allow user to check the checkboxes
2. I kept "selected" of wrapAgent as false only and ran the test class still the test coverage stands at 73%

Kindly advise or provide inputs Isn't there a way we can have coverage of wrapper selected is false

Many thanks in advance
Ajay K DubediAjay K Dubedi
Hi Harjeet,

If you want to keep "selected" of wrapAgent as false and want to increase your code coverage then you can use Test.isRunningtest() in your code. You can update your code as below:
 
for(User u : userlist)
        { 
            system.debug('message3>>'+u);
            for(wrapAgent wrapAccountObj : wrapAgentList) {
                if(!Test.isRunningTest()){                          
                    if(wrapAccountObj.selected == true) {
                        userSetId.add(wrapAccountObj.teleAgent.id);
                    }
                }else{
                    userSetId.add(wrapAccountObj.teleAgent.id);
                } 
                
            }
            
        }

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com
This was selected as the best answer
zabee saqibizabee saqibi
hamada sayedhamada sayed
I am 100% sure you can not do this (callouts after DML) because, in the past, I tried to do it but failed every time. If you found a way to do so, please don't forget to share the method with all of us here on this thread. It will be a significant input from your side for this community as well. Thank you! Coin Master free spins (https://www.coinmasterfreespinss.com/)