2

I am looking for a way to get the list of all the queue a user is subscribe to. Now I know this question as already answered, using the GroupMember table, but the problem is a user can be a member of a queue because he's role is parent of an other role that is on the queue which make is way more difficult to do it using SOQL and respect the governor limits.

Any idea/suggestion?

1

This may or may not work for you but I wrote this bit of code to find the Heirarchy of the account object. This could be modified to use the UserRole object and get all the roles that the User is above in the Hierarchy. Then you could get a list of all uses with those roles and then get the groups they belong to.

For example

A

-----b

-----------b1

-----c

-----------c1

---------------c1.2

-----d

So if I start at b1, it will return all accounts up to A and back down thus including c1.2 as well.

Maybe someone will find it useful. You could refactor it to work with roles or only go up 1 level to find parent and siblings, etc...

Account theAccount = //Your SOQL Here
Set<ID> AllParents = new Set<ID>();
Boolean allDone;

AllParents.add(theAccount.ID);
      //If the account we are starting at has a Parent ID, add it
      //to the set of IDs
      if(theAccount.ParentID != Null){
        AllParents.add(theAccount.ParentID);
      }

      //Main loop to traverse through the Hierarchy
      do{
         //Set the flag to indicate loop should stop
         allDone = true;
        //Get a list of accounts with IDs or Parent IDs in the AllParents Set
        //This will produce 1 SOQL for each level in the Hierachy 
        //Max is then 99 levels deep which I believe no one will  have
        //You could add a check though
        for(Account a : [Select ID, ParentID From Account Where ID IN :AllParents OR ParentID IN :AllParents]){

          if(!AllParents.contains(a.ID) || ( !AllParents.contains(a.parentID) && a.parentID != null)){

            if(a.parentID != null)
              AllParents.add(a.parentID);

            AllParents.add(a.id);
            //Reset flag to find more parents / children
            allDone = false;

          }

        }

      } while(allDone == false); 
3
  • 1
    Pretty dangerous to hit the governor limits... Feb 24 '14 at 16:38
  • Agreed that is a possibility and that is why you add code to check and ensure you do not. If you know of a better way to do it please post up your thoughts. This has been a need that has not been meet given the current API's and until there is a way to do it this is what we are stuck with.
    – Eric
    Feb 25 '14 at 3:24
  • You saved me Eric....It worked as I needed it to.... Bunch of thanks to you
    – user10139
    Jun 26 '14 at 15:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.