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
Our Man In BananasOur Man In Bananas 

VF Page lookup field in User object

Hi,
I am building a VF page for my custom object Change Request.

In my object trigger on Save, I populate the Manager field of the record with this code:
if (trigger.isBefore && (trigger.isInsert || trigger.isUpdate))
    {
          system.debug('step 3 - set Manager ID & department name');
          
          for (Request_for_System_Change__c r : Trigger.new) {
              
                  string LineManagerid = [SELECT Id, ManagerId from User where Id =: r.OwnerId].ManagerId;
                  string DeptName = [SELECT Id, Department from User where Id =: userinfo.Getuserid()].Department;
                  r.Manager__c = LineManagerId;  
                  r.Department_Name_String__c = DeptName;             
          }         
        }
an that works fine.

So now I am building a VF page I need to do something similar but in my extension class as currently it just shows the manager ID like this "Manager: 009w0000003LRsuAAZ".

So at the top of my VF page I have:
<apex:page id="ChangeRequest" StandardController="Request_for_System_Change__c" extensions="RequestForSystemChangeController"
           standardStylesheets="false" 
			showHeader="false"  >
and the field is defined like this:
<apex:pageBlockSectionItem>
    <apex:outputLabel value="Manager: "/>
        <apex:panelGrid columns="1">
            <apex:outputText value="{!get_Manager(Request_for_System_Change__c.Owner__c)}"/>
         </apex:panelGrid>
</apex:pageBlockSectionItem>
In my extension class I have this method to get the Manager from the User object:
public string get_Manager(string owner_id){
	string LineManagerid = [SELECT Id, ManagerId from User where Id =: owner_id].ManagerId;
//      string LineManagerid; // = [SELECT Id, ManagerId from User where Id =:ApexPages.currentPage().getParameters().get(‘Manager_id’)];
        string DeptName = [SELECT Id, Department from User where Id =: userinfo.Getuserid()].Department;
        return LineManagerId;  
                  //r.Department_Name_String__c = DeptName;                  
    }
But I am getting an error: Unknown function get_Manager. Check spelling.

so what is the correct way to do this, and where am I going wrong?



 
Krishna SambarajuKrishna Sambaraju
Here is how your extension class can be.
public class RequestForSystemChangeController{
	public string lineManagerId {get; set;}
	public RequestForSystemChangeController(ApexPages.StandardController controller)
	{
		Request_for_System_Change__c reqChange = [select Id, Owner__c from Request_for_System_Change__c where Id = :controller.getId()];
		lineManagerid = [SELECT Id, ManagerId from User where Id =: reqChange.Owner__c].ManagerId;
	}
}
And reference the variable "lineManagerId" declared in the class in the visualforce page as below.
 
<apex:pageBlockSectionItem>
    <apex:outputLabel value="Manager: "/>
        <apex:panelGrid columns="1">
            <apex:outputText value="{!lineManagerId}"/>
         </apex:panelGrid>
</apex:pageBlockSectionItem>

Hope this works as you needed.
 
Our Man In BananasOur Man In Bananas
Thanks but now I am getting an error:
 
System.QueryException: List has no rows for assignment to SObject 
Class.RequestForSystemChangeController.<init>: line 17, column 1
it's on the line

Request_for_System_Change__c reqChange = [select Id, Owner__c from Request_for_System_Change__c where Id = :stdController.getId()];

it's kind of strange as the ID of the record definitely exists
Krishna SambarajuKrishna Sambaraju
You need to pass the id field as query string to the visualforce page. In your browser after the page name type in ?id=<id of Request_for_System_Change__c>

Let me know if you still have problems.