0

I am trying to simplify my code by using a helper class to search users for a query. I have the following helper class:

public class RoleUtils {

    public static ID userId {get;set;} //variable for this user id
    public static ID userRoleId {get;set;} //variable for this user role id
    public static Set<ID> userSubs {get;set;} //variable for all subordinates to this user in role hierarchy
    public static Set<ID> userSubRoles {get;set;}
    public static Set<ID> userPar {get;set;} //variable for all parents to this user in role hierarchy
    public static Set<ID> usergroup {get;set;}

    public static void getIds(){
        usergroup = new Set<ID>();
        userId = UserInfo.getUserId();
        usergroup.add(userId);
        userRoleId = UserInfo.getUserRoleId();
        userSubRoles = new Set<ID>();

        for(UserRole subRole :[select Id from UserRole where ParentRoleId = :userRoleId AND ParentRoleID != null])
        {
            userSubRoles.add(subRole.Id);
        }
        for(User subuser :[SELECT ID, UserRoleId FROM User WHERE UserRoleId IN :userSubRoles])
        {
            usergroup.add(subuser.Id);
        }


        system.debug('userId '+userId);
        system.debug('userRoleId '+userRoleId);
        system.debug('userSubRoles '+userSubRoles);
        system.debug('usergroup '+usergroup);
    }
}

I've tested this and it works fine.

In another class I have the following:

public void myDash(){
    Date currentDate = System.today();
    Date minusonemonth = currentDate.addMonths(-1);
    Date plusoneyear = currentDate.addYears(1);
    Date plusthreemonths = currentDate.addDays(90);
    RoleUtils.getIds();

    getCon = [SELECT RecordTypeId, Contract_Number__c, OwnerId, Annual_Contract_Volume__c, Owned_by_me__c, Account__c, Account__r.Name, Id, Name, Utility__c, Contract_Nom_Group__c, Supplier__c, Consultant__c, Contract_End_Date__c, Pro_forma_Annual_Margin__c
            FROM NG_Contract__c
            WHERE Contract_End_Date__c >= :minusonemonth
            AND Contract_End_Date__c <= :plusthreemonths
            AND Annual_Contract_Volume__c > 0
//          AND OwnerId IN :usergroup
            ORDER BY Contract_End_Date__c ASC
            LIMIT 10];

How do I call the usergroup variable from my helper class to modify my getCon query to search for records owned by a user in that set?

  • you can combine the two queries into one BTW. ...WHERE UserRoleID IN (Select id from UserRole where ....) – cropredy Jul 20 '17 at 4:48
3

You just reference it statically similar to what you did with getIds():

AND OwnerId IN :RoleUtils.userGroup

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.