0

I have a custom link on the home page that is used to create a new case. The link opens a custom visualforce page for the case object. I am trying to prepopulate a field called RequestedBy (lookup to user) with the user's name. My visualforce page uses an extension, which is just a query into the user object. I am retrieving the user's name, but I the RequestedBy field is not being popluated with the name.

I tried by using <apex:inputfield value="{!currentuser.name}"></apex:inputfield> in my vf page, but that doesn't work.

public class UserExtension{ 
    public user currentuser{get;set;} 
    public UserExtension(ApexPages.StandardController cont){
        currentuser=new User(); 
        currentuser=[Select Id,Name,Email,Phone from User where Id=:userinfo.getuserId()]; 
    } 
}

Can someone please help me? I'm trying to populate the Requested_By__C, User_Phone__C, and User_Email__C fields in my vforce form with the results from my query in the above controller.

<apex:inputfield value="{!Case.Requested_By__c}"></apex:inputfield>
<apex:inputfield value="{!Case.User_Phone__c}"></apex:inputfield>
<apex:inputfield value="{!Case.User_Email__c}"></apex:inputfield>
  • 1
    Could you also show the relevant parts of the controller extension? – martin Jul 31 '17 at 1:51
  • @martin, My controller is the below. public class UserExtension{ public user currentuser{get;set;} public UserExtension(ApexPages.StandardController cont){ currentuser=new User(); currentuser=[Select Id,Name,Email,Phone from User where Id=:userinfo.getuserId()]; } } – MarvinM Aug 1 '17 at 4:43
  • Thanks, I've edited that into the question and nominated your question for reopening. – martin Aug 1 '17 at 4:56
  • Thanks @martin...I'll be sure to pose my question better next time. – MarvinM Aug 1 '17 at 4:58
0

To populate the name of the user, you just need to refer the RequestedBy field.

<apex:inputField value="{!Case.API_Name_of_RequestedBy_Field}" />

The apex:inputField will populate the Name of the lookup field.

As you are not using Case extension, to auto-populate the data from the User object and then create a case, do the following

public class UserExtension{ 
    public user currentuser{get;set;}
    public Case caseObject {get;set;}

    public UserExtension(ApexPages.StandardController cont){
        currentuser=new User(); 
        currentuser=[Select Id, Name, Email, Phone from User where id=:userinfo.getuserId()];
        caseObject = new Case();
        caseObject.Requested_By__c = currentuser.Id;
        caseObject.User_Phone__c = currentuser.Phone;
        caseObject.User_Email__c= currentuser.Email;
} 

}

VF page:

<apex:inputfield value="{!caseObject.Requested_By__c}"></apex:inputfield>
<apex:inputfield value="{!caseObject.User_Phone__c}"></apex:inputfield>
<apex:inputfield value="{!caseObject.User_Email__c}"></apex:inputfield>
  • @Anika, When I do what you suggest, the page opens and the look up field is there, but the name of the user doesn't populate the field. <apex:inputField value="{!Case.Requested_By__c}" /> – MarvinM Aug 1 '17 at 4:46
  • Is it Case Extension? Also, are you passing the id of the case record? – Ankita Aug 1 '17 at 5:26
  • No, I am trying to create a new case with the use of a custom link. The link opens my vforce page, which has an extension that fires off a query against the user object. – MarvinM Aug 1 '17 at 5:46
  • You are not querying on the Case object. So, you will not get the value. – Ankita Aug 1 '17 at 5:51
  • Sorry, I am not being clear. I"m trying to create a new case and auto-populate the requested by, email, and phone number fields on a custom vforce page layout. I use the a custom link on the home page to launch the vforce page, which is using the extension to query the user object and return the data I need. – MarvinM Aug 1 '17 at 5:57

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.