0

I have a visualforce page for new cases. It uses an extension, which queries the user object to obtain user information. The user information is used to pre-populate some lookup fields in my visualforce page. Everything works up to this point. However, When I click the save button, the pre-populated fields are not saved when the case record is created.

Here is my visualforce page

<apex:page standardcontroller="Case" tabstyle="Case" extensions="UserExtension">
    <apex:form >
      <apex:sectionheader title="Case Edit" subtitle="{!if(Case.Id==null,'New Case',Case.CaseNumber)}"></apex:sectionheader>
       <apex:pageblock mode="edit" id="casePB" title="Case Edit">
         <apex:pageblockbuttons >
            <apex:commandbutton value="Save" action="{!save}"></apex:commandbutton>
            <apex:commandbutton value="Cancel" action="{!cancel}"></apex:commandbutton>
         </apex:pageblockbuttons>
<apex:pageblocksection id="UserInformationPBS" title="User Information">
    <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>
</apex:pageblocksection>
<apex:pageblocksection id="CaseInformationPBS" title="Case Details">
    <apex:inputfield value="{!Case.Type}"></apex:inputfield>
        <apex:outputtext value=""></apex:outputtext>  
        <apex:inputfield value="{!Case.Subject}"></apex:inputfield>
        <apex:outputtext value=""></apex:outputtext>
    <apex:inputfield value="{!Case.Request_Description__c}"></apex:inputfield>
</apex:pageblocksection>
</apex:pageblock>
</apex:form>

Here is my extension

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;
    } 
}
0

Unless you override the standard save() method provided by the standardController, clicking the save button on your page will cause the instance of the Case object, held by the standardController to be saved.

In your extension, you're creating a completely separate instance of Case, and pre-populating the fields on that instance.

To get things to behave the way you expect them to, you need to be working on the Case instance held by the standardController.

The quickest, simplest fix here is a one-line change

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

    // Instead of making a new case, grab the one that already exists from the 
    // controller.
    // This is done by using the getRecord() method.
    // getRecord() returns a generic SObject, so you'll need to explicitly cast
    //   it before use.
    caseObject = (Case)cont.getRecord();
    //caseObject = new Case();
    caseObject.Requested_By__c = currentuser.Id;
    caseObject.User_Phone__c = currentuser.Phone;
    caseObject.User_Email__c= currentuser.Email;
}

In terms of coding practice/style. Having your visualforce page refer to the same thing by two different names would probably be a bit confusing. My preference would be to remove caseObject from your visualforce page (using Case instead). The updated code of your constructor shouldn't need to be changed, though you may want to change caseObject to case for consistency's sake there as well.

  • Thank you for explaining this to me and guiding me in a better practice. – MarvinM Aug 4 '17 at 17:12
0

Can you make these changes where you could use this line

caseObject = (Case) cont.getRecord();

Controller

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

    public UserExtension(ApexPages.StandardController cont){
        caseObject = (Case) cont.getRecord(); 

        currentuser=[Select Id, Name, Email, Phone from User where id=:userinfo.getuserId()];

        caseObject.Requested_By__c = currentuser.Id;
        caseObject.User_Phone__c = currentuser.Phone;
        caseObject.User_Email__c= currentuser.Email;
    } 
}
0

The problem is that caseObject isn't saved by the standard controller Save. You need to make it reference the standard controller record.

Instantiate your caseObject like this:

caseObject = (Case)cont.getRecord();

Then change your VF to always point to Case. For example:

<apex:inputfield value="{!Case.Requested_By__c}"></apex:inputfield>

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.