1

I want to know the best optimized way to access current logged in user's information and set few flags. I have a controller extension and I have set flags in constructor like this:

User u = [SELECT ID,Name,ProfileID,Profile.Name,UserRole.Name,UserRole.ID,Team_Lead__c,Leading_Teams__c,Department,Desk__c
             FROM User 
             WHERE id=:userId];

if(u.UserRole.Name != null && u.UserRole.Name.contains('xyz')){
                isComUser = true;
      }      
            if(u.UserRole.Name != null && u.UserRole.Name.contains('pqr'))   { 
                isFPUser = true; 
}

...and the conditions go on.. There are so many conditions that constructor has become really big & unmanagable.

Is there a good way to do this? Thanks in advance

  • Couldn't you just add a custom field to the user record to be this Flag? Then its just an attribute of the user and you don't need all the logic in the controller.....just a thought. – Chris Duncombe Oct 22 '15 at 17:31
  • you could also use getter properties for all these Boolean flags and avoid the constructor altogether – cropredy Oct 22 '15 at 17:37
  • There are some flags, string etc. which are set based on conditions like user, profile, role etc. Sometimes there is a complex logic too. The flags are used in methods & VF page. Many sections on the VF page render based on these. Also, there is a page redirection which happens based on who logged-in user is. The action is called on the page load. In short, want to know the best ideas to optimize this and also reduce the no. of lines of code. Getter properties is also a good idea. I have also seen posts where people using separate class for accessing user info and calling it from controller. – TJD Oct 22 '15 at 18:07
1

You can use conditions as Boolean assignments to reduce the complexity of the code. This has the side effect of increasing code coverage "for free", as you no longer have to test each IF statement solely for the purpose of code coverage. You can also combine this with getters for even more compactness. For example:

public Boolean isComUser { get { return u.UserRole.Name != null && u.UserRole.Name.contains('xyz'); } }

(Assume that u is instead defined as a global variable instead of inside a function).

Even just placing this technique in your constructor can reduce complexity:

isComUser = u.UserRole.Name != null && u.UserRole.Name.contains('xyz');

This will also ensure that all variables are initialized to true/false, avoiding System.NullPointerException errors when using the values.

Finally, I also tend to leverage Custom Permissions to assign permissions to profiles, or Custom Settings to avoid wasting SOQL queries. It's generally a better idea to create permissions or settings instead of hard-coding data into the user's profile or role name.

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.