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
BeatBeat 

Can not get values from apex:inputField in Extended Controller

Hi,

 

I'm not able to get the inputField values passed into my extended controller. Need that to do some calculations in my apex code and refresh the visualforce page to show the new calculated values to the user.

Here share the code I have up to now:

 

Visualforce code: (please see the line comments on it)

 

<apex:page standardController="Cnno_Purchase_Requisition__c" extensions="PurchaseRequisitionController" id="PR_New_page"> <apex:SectionHeader title="Purchase Requisition" subtitle="New Purchase Resquisition" /> <apex:form > <apex:pageblock title="Purchase Requisition Creation" mode="edit" id="block"> <apex:pageMessages /> <apex:pageBlockButtons location="both"> <apex:commandButton action="{!save}" value="Save"></apex:commandButton> <apex:commandButton action="{!cancel}" value="Cancel"></apex:commandButton> <apex:commandButton action="{!doRefresh}" value="Refresh Values" rerender="block"></apex:commandButton> </apex:pageBlockButtons> <apex:pageBlockSection columns="2" title="Information"> <apex:outputField value="{!PurchaseRequisition.OwnerId}"/> <apex:outputField value="{!PurchaseRequisition.Status__c}"/> <apex:pageBlockSectionItem > <apex:outputLabel value="Item Requested"/> <apex:outputPanel id="item"> <apex:inputField value="{!Cnno_Purchase_Requisition__c.Item_Requested__c}"> <!-- apex:actionSupport event="" rerender="item" status="status"/ --> </apex:inputField> <!-- apex:actionStatus startText="applying value..." id="status"/ --> </apex:outputPanel> </apex:pageBlockSectionItem> <apex:inputField value="{!Cnno_Purchase_Requisition__c.Request_Date__c}"/> <apex:inputField value="{!Cnno_Purchase_Requisition__c.Required_Item_Description__c}"/> <apex:outputField value="{!Cnno_Purchase_Requisition__c.Item_Requested__r.Standard_Cost__c}"/> <!-- THIS IS NOT SHOWN!! --> <apex:inputField value="{!Cnno_Purchase_Requisition__c.Ship_To_Location__c}"/> <apex:pageBlockSectionItem > <apex:outputlabel value="Total Amount" /> <apex:outputtext value="{!totalamount}"/> <!-- This throught NULL when refresh performs --> </apex:pageBlockSectionItem> <apex:inputField value="{!Cnno_Purchase_Requisition__c.Max_to_pay__c}" /> </apex:pageBlockSection> <apex:pageBlockSection columns="1" title="Additional Notes"> <apex:inputField value="{!Cnno_Purchase_Requisition__c.Additional_Notes__c}"/> </apex:pageBlockSection> </apex:pageblock> </apex:form> </apex:page>

 

APEX Code in extended controller:

 

public class PurchaseRequisitionController { private Cnno_Purchase_Requisition__c purchaseRequisitionObj; private Cnno_Purchase_Items__c item; private String vendor; private Double totalAmount; public PurchaseRequisitionController(ApexPages.StandardController controller) { this.purchaseRequisitionObj = (Cnno_Purchase_Requisition__c)controller.getRecord(); this.purchaseRequisitionObj.OwnerId = UserInfo.getUserId(); this.purchaseRequisitionObj.Status__c = '1 - Open'; } public Cnno_Purchase_Requisition__c getPurchaseRequisition() { return purchaseRequisitionObj; } public void setPurchaseRequisition(Cnno_Purchase_Requisition__c pr) { this.purchaseRequisitionObj = pr; } public Double getTotalAmount() { return TotalAmount; } public void doRefresh() { totalAmount = purchaseRequisitionObj.Item_Requested__r.Standard_Cost__c * purchaseRequisitionObj.Quanitity__c; } }

 

Will really appreciate your help! I'm blocked here.

 

Thanks!

 

Fernando.-

Best Answer chosen by Admin (Salesforce Developers) 
mattdarnoldmattdarnold

Hi - try updating your controller with the following code. I think the issue you're experiencing is that your referencing an object that is assigend in your constructor, but isn't set again in from the Visualforce page as the standard controller logic is handling the setting for Cnno_Purchase_Requisition__c not your custom getters / setters. Instead you need to get the Cnno_Purchase_Requisition__c object referenced and set by the standard controller logic through the code below. I haven't tested with your code specifically, but I've had a similar issue in some of my work lately and this change fixed the issue for me.

 

-- Matt

 

 

public class PurchaseRequisitionController {

private Cnno_Purchase_Requisition__c purchaseRequisitionObj;
private Cnno_Purchase_Items__c item;
private String vendor;
private Double totalAmount;
ApexPages.StandardController controller;

public PurchaseRequisitionController(ApexPages.StandardController controller) {
this.purchaseRequisitionObj = (Cnno_Purchase_Requisition__c)controller.getRecord();
this.purchaseRequisitionObj.OwnerId = UserInfo.getUserId();
this.purchaseRequisitionObj.Status__c = '1 - Open';
this.controller = stdController;
}

public Cnno_Purchase_Requisition__c getPurchaseRequisition() {
return purchaseRequisitionObj;
}

public void setPurchaseRequisition(Cnno_Purchase_Requisition__c pr) {
this.purchaseRequisitionObj = pr;
}

public Double getTotalAmount() {
return TotalAmount;
}

public void doRefresh() {
totalAmount = ((Cnno_Purchase_Requisition__c)controller.getRecord()).Item_Requested__r.Standard_Cost__c * ((Cnno_Purchase_Requisition__c)controller.getRecord()).Quanitity__c;
}

}

 

 

 

All Answers

mtbclimbermtbclimber

Beat wrote:

 

 


<apex:outputField value="{!Cnno_Purchase_Requisition__c.Item_Requested__r.Standard_Cost__c}"/> <!-- THIS IS NOT SHOWN!! -->


 

 

 This could either be data specific or permission related. First question, does your purchase req object have a related item requested? If not then the relationship Item_Requested__r will be null and no value will be shown.  If the answer is yes then check permissions, does the user you are using have access to read both the lookup field that establishes the relationship AND the related object?  If either is no then this will not show up for the given user and you'll either need to change permissions or query for the value in your controller extension (where you are in system mode)

 

 


Beat wrote:

 


<apex:outputlabel value="Total Amount" /> <apex:outputtext value="{!totalamount}"/> <!-- This throught NULL when refresh performs -->


 

 

 

 Fix the above and I would expect this to go away.

 

 

 

mattdarnoldmattdarnold

Hi - try updating your controller with the following code. I think the issue you're experiencing is that your referencing an object that is assigend in your constructor, but isn't set again in from the Visualforce page as the standard controller logic is handling the setting for Cnno_Purchase_Requisition__c not your custom getters / setters. Instead you need to get the Cnno_Purchase_Requisition__c object referenced and set by the standard controller logic through the code below. I haven't tested with your code specifically, but I've had a similar issue in some of my work lately and this change fixed the issue for me.

 

-- Matt

 

 

public class PurchaseRequisitionController {

private Cnno_Purchase_Requisition__c purchaseRequisitionObj;
private Cnno_Purchase_Items__c item;
private String vendor;
private Double totalAmount;
ApexPages.StandardController controller;

public PurchaseRequisitionController(ApexPages.StandardController controller) {
this.purchaseRequisitionObj = (Cnno_Purchase_Requisition__c)controller.getRecord();
this.purchaseRequisitionObj.OwnerId = UserInfo.getUserId();
this.purchaseRequisitionObj.Status__c = '1 - Open';
this.controller = stdController;
}

public Cnno_Purchase_Requisition__c getPurchaseRequisition() {
return purchaseRequisitionObj;
}

public void setPurchaseRequisition(Cnno_Purchase_Requisition__c pr) {
this.purchaseRequisitionObj = pr;
}

public Double getTotalAmount() {
return TotalAmount;
}

public void doRefresh() {
totalAmount = ((Cnno_Purchase_Requisition__c)controller.getRecord()).Item_Requested__r.Standard_Cost__c * ((Cnno_Purchase_Requisition__c)controller.getRecord()).Quanitity__c;
}

}

 

 

 

This was selected as the best answer
BeatBeat
Many thanks for your response. Look, just to provide more context details, this visualforce page is for creating a Purchase Requisition (Overwrite standard "New"). So the purchase req in fact does not exist at this instance. AS purchase item is defined as lookup relation, I first select one purchase item from the pop up and then once selected and again in my page, I do a refresh to update the "Standard Cost" of the pruchase item in the page shown. That's the idea.
mtbclimbermtbclimber

Beat, can you explain how this resolves your issue?  AFAIK, you are going to need to query the related object to get the value you are after which I don't see happening in Matt's suggestion.

BeatBeat

Andrew, I got the solution based in the two responses I received. First, this sentence made me think about "First question, does your purchase req object have a related item requested? If not then the relationship Item_Requested__r will be null and no value will be shown." And then this code suggestion:

 

public void doRefresh() { totalAmount = ((Cnno_Purchase_Requisition__c)controller.getRecord()).Item_Requested__r.Standard_Cost__c * ((Cnno_Purchase_Requisition__c)controller.getRecord()).Quanitity__c; }

 

 

With all these, I made the corresponding changes and get this:

 

public class PurchaseRequisitionController { private Cnno_Purchase_Requisition__c purchaseRequisitionObj; private Cnno_Purchase_Items__c item; private Account vendor; private Double totalAmount; private Double stdCost; ApexPages.StandardController controller; public PurchaseRequisitionController(ApexPages.StandardController controller) { this.purchaseRequisitionObj = (Cnno_Purchase_Requisition__c)controller.getRecord(); this.purchaseRequisitionObj.OwnerId = UserInfo.getUserId(); this.purchaseRequisitionObj.Status__c = '1 - Open'; this.controller = controller; } public Cnno_Purchase_Requisition__c getPurchaseRequisition() { return purchaseRequisitionObj; } public void setPurchaseRequisition(Cnno_Purchase_Requisition__c pr) { this.purchaseRequisitionObj = pr; } public Double getTotalAmount() { return totalAmount; } public Double getStandardCost() { return stdCost; } public Account getVendor() { return vendor; } public void doRefresh() { String piName = ((Cnno_Purchase_Requisition__c)controller.getRecord()).Item_Requested__c; item = [select Standard_Cost__c, Preferred_Vendor__c from Cnno_Purchase_Items__c where id = :piName]; vendor = [select Name from Account where id = :item.Preferred_Vendor__c]; stdCost = item.Standard_Cost__c; totalAmount = ((Cnno_Purchase_Requisition__c)controller.getRecord()).Quanitity__c * stdCost; } }

 

 

Now I call the getters from my visualforce page and all the values are refreshed and showed. Of course now I have to clean up the code... but it is functioning!!

 

Thanks a lot.

 

Fernando

mtbclimbermtbclimber

Right. I thought there might be more to this. 

 

Just so no one reading this later gets the wrong impression, there is no need to go through the standardcontroller's getRecord() method in the action when you've already saved off that result to a class member variable. The aspect that was missing was the query. 

 

In other words, this condensed code:

 

public class PurchaseRequisitionController {

private Cnno_Purchase_Requisition__c purchaseRequisitionObj;
private Double totalAmount;

public PurchaseRequisitionController(ApexPages.StandardController controller) {
this.purchaseRequisitionObj = (Cnno_Purchase_Requisition__c)controller.getRecord();
}

public Double getTotalAmount() {
return TotalAmount;
}

public void doRefresh() {
totalAmount = purchaseRequisitionObj.Item_Requested__r.Standard_Cost__c * purchaseRequisitionObj.Quanitity__c;
}

}

 

would be equivalent in function with:

 

public class PurchaseRequisitionController {

private Double totalAmount;
ApexPages.StandardController controller;

public PurchaseRequisitionController(ApexPages.StandardController controller) {
this.controller = stdController;
}


public Double getTotalAmount() {
return TotalAmount;
}

public void doRefresh() {
totalAmount = ((Cnno_Purchase_Requisition__c)controller.getRecord()).Item_Requested__r.Standard_Cost__c * ((Cnno_Purchase_Requisition__c)controller.getRecord()).Quanitity__c;
}

}