+ Start a Discussion
Markey1Markey1 

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.

Best Answer chosen by Admin (Salesforce Developers) 
dwwrightdwwright

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:

public class AESK_Test {
 
    /*Variables*/
    public SafeKey_Certification__c skTest;
    
    
    public String test_us001vereq {get; set;}
    public String test_us001veres {get; set;}        
      
    /*Controller*/
    ApexPages.StandardController controller;
    public AESK_Test(ApexPages.StandardController con){
        controller = con;
        skTest = (SafeKey_Certification__c)controller.getRecord();
        //Deleted line here
    }           

    /*Validate, save, and return page*/
    public PageReference save() {    
        try {
            //Set the value of the field before inserting
            skTest.US001__c = test_us001vereq + test_us001veres;
            //Then do the insert
            insert skTest;
            /*controller.save();*/
        } 
        catch (DMLException e) {
            ApexPages.addMessages(e);
            return null;
        }
        return null; 
    } 
}

 

 

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

public class AESK_Test {
 
    /*Variables*/
    public SafeKey_Certification__c skTest;
    
    
    public String test_us001vereq {get; set;}
    public String test_us001veres {get; set;}        
      
    //Deleted Line here
    public AESK_Test(ApexPages.StandardController con){
        //Deleted line here
        skTest = (SafeKey_Certification__c)con.getRecord();
        //Deleted line here
    }           

    /*Validate, save, and return page*/
    public PageReference save() {    
        try {
            //Set the value of the field before inserting
            skTest.US001__c = test_us001vereq + test_us001veres;
            //Then do the upsert
            upsert skTest;
            //Deleted line here
        } 
        catch (DMLException e) {
            ApexPages.addMessages(e);
            return null;
        }
        return null; 
    } 
}

 

:

 

 

 

 

All Answers

dwwrightdwwright

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:

 

 

public String fieldInput1 {get; set;}
public String fieldInput2 {get; set;}

 

 

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:

 

<apex:inputTextarea id="test_us001vereq" value="{!fieldInput1}" />
<apex:inputTextarea id="test_us001veres" value="{!fieldInput2}" />

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:

 

 

//Assuming SK__c is actually a variable of an SK__c sObject
SK__c.US001__c = fieldInput1 + '\n' + fieldInput2;

 

I hope this helps.

 

 

 

Markey1Markey1

public class AESK_Test {
 
    /*Variables*/
    public SafeKey_Certification__c skTest;
    
    
    public String test_us001vereq {get; set;}
    public String test_us001veres {get; set;}        
      
    /*Controller*/
    ApexPages.StandardController controller;
    public AESK_Test(ApexPages.StandardController con){
        controller = con;
        skTest = (SafeKey_Certification__c)controller.getRecord();
        skTest.US001__c = test_us001vereq + test_us001veres;
    }           

    /*Validate, save, and return page*/
    public PageReference save() {    
        try {
            insert skTest;
            /*controller.save();*/
        } 
        catch (DMLException e) {
            ApexPages.addMessages(e);
            return null;
        }
        return null; 
    } 
}

 

Markey1Markey1

By the way, here is the Visualforce page code:

 

Unbound Fields (user will enter text)

<apex:pageBlockSectionItem >
  <apex:outputLabel value="US001_VEReq" for="test_us001vereq" />
  <apex:inputTextarea id="test_us001vereq" value="{!test_us001vereq}" />
</apex:pageBlockSectionItem>
              
<apex:pageBlockSectionItem >
  <apex:outputLabel value="US001_VERes" for="test_us001veres" value="{!test_us001veres}" />
  <apex:inputTextarea id="test_us001veres"  />
</apex:pageBlockSectionItem>

 

Bound field (text entered by the user in the above fields should be combined/concatenated and then saved to this field)

<apex:pageBlockSectionItem >
  <apex:outputLabel value="US001" for="test_us001" />
  <apex:inputTextArea value="{!SafeKey_Certification__c.US001__c}" id="test_us001" />
</apex:pageBlockSectionItem>

 

dwwrightdwwright

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:

public class AESK_Test {
 
    /*Variables*/
    public SafeKey_Certification__c skTest;
    
    
    public String test_us001vereq {get; set;}
    public String test_us001veres {get; set;}        
      
    /*Controller*/
    ApexPages.StandardController controller;
    public AESK_Test(ApexPages.StandardController con){
        controller = con;
        skTest = (SafeKey_Certification__c)controller.getRecord();
        //Deleted line here
    }           

    /*Validate, save, and return page*/
    public PageReference save() {    
        try {
            //Set the value of the field before inserting
            skTest.US001__c = test_us001vereq + test_us001veres;
            //Then do the insert
            insert skTest;
            /*controller.save();*/
        } 
        catch (DMLException e) {
            ApexPages.addMessages(e);
            return null;
        }
        return null; 
    } 
}

 

 

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

public class AESK_Test {
 
    /*Variables*/
    public SafeKey_Certification__c skTest;
    
    
    public String test_us001vereq {get; set;}
    public String test_us001veres {get; set;}        
      
    //Deleted Line here
    public AESK_Test(ApexPages.StandardController con){
        //Deleted line here
        skTest = (SafeKey_Certification__c)con.getRecord();
        //Deleted line here
    }           

    /*Validate, save, and return page*/
    public PageReference save() {    
        try {
            //Set the value of the field before inserting
            skTest.US001__c = test_us001vereq + test_us001veres;
            //Then do the upsert
            upsert skTest;
            //Deleted line here
        } 
        catch (DMLException e) {
            ApexPages.addMessages(e);
            return null;
        }
        return null; 
    } 
}

 

:

 

 

 

 

This was selected as the best answer
Markey1Markey1

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:

<apex:pageBlockSectionItem >
  <apex:outputLabel value="US001_VEReq" for="test_us001vereq" />
  <apex:inputTextarea id="test_us001vereq" value="{!test_us001vereq}"/>
</apex:pageBlockSectionItem>
              
<apex:pageBlockSectionItem >
  <apex:outputLabel value="US001_VERes" for="test_us001veres" />
  <apex:inputTextarea id="test_us001veres" value="{!test_us001veres}"  />
</apex:pageBlockSectionItem>

 Class:

public class AESK_Test {
 
    //Variables
    public SafeKey_Certification__c skTest;
    public String test_us001vereq {get; set;}
    public String test_us001veres {get; set;}
        
    
    //Controller
    public AESK_Test(ApexPages.StandardController con){
        skTest = (SafeKey_Certification__c)con.getRecord();
    }           

    //Validate, save, and return page
    public PageReference save() {    
        try {
            //set field values
            skTest.US001__c = test_us001vereq + test_us001veres;
            upsert skTest;
        } 
        catch (DMLException e) {
            ApexPages.addMessages(e);
            return null;
        }
        return null; 
    } 
}