You need to sign in to do that
Don't have an account?
![Markey1 Markey1](https://dfc-org-production.my.site.com/img/userprofile/default_profile_45_v2.png)
Need Help or Direction - How to reference fields on Visualforce page
I am utilizing Sites and my Visualforce pages use a custom object as the standard controller with an extension for additional functionality.
How do I reference fields directly on a Visualforce page? For example, I have two fields that only have id's and no input value.
Example:
<apex:pageBlockSectionItem > <apex:outputLabel value="US001_VEReq" for="test_us001vereq" /> <apex:inputTextarea id="test_us001vereq"/> </apex:pageBlockSectionItem>
<apex:pageBlockSectionItem > <apex:outputLabel value="US001_VERes" for="test_us001veres" /> <apex:inputTextarea id="test_us001veres" /> </apex:pageBlockSectionItem>
I want to combine the input from these two Text Area Fields into one field which does have a value, meaning upon save the concatenated (combined) value will be entered (saved) into the database.
Example:
<apex:pageBlockSectionItem > <apex:outputLabel value="US001" for="test_us001" /> <apex:inputTextArea value="{!SK__c.US001__c}" id="test_us001" /> </apex:pageBlockSectionItem>
What options do I have to accomplish this task. I have spent 4 days looking at the forums, code examples, etc. and can not figure out how to make this work. Any suggestions, ideas, code examples, and/or links are much appreciated. Please let me know if this is unclear and I will provide more detail.
Also, for anyone familiar with MS Access Databases, this would be similar to bound/unbound fields and how to reference those fields within a form via VBA etc.
Markey,
You assign the strings to your constructor. You don't want to do it that way because the constructor controls what happens when the page is initialized. This is why the field is getting set to "null null" because when the page is being generated, the constructor is assigning the variables' values (which are null) to that sObject field.
What you want to do is move that line to your save method. This will take whatever input is in the fields and assign it to the sObject field. Try this:
One thing to note about your code--
You create a standardController variable, but this isn't really necessary for what your doing, since in your constructor, you assign your skTest variable to the standard Controller's record. When you call standardController.save(), it just upserts the record into the database. Since your class already references this record, you can just do "upsert skTest" and it will perform the exact same action. Doing this will allow you to take out 3 lines of code
:
All Answers
Markey1, this is not complicated to do.
If you want to store data in fields and pass it to the database, you have two options:
1: Store it in <apex:inputField> components, which directly reference Sobject fields.
2: Store it in an instance variable that you create in your controller extension.
Option 2 sounds like what you need to do. Assuming you just need some simple text input in these fields, you need to create instance variables in your controller extension and make them publicly available. Heres the syntax you would use:
This creates 2 public variables and establishes a default getter/setter method for them. From here, you can reference these variables in your visualforce page simply by typing {!fieldInput1} or {!fieldInput2} depending on which one you need.
In order to tie these variables to fields on your page, you would use the Visualforce syntax:
Now any time a user types something into these fields, your controller will assign the input to the string variables. From there, you can do whatever you want with it in your controller, for instance:
I hope this helps.
dwwright,
Thank you so much for your reply. This makes sense and is a whole lot easier than I anticipated. I've got the concept down but have one more question. When the Visualforce page loads, the forms shows "nullnull" in the "US001__c" field . How do I get this box to populate after the user has entered text into the "test_us001vereq" and "test_us001veres" fields? Also, lets say the user changes the text before submitting the form, will the "US001__c" field automatically update?
I am an Apex noob and may have entered your examples incorrectly. Items added are in blue below.
My Code:
By the way, here is the Visualforce page code:
Unbound Fields (user will enter text)
Bound field (text entered by the user in the above fields should be combined/concatenated and then saved to this field)
Markey,
You assign the strings to your constructor. You don't want to do it that way because the constructor controls what happens when the page is initialized. This is why the field is getting set to "null null" because when the page is being generated, the constructor is assigning the variables' values (which are null) to that sObject field.
What you want to do is move that line to your save method. This will take whatever input is in the fields and assign it to the sObject field. Try this:
One thing to note about your code--
You create a standardController variable, but this isn't really necessary for what your doing, since in your constructor, you assign your skTest variable to the standard Controller's record. When you call standardController.save(), it just upserts the record into the database. Since your class already references this record, you can just do "upsert skTest" and it will perform the exact same action. Doing this will allow you to take out 3 lines of code
:
dwwright,
You are friggin' awesome, thank you so much! I finally got this working with your help... such a simple task and I was making it much harder than required. Also, thank you for help with cleaning up the unecessary lines of code.
If you have any suggestions in regards to beginner Apex Developer guides please share... when I say beginner, not beginner as in a programmer learning Apex, but someone who is learning Apex as their first programming experience. Most of the documentation I have come across for beginners assumes the user is familiar with programming techniques and terminology.... but I digress, thanks again!
For anyone else viewing this post, this is the code I ended up with to make the concatenation of fields work.
Page:
Class: