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
Jean Velonis 21Jean Velonis 21 

Soql statement to see if a user has leads in a status

I'm trying to limit the user when they use a botton to only be able to have one record if they don't have any other records in the status of "New". 


global with sharing class RetrieveNextUtils {
    webService static Id retrieveNextCase(String userId)
    {
        //Here I'm looking to see if the current user has any leads in Lead Status containing New
        
        
        //Really we're only specifying the user ID for the sake of the test methods
        if (userId=='') {
            //Use the currently running user
            userId = UserInfo.getUserId();
        }
        
        //First find out which queues this user is a member of
        List<Id> listGroupIds = getQueuesForUser(userId);
        
        if(listGroupIds.size()>0) 
        {
            //Find an open case that is assigned to one of those queues
            Case caseObj = [select c.Id,c.OwnerId from Case c where 
                                                        c.IsClosed=false 
                                                        and c.OwnerId in :listGroupIds 
                                                        limit 1 
                                                        for update];
                                                
            if (caseObj!=null) {        
                //If we found one, assign it to the current user
                caseObj.OwnerId = userId;
                update caseObj;
                
                return caseObj.Id;
            }
        }
        
        return null;
    }
    
    webService static Id retrieveNextLead(String userId)
    {
        //Really we're only specifying the user ID for the sake of the test methods
        if (userId=='') {
            //Use the currently running user
            userId = UserInfo.getUserId();
        }
        
        //First find out which queues this user is a member of
        List<Id> listGroupIds = getQueuesForUser(userId);
        
        if(listGroupIds.size()>0) 
        {
        
            
            //Find an open lead that is assigned to one of those queues
            List<Lead> leads = [select l.Id,l.OwnerId from Lead l where 
                                                        l.IsConverted=false 
                                                        and l.OwnerId in :listGroupIds 
                                                        limit 1 
                                                        for update];
                                                
            if (leads.size()>0) {       
                //If we found one, assign it to the current user
                leads[0].OwnerId = userId;
                update leads;
                
                return leads[0].Id;
            }
        }
        
        return null;
    }
    
    //Returns a list of ids of queues that this user is a member of
    public static List<Id> getQueuesForUser(String userId) 
    {
        List<Id> listGroupIds = new List<Id>();
        List<GroupMember> listGroupMembers = [Select g.GroupId From GroupMember g 
                                                where g.Group.Type='Queue'
                                                and g.UserOrGroupId=:userId];
                                                
        if (listGroupMembers!=null && listGroupMembers.size()>0) {      
            for (GroupMember gm:listGroupMembers) {
                listGroupIds.add(gm.GroupId);
            }
        }
        
        return listGroupIds;
    }
    
    
        
   
}
Best Answer chosen by Jean Velonis 21
Daniel BallingerDaniel Ballinger

Try something like the following:

webService static Id retrieveNextLead(String userId) {
    //Really we're only specifying the user ID for the sake of the test methods
    if (userId=='') {
        //Use the currently running user
        userId = UserInfo.getUserId();
    }
        
    List<Lead> existingUserNewLeads = [Select Id, Name, Status, IsConverted 
                                       From Lead 
                                       where 
                                           OwnerID = :userId 
                                           and IsConverted = false 
                                           and Status = 'New'];
    if(!existingUserNewLeads.isEmpty()) {
        // This user already has a Lead with Status New assigned to them
        // Don't let them be greedy and get another lead until they are finished with their current one.
        // No more leads for you!
        return null;
    }

    //First find out which queues this user is a member of
    List<Id> listGroupIds = getQueuesForUser(userId);
    // ... Existing method code
    return null;
}

All Answers

Daniel BallingerDaniel Ballinger

Try something like the following:

webService static Id retrieveNextLead(String userId) {
    //Really we're only specifying the user ID for the sake of the test methods
    if (userId=='') {
        //Use the currently running user
        userId = UserInfo.getUserId();
    }
        
    List<Lead> existingUserNewLeads = [Select Id, Name, Status, IsConverted 
                                       From Lead 
                                       where 
                                           OwnerID = :userId 
                                           and IsConverted = false 
                                           and Status = 'New'];
    if(!existingUserNewLeads.isEmpty()) {
        // This user already has a Lead with Status New assigned to them
        // Don't let them be greedy and get another lead until they are finished with their current one.
        // No more leads for you!
        return null;
    }

    //First find out which queues this user is a member of
    List<Id> listGroupIds = getQueuesForUser(userId);
    // ... Existing method code
    return null;
}
This was selected as the best answer
Jean Velonis 21Jean Velonis 21
Thank you Daniel!