You need to sign in to do that
Don't have an account?

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;
}
}
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;
}
}
Try something like the following:
All Answers
Try something like the following: