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
Brendan MBrendan M 

get queue names from Schema, GetDescribe, Describe or alternative method

Looking to save SOQL queries in various apex classes. Just wondering if its possible to use a schema or describe method instead of an SOQL query to retrieve them?

Really appreciate any information, help or sample solution.  

Code sample below where I would like to replace the SOQL if possible: 

 

private static Map<String,ID> getQueuesByName(){
        Map<String,ID> queuesByName = new Map<String,ID>();
        
        Set<String> queueNameSet = new Set<String>{Constants.QUEUE_VEHICLES,Constants.QUEUE_GENERAL,
                                                    Constants.QUEUE_CMT_RECONCILIATIONS,Constants.QUEUE_CMT_RECOVERIES,
                                                    Constants.QUEUE_SFLEET_ADMIN,Constants.QUEUE_SFLEET_ACC_MGMT};
        
        List<Group> queues = [
            SELECT 
                Id,DeveloperName 
            FROM 
                Group 
            WHERE 
            Type = 'Queue' AND DeveloperName IN :queueNameSet
        ];

        if (!queues.isEmpty()){
            for(Group queue : queues ) {
                queuesByName.put(queue.DeveloperName, queue.Id);
            }
        }

        return queuesByName;
    }
Best Answer chosen by Brendan M
Prateek Prasoon 25Prateek Prasoon 25
Hey Brendan,

Yes, you can use the Schema or Describe methods to retrieve information about SObjects and fields, instead of using an SOQL query. This is known as dynamic Apex.

 
private static Map<String,ID> getQueuesByName() {
    Map<String,ID> queuesByName = new Map<String,ID>();
    
    Set<String> queueNameSet = new Set<String>{Constants.QUEUE_VEHICLES,Constants.QUEUE_GENERAL,
                                                Constants.QUEUE_CMT_RECONCILIATIONS,Constants.QUEUE_CMT_RECOVERIES,
                                                Constants.QUEUE_SFLEET_ADMIN,Constants.QUEUE_SFLEET_ACC_MGMT};
    
    // Get the SObject describe for Group
    Schema.DescribeSObjectResult groupDescribe = Schema.SObjectType.Group;
    
    // Get the describe result for the DeveloperName field
    Schema.DescribeFieldResult developerNameField = groupDescribe.fields.getMap().get('DeveloperName').getDescribe();
    
    // Get the picklist values for the DeveloperName field
    List<Schema.PicklistEntry> picklistValues = developerNameField.getPicklistValues();
    
    // Filter the picklist values to the ones in the queueNameSet
    List<String> filteredValues = new List<String>();
    for (Schema.PicklistEntry entry : picklistValues) {
        if (queueNameSet.contains(entry.getLabel())) {
            filteredValues.add(entry.getValue());
        }
    }
    
    // Query for the queues with the filtered DeveloperNames
    List<Group> queues = [
        SELECT Id, DeveloperName
        FROM Group
        WHERE Type = 'Queue' AND DeveloperName IN :filteredValues
    ];
    
    if (!queues.isEmpty()) {
        for (Group queue : queues) {
            queuesByName.put(queue.DeveloperName, queue.Id);
        }
    }
    
    return queuesByName;
}



If you find my answer helpful, please mark it as the best answer.

All Answers

Prateek Prasoon 25Prateek Prasoon 25
Hey Brendan,

Yes, you can use the Schema or Describe methods to retrieve information about SObjects and fields, instead of using an SOQL query. This is known as dynamic Apex.

 
private static Map<String,ID> getQueuesByName() {
    Map<String,ID> queuesByName = new Map<String,ID>();
    
    Set<String> queueNameSet = new Set<String>{Constants.QUEUE_VEHICLES,Constants.QUEUE_GENERAL,
                                                Constants.QUEUE_CMT_RECONCILIATIONS,Constants.QUEUE_CMT_RECOVERIES,
                                                Constants.QUEUE_SFLEET_ADMIN,Constants.QUEUE_SFLEET_ACC_MGMT};
    
    // Get the SObject describe for Group
    Schema.DescribeSObjectResult groupDescribe = Schema.SObjectType.Group;
    
    // Get the describe result for the DeveloperName field
    Schema.DescribeFieldResult developerNameField = groupDescribe.fields.getMap().get('DeveloperName').getDescribe();
    
    // Get the picklist values for the DeveloperName field
    List<Schema.PicklistEntry> picklistValues = developerNameField.getPicklistValues();
    
    // Filter the picklist values to the ones in the queueNameSet
    List<String> filteredValues = new List<String>();
    for (Schema.PicklistEntry entry : picklistValues) {
        if (queueNameSet.contains(entry.getLabel())) {
            filteredValues.add(entry.getValue());
        }
    }
    
    // Query for the queues with the filtered DeveloperNames
    List<Group> queues = [
        SELECT Id, DeveloperName
        FROM Group
        WHERE Type = 'Queue' AND DeveloperName IN :filteredValues
    ];
    
    if (!queues.isEmpty()) {
        for (Group queue : queues) {
            queuesByName.put(queue.DeveloperName, queue.Id);
        }
    }
    
    return queuesByName;
}



If you find my answer helpful, please mark it as the best answer.
This was selected as the best answer
Brendan MBrendan M
Thank you so much Prateek! Such a helpful answer :)