+ Start a Discussion
PjayPjay 

How to update an inputField dynamically on a VF page?

Hi all

 

Hope someone can help with this, have been trying to do this for a while and have searched for a solution to no avail. 

 

Here is a snippet from my VF page

<apex:inputField value="{!Service__c.Service_Fee__c}" id="txtServiceFee"/> ... <apex:inputField value="{!Service__c.Type__c}"> <apex:actionSupport event="onblur" action="{!UpdateServiceFee}" rerender="txtServiceFee"/> </apex:inputField>

 

What I am trying to do is update the txtServiceFee inputField value on the VF page whenever the onblur event of lookup field Type__c is triggered. I have attempted to do that with the APEX code below:

 

public class ServiceFee { private Service__C servc; public ServiceFee(ApexPages.StandardController stdController) { servc = (Service__C)stdController.getRecord(); servc.Service_Fee__C = 12.00; } public void UpdateServiceFee() { servc.Service_Fee__C = 16.00; } }

 

When the page loads the txtservicefee input field is set to 12.00 as expected. However when the onblur event is triggered the inputField IS rerendered but isn't updated to 16.00.

 

What am I doing wrong here? Any help would be greatly appreciated.

 

Thanks

Paul

Message Edited by Pjay on 10-28-2009 05:40 PM
Best Answer chosen by Admin (Salesforce Developers) 
ShikibuShikibu

Hmm. I don't think I agree with prageeth (for instance, if the servc record is being created anew, it would need to be inserted, not updated, and it won't be able to be inserted if some other fields have not yet been filled in so the whole record is valid).

 

And I don't think I agree with Rajesh. The constructor gets the record, so the private variable should be referring to the same record that the page is referring to. I think this means the VF page needs to use the standard controller (along with the controller extension that Pjay is creating).

 

I suggest that you try surrounding the inputfield with an actionregion, to make sure that you are not also submitting the existing value of Service__c.Service_Fee__c. "Immediate=true" in the actionregion will cause this data to be submitted without running validations on the page (for instance, complaining if required fields elsewhere on the page have not been filled).

 

 

<apex:actionRegion immediate="true"> <apex:inputField value="{!Service__c.Type__c}"> <apex:actionSupport event="onblur" action="{!UpdateServiceFee}" status="updating_txtServiceFee" rerender="txtServiceFee"/> </apex:inputField> </apex:actionRegion>

 

 

 

 

 

 

 

All Answers

prageethprageeth

Hi Pjay;

 

You need to update the database. Change your UpdateServiceFee() method as below.

 

public void UpdateServiceFee()

{

servc.Service_Fee__C = 16.00;

update servc;

}

 

 

 

 

Rajesh ShahRajesh Shah
You have declared servc as a private variable in your class. It is no way related to the object with which you are trying to display on the page. Make servc as public with getter and setter and use that to display on page and then it will work.
ShikibuShikibu

Hmm. I don't think I agree with prageeth (for instance, if the servc record is being created anew, it would need to be inserted, not updated, and it won't be able to be inserted if some other fields have not yet been filled in so the whole record is valid).

 

And I don't think I agree with Rajesh. The constructor gets the record, so the private variable should be referring to the same record that the page is referring to. I think this means the VF page needs to use the standard controller (along with the controller extension that Pjay is creating).

 

I suggest that you try surrounding the inputfield with an actionregion, to make sure that you are not also submitting the existing value of Service__c.Service_Fee__c. "Immediate=true" in the actionregion will cause this data to be submitted without running validations on the page (for instance, complaining if required fields elsewhere on the page have not been filled).

 

 

<apex:actionRegion immediate="true"> <apex:inputField value="{!Service__c.Type__c}"> <apex:actionSupport event="onblur" action="{!UpdateServiceFee}" status="updating_txtServiceFee" rerender="txtServiceFee"/> </apex:inputField> </apex:actionRegion>

 

 

 

 

 

 

 

This was selected as the best answer
PjayPjay

Thanks all for the replies. Big thanks to Shikibu, that solution worked great. It is behaving as expected now. The only issue was the label for the inputField within the actionRegion tags has disappeared. It seems encasing an inputField (this particular inputField is bound to a lookup field) within an action region makes the label disappear. I wonder if this is by design?

 

Anyone know of anyway to show the label? Or do I just have to create it manually with a label tag etc.?

ShikibuShikibu

Use a pageSectionItem and an outputLabel. It's frustrating, but it seems that pageSection only adds labels for top-level elements it surrounds, not for all outputFields that it surrounds. So if you surround an outputField in a pageSection with anything, you must put the whole kaboodle inside a pageSectionItem and add your own label.

 

 

PjayPjay
Will do. Thanks Again Shikibu.