Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

Using the Web-Services SOAP API, I'd like to query for all UserRoles under the same Parent Role.

The only way I can think of doing this is by querying all Roles, then putting together a tree using the ParentRoleId field. Is there a better way?

share|improve this question
1  
I've done it that way from Apex before. Get all the User Roles, map them all out, then add each Role to the list for its Parent, Parent's Parent, etc. Most orgs don't have more than a couple hundred Roles, so it isn't too big an operation. –  Jeremy Nottingham Aug 27 '12 at 21:50

1 Answer 1

up vote 3 down vote accepted

Here is an alternative method based on (extracted from) Find My Salesforce Users by Role Hierarchy by Jeff Douglas.

Is it better than your proposed solution? Probably not. It will depend on how many user roles there are and how deep the tree is. If you have a large number of UserRoles with very few levels of nesting it may be more efficient to make a couple of SOQL calls rather than retrieving all the roles and then rebuilding the entire tree.

private static Set<ID> getAllSubRoleIds(Set<ID> roleIds) {

    Set<ID> currentRoleIds = new Set<ID>();

    // get all of the roles underneath the passed roles
    for(UserRole userRole :[select Id from UserRole where ParentRoleId 
         IN :roleIds AND ParentRoleID != null]) {
        currentRoleIds.add(userRole.Id);
    }

    // go fetch some more rolls!
    if(currentRoleIds.size() > 0) {
        currentRoleIds.addAll(getAllSubRoleIds(currentRoleIds));
    }

    return currentRoleIds;
}

And then you would get all the sub UserRoles for the parent UserRoleId with:

Set<Id> allSubRoleIds = getAllSubRoleIds(new Set<ID>{parentUserRoleId});

Update

Just reread the question and picked up that you are using the Web Services API. You could still use exactly the same approach, but send the SOQL queries via SOAP rather than Apex.

It would pay to do some real world measurements about which performs best in your environment considering:

  • Can you cache the data?
  • Are you only ever interested in a subset of the parent Role Ids?
  • How many levels of nesting there are in the hierarchy?
  • How many UserRoles there are?

If you are only every interested in a subset of parent Ids and the nesting level is fairly shallow I'd look at just caching the values of interest and pulling them down with a few recursive calls. Otherwise, you will probably be better pulling all the possible data down in a query with possibly a follow up queryMore.

share|improve this answer
1  
Thanks for the reference. I'd like to stay away from using an SOQL statement in a recursive loop, though. Right now, there are less than 300 roles in the organization. I may just use the method I mentioned. –  Matt K Aug 28 '12 at 15:30
    
@MatthewKeefe I agree. With less than 300 roles your existing method will serve you better. Worst case you would need two web service calls to pull all the possible values. –  Daniel Ballinger Aug 28 '12 at 19:58
    
Sounds like a good approach--but I would create an APEX Web Service class that returns the list of users in a Role using the above code. Then, call that Class via WebService call. –  DaveS Jan 7 '13 at 19:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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