2

I have been trying to do something that I thought was easy, but it is making me crazy. I can get values from the apex code to be shown on a Visualforce page, that is ok, {get; set;} will do it. Now, the problem is that I need to find out if there is text in a field that it is part of a Visualforce page to help me with a "if statement" back in the controller code.

I have been trying to use something like this:

descPhotoOne = ApexPages.currentPage().getParameters().get('controller.customField__c');

I thought that by creating a string desPhotOne {get; set} I was going to be able to pickup the value within the customfield__c, but I was wrong. The code saved without errors, but when exposing the string descPhotoOne on my Visualforce and adding text to my customField__c, nothing happens.

So, what would be the best approach to get back to my controller the value entered in a custom field that is part of the visualforce page?. Or at least to know of his presence in that field (null , not null)?

I need to know if there is any value inside the custom field to use it on a if statement back on the controller... anyone can give me some directions on how to do this?

  • Hi. Please add in some more code (controller, VF page) so we can judge if this is e.g. a standard controller or not, which variables the VF page uses exactly, and what is the trigger on the page to pass the variable back to the controller.. – Guy Clairbois Apr 3 '14 at 13:12
3

Your code fragment is accessing the URL parameters rather than any values you might have in form fields which is why it does not work.

Visualforce automatically binds form fields to SObjects or primitives exposed by the controller. This is explained in articles like An Introduction to Visualforce. Here is an example of how this normally looks. The Visualforce:

<apex:page>
    <apex:form>
        ...
        <apex:inputField value="{!co.CustomField__c}"/>
        ....
    </apex:form>
</apex:page>

The Apex controller:

public with sharing MyController {

    // Controller code sets this custom object reference
    // When the page is first displayed the CustomField__c value comes from here
    // When the form is submitted, the CustomField__c value is automatically updated
    public CustomObject__c co {get; set;}

    // An action initiated from the Visualforce
    public PageReference save() {
        if (co.Custom_field__c) {
            ....
        }
    }
}
| improve this answer | |
0

Viasur, I think I have done something similar, in this case I have a html textbox and I pass the value via javascript to the class and the run a query based on the textbox value.Here is code I hope it helps!

  //VF page using javascript to pass values

   <script type="text/javascript">
  function doSearch() {
    searchServer(           
      document.getElementById("name").value);
  } 

   </script> 

 //text box
    <table>     
    <tr>
    <td>
    <input type="text" id="name" onChange="doSearch();"/>
    </td>
    </tr>
    </table>


  <apex:actionFunction name="searchServer" action="{!runSearch}" rerender="results,debug,errors"> 
      <apex:param name="name" value="" /> 
  </apex:actionFunction> 

 //this is in your class
 // runs the search with parameters passed via Javascript

 public PageReference runSearch() { 

  String name = Apexpages.currentPage().getParameters().get('name');

 soql = 'SELECT  Name, Product_Name__c, Account__c' +
     'FROM NRProducts__c  WHERE (Account__r.Name LIKE \'%'+String.escapeSingleQuotes(name)+'%\') OR (name LIKE \'%'+String.escapeSingleQuotes(name)+'%\') OR (Product_Name__c LIKE \'%'+String.escapeSingleQuotes(name)+'%\')';

   runQuery();

   return null;  
  }

  // here I store the query values
  public List<NRProducts__c> products {get;set;}

  public void runQuery() {

  products = Database.query(soql) 
} 
| improve this answer | |
  • Thank guys for your replies. Carlos, I can use the <apex:param>, but based on the documentation only certain components are supported and inputField is not one of them :(. Keith, thanks for clarifying your initial point. I did read that information from your first entry but I couldn't find a solution based on that. In your last explanation things were looking much better. It is exactly my intention but after clicking on Save I get an error: "Attempt to de-reference a null object Error is in expression '{!save}' in component <apex:commandButton> in page" – Carlos Naranjo Apr 4 '14 at 9:03
0

I finally got it.

public with sharing class MyController{
    public CustomObject__c vr = new CustomObject__c();

    public MyController(ApexPages.StandardController controller) {
        this.vr=(CustomObject__c)controller.getRecord();
    }

    // Now I can reach to any field that are part of the CustomObject__c.
    if(vr.CustomField__c ==null){
        //Your code here
    }

On my Visualforce page all I need to do is to use {!vr.Custom_Field__c}.
For me it works!! Thanks all for your inputs and for taking the time to help me, it is much appreciated, really.

| improve this answer | |
  • This is pretty much an unformatted version of the code 'Keith C' posted yesterday. – dphil Apr 4 '14 at 19:05
  • Yes, thanks to Keith C I was able to get closer to what I needed, no doubts about it.Keith gave me the right direction. Thanks Keith. – Carlos Naranjo Apr 5 '14 at 9:27

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.