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 Grey 10Jean Grey 10 

access user roles through helper class

I wrote the following class that I intend to use as a 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);
    }
}

In another class I have:

        Date currentDate = System.today();
        Date minusonemonth = currentDate.addMonths(-1);
        Date plusoneyear = currentDate.addYears(1);
        Date plusthreemonths = currentDate.addDays(90);
        RoleUtils roleUtil = new RoleUtils();

        getCon = [SELECT RecordTypeId, Contract_Number__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, Annual_Contract_Volume__c
                FROM NG_Contract__c
                WHERE Contract_End_Date__c >= :minusonemonth
                AND Contract_End_Date__c <= :plusthreemonths
                ORDER BY Contract_End_Date__c ASC
                LIMIT 10];

I would like to add a filter to my list to choose records owned by one of the users in my usergroup variable from my helper class. How do I do this?
Best Answer chosen by Jean Grey 10
Mohit Sharma 37Mohit Sharma 37
Hi Jean Grey 10,
 
You have almost done, only need to fetch the data from the RoleUtils class.
Everything is static so you have no need to create the object of RoleUtils class you fetch is directly and use usergroup in your query.
Date currentDate = System.today();
Date minusonemonth = currentDate.addMonths(-1);
Date plusoneyear = currentDate.addYears(1);
Date plusthreemonths = currentDate.addDays(90);

// No need to create an object because it is static method
RoleUtils.getIds();

//Initialize getCon list if you have not initialize it.
getCon = [SELECT RecordTypeId, Contract_Number__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, Annual_Contract_Volume__c
          FROM NG_Contract__c
          WHERE Contract_End_Date__c >= :minusonemonth
          AND Contract_End_Date__c <= :plusthreemonths
          AND OwnerId IN :RoleUtils.usergroup
          ORDER BY Contract_End_Date__c ASC
          LIMIT 10];

Please mark it as best answer if it works accordingly. :)

Have a great day!!