0

I have a field on a VF page when a user insert a value in that field and click a button, that value has to be pass to a variable inside the controller extension. The input field is:

<apex:inputField required="true" id="prodotto" value="{!Prodotto_contratto__c.Scelta__c}"/>

the button is:

<apex:pageBlockButtons location="top">
                <apex:commandButton action="{!addFromShoppingCart}" value="Aggiungi al Contratto" reRender="pselected,pinserted" />
            </apex:pageBlockButtons>

Now in the controller extension I have:

public Id prodotto{get;set;}

but I can't get the value inside the void function:

public void addFromShoppingCart(){
    Prodotto_contratto__c pr=new  Prodotto_contratto__c();
    pr.Name='prodotto in contratto';
    pr.Scelta__c=prodotto;
        System.debug(pr.Scelta__c);
    insert pr;
    System.debug('prodotto '+pr.id);    
    }

infact the system.debug value is null. Please Help me, I'm becoming crazy on this.

2

To use you have to bind sObject fields with it. In your case you can declare an instance of type Prodotto_contratto__c and bind it's field with UI. Then you can access them in the controller.

<apex:inputField required="true" id="prodotto" value="{!prodotto.Scelta__c}"/>

Declare an object type of Prodotto_contratto__c

public Prodotto_contratto__c prodotto{get;set;}

Access prodotto fields as usual

public void addFromShoppingCart(){
    System.debug(prodotto.Scelta__c);   //this will give you the user input value       
}
1

If the field is a lookup and you want to keep the salesforce functionality (search for record). You can also use:

Prodotto_contratto__c pr { get; set; }
pr=new  Prodotto_contratto__c();

And then use the field directly:

<apex:inputField required="true" id="prodotto" value="{pr.Scelta__c}"/>
1

The value of the inputfield should match the field in the controller that you want to set. So

<apex:inputText required="true" id="prodotto" value="{!prodotto}"/>

will populate the controller field

public String prodotto{get;set;}
  • Can you bind string type class variables with <apex:inputField /> ? – highfive Jul 15 '16 at 9:56
  • good point - should probably be inputText – Doug B Jul 15 '16 at 9:58

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.