Custom User Controller Extension - to allow users to edit fields on user record

[update : captains log june 18 2020. #summer20 release. its a lot easier to use a flow that runs in system mode to do most anything that requires a without-sharing apex controller]

As custom controllers can run in system mode, unencumbered by such niceties as security restrictions, I reasoned that I could pull together a custom visualforce page that users could go to which would allow them to set the values of fields on their own user records, that otherwise they would not have access to. I cannot say that this is definitely a good idea. I am not a coder and did a lot of reading, guessing, and borrowing to bring this code to life. To see the final product, go to this link

A couple of books I can recommend are The Visualforce Cookbook by Keir Bowden and Development with the Force.com Platform by Jason Ouellette

I went for an extension on the User object, because it seemed to make sense that this would extend the standard functionality, and I guess I was right because it works… I am not a coder. Use at your own risk.

Simple Controller Extension #

I created 4 text input fields on the user object to store values that are referenced by a custom button. For the story behind this, see this link:. This code pulls in the custom fields from the User object, making them accessible to my visualforce page. The pilot version of the visualforce page displayed text boxes for user input, which were replaced w/ picklists in the final version. First, the pilot:

public class UserSelectorExtensionBeta {
private final User usr;
public UserSelectorExtensionBeta(ApexPages.StandardController stdController) {
   this.usr = (User)stdController.getRecord();
 }

<!--   Pull 4 custom fields into constructor -->
user u = [SELECT Id, Default_Department_Code__c,       Default_Location_Code__c, Default_Funding_Source__c, Default_P_L_Code__c, Include_Opp_Amount_on_FL__c From USER Where Id =:UserInfo.GetuserId()];

public User getUser () {
     return u;
     }   

public PageReference save() {
   update u;
    return null;
    }   
 }

I showed this to a few users and they loved the idea, but were not particularly impressed by the text boxes for input, so I managed to figure out how to dynamically populate those w/ some help from Bob Buzzard, aka Keir Bowden, on developer.force.com, but I have not been able to locate the original post I referenced. The updated code for the controller extension is copied below. I will try to dig in and explain it in detail soon. and will add a post for the visualforce page that references this controller.

Updated code, which dynamically creates picklists on the visualforce page instead of text boxes (though the fields it writes to are indeed text boxes), is copied below.

Full Controller Extension #

3 of the 4 fields are picklists, one is a relationship (lookup) field. The bit that starts with public List getfundCodes() is the lookup field, where I am pulling the values of records in a custom object to display in the picklist, and binding them to the standard record name field. For the picklist fields, I am pulling the values of the fields via a schema getdescribe call on the picklist field, and adding them to the list of options.

Public class UserControllerExtension {
private final User usr;

public UserControllerExtension(ApexPages.StandardController stdController) {
    this.usr = (User)stdController.getRecord();    
 }

public user u = [SELECT Id, Default_Department_Code__c, Default_Location_Code__c, Default_Funding_Source__c, Default_P_L_Code__c, Set_Default_Allocation_Status_to_Final__c, Include_Opp_Amount_on_FL__c From USER Where Id =:UserInfo.GetuserId()];

public User getUser () {
     return u;
     }   

public List<SelectOption> getDepCodes()
     {
     List<SelectOption> options = new List<SelectOption>();
     options.add(new SelectOption(' ','--None--'));

    Schema.DescribeFieldResult fieldresult = Funding_Link__c.Department_Code__c.getDescribe();
    List<Schema.PicklistEntry> depcode = fieldResult.getPicklistValues();

    for (Schema.PicklistEntry f: depcode)
    {
    options.add(new SelectOption(f.getLabel(), f.getValue()));
    }
    return options;
    }

public List<SelectOption> getLocCodes()
    {
     List<SelectOption> locoptions = new List<SelectOption>();
     locoptions.add(new SelectOption(' ','--None--'));

    Schema.DescribeFieldResult locfieldresult = Funding_Link__c.Location_Code__c.getDescribe();
    List<Schema.PicklistEntry> loccode = locfieldResult.getPicklistValues();

    for (Schema.PicklistEntry f: loccode)
    {
    locoptions.add(new SelectOption(f.getLabel(), f.getValue()));
    }
    return locoptions;
   }

public List<SelectOption> getPlCodes()
    {
     List<SelectOption> ploptions = new List<SelectOption>();
     ploptions.add(new SelectOption(' ','--None--'));

    Schema.DescribeFieldResult plfieldresult = Funding_Link__c.P_L_Code__c.getDescribe();
    List<Schema.PicklistEntry> plcode = plfieldResult.getPicklistValues();

    for (Schema.PicklistEntry f: plcode)
    {
    ploptions.add(new SelectOption(f.getLabel(), f.getValue()));
    }
    return ploptions;
   }    

public List<SelectOption> getfundCodes()
   {
     List<SelectOption> fundoptions = new List<SelectOption>();
     fundoptions.add(new SelectOption(' ','--None--'));
     List<Funding_Source__c> fs = [Select Name, Funding_Source_Name__c from Funding_Source__c WHERE Active__c=TRUE ORDER BY Name ASC];        
     for (Funding_Source__c f: fs)
     {
     string displayFS = (f.name + '  '+ f.Funding_Source_Name__c);
     fundoptions.add(new SelectOption(f.name, displayFS));
     }
     return fundoptions;
   }

 public PageReference save() {
   update u;
    return null;
    }   
  }
 
11
Kudos
 
11
Kudos

Now read this

Using a dynamic choice to select multiple records in visual workflow

With a small trick, Dynamic choice elements in visual workflow can be used to display multiple records, and only act on the selected record(s). In a previous post, I described how to create a button that could copy all attachments from... Continue →