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
K SrikanthK Srikanth 

Round robin test class

Can any one help test class for below trigger.


trigger AssignLeadsToQueue on Lead (before insert, before update){
    
  
  if (trigger.isBefore && (trigger.isInsert || trigger.isUpdate)){

      
        List<Group> queues = [
                SELECT Id,
                    (Select Id, UserOrGroupId FROM GroupMembers Order By ID ASC)
                FROM Group
                Where Type = 'Queue'AND Name = 'High-Priority Leads'
        ];
   
        // Get the index of the last lead assigned user in the queue
        Lead_Round_Robin_Assignment__c lrr = Lead_Round_Robin_Assignment__c.getOrgDefaults();
        Integer userIndex = (lrr.get('User_Index__c') == null || Integer.valueOf(lrr.get('User_Index__c')) < -1) 
            ? -1 : Integer.valueOf(lrr.get('User_Index__c'));

        if (queues.size() > 0 && queues.get(0).GroupMembers.size() > 0) {
     
            Id queueId = queues.get(0).Id;
            Integer groupMemberSize = queues.get(0).GroupMembers.size();
            for (Lead l : Trigger.new) {
                if ((l.LeadSource =='Moengage'||l.LeadSource =='Facebook Ads'|') && l.Status== 'New'&&l.CreatedById=='********') {
                   system.debug('inside it is coming');
                    Integer leadUserIndex =  (userIndex + 1) >= groupMemberSize ? 0 : userIndex + 1;
                    l.OwnerId = queues.get(0).GroupMembers.get(leadUserIndex).UserOrGroupId;
                    userIndex = leadUserIndex;
                }
            }

            // Update the custom settings user index with the last lead assigned user
            lrr.User_Index__c = userIndex;
            update lrr;
        }
    }

}
SwethaSwetha (Salesforce Developers) 
HI Srikanth,
The below articles give a good insight into how coverage can be improved
 
https://salesforce.stackexchange.com/questions/244794/how-do-i-increase-my-code-coverage-or-why-cant-i-cover-these-lines 
 
Example test class code snippet which runs to create the queue
 
@IsTest 
public class TestForQueue {
    static testMethod void validateTestForQueue() {
        //Creating Group
        Group testGroup = new Group(Name='QUEUE NAME', Type='Queue');
        insert testGroup;
        
        //Creating QUEUE
        System.runAs(new User(Id=UserInfo.getUserId()))
        {
            //Associating queue with group AND to the Case object
            QueuesObject testQueue = new QueueSObject(QueueID = testGroup.id, SObjectType = 'Case');
            insert testQueue;
        }
        
        //QUERYing to check -- Test Case 1
        QueueSobject q1 = [Select Id,q.Queue.Name,q.Queue.ID from QueueSobject q ORDER BY q.Queue.Name] ;
        System.debug(q1.Queue.Name);
        
        //Updating Queue Name
        testGroup.Name = 'DIFFERENT QUEUE NAME' ;
        update testGroup;
        
        //QUERYing to check -- Test Case 2
        QueueSobject q2 = [Select Id,q.Queue.Name,q.Queue.ID from QueueSobject q ORDER BY q.Queue.Name] ;
        System.debug(q2.Queue.Name); 
    }
}

Ref: https://pradeep-dani.blogspot.com/2016/02/create-queue-in-sfdc-apex-test-class.html

Related: https://salesforce.stackexchange.com/questions/33455/assign-a-case-to-a-queue-in-apex-test-class

If this information helps, please mark the answer as best. Thank you