0

I am trying to store value in an object using:
<apex:inputField type="number" value="{!VS.Vegetable_Amount__c}" id="v0" />

in the controller I am doing this:

public Expense_Object__c VS {get;set;}
public PageReference saveAndReturn()
{
        system.debug('Value is:'+VS.Total_Amount__c);
}

I am trying to fetch value here but neither data is getting inserted nor I am getting any value?

2
  • Quick tip: Stackexchange sites don't render things that look like HTML (i.e. Visualforce) unless they're formatted as code (either inline code, or a code block). You should take a look at the formatting page in the help center so you can avoid this problem in the future. Proper formatting and indentation also makes things easier to read, and the fewer barriers you introduce between your problem and someone trying to understand it and help, the better it'll be for you.
    – Derek F
    Commented Jan 26, 2019 at 14:34
  • ok @DerekF.Can anyone please hep its urgent? Commented Jan 26, 2019 at 14:47

3 Answers 3

2

Not much code to go on, but a quick guess: you need to create an instance of Expense_Object__c and assign it to VS in the controller's constructor. Currently, it looks like VS is null.

public with sharing class YourClassName {

    public Expense_Object__c VS {get;set;}

    public YourClassName() {
        // create an expense object instance to use
        VS = new Expense_Object__c();
    }

    public PageReference saveAndReturn()
    {
        system.debug('Value is:'+VS.Total_Amount__c);
    }
}
1
  • ok let me try and get back Commented Jan 26, 2019 at 15:58
0

There are two things you need to check/correct

  • You have to create an instance of Expense_Object__c object and assign it to VS variable.
  • You are using Vegetable_Amount__c in the input field and you are checking the value of Total_Amount__c in debug log.

Note: There is no insert statement in your controller.

2
  • Thanks for the reply,Did the changes as below: public with sharing class SaveAndReturnController { public Expense_Object__c VS {get;set;} public SaveAndReturnController() { VS = new Expense_Object__c(); } public PageReference saveAndReturn() { system.debug('Value is:'+VS.Vegetable_Amount__c); //I am saving after this } } But getting error on preview of VF attempt to de-refernce a null object. Any suggestion? Commented Jan 26, 2019 at 19:29
  • I am able to achieve it Thank you all for the help.Let me post the Answer. Commented Jan 26, 2019 at 20:16
0
public Expense_Object__c VS1 {get;set;}//This I was using already
public SaveAndReturnController(ApexPages.StandardController controller)//This 
is new
{
 VS = new Expense_Object__c();
 this.controller = controller;        
}
public PageReference saveAndReturn()
{ 
system.debug('X is:'+VS.Vegetable_Amount__c);
PageReference cancel = new 
PageReference(ApexPages.currentPage().getHeaders().get('Referer'));
insert VS;//introduced this insert
//controller.save(); I was using this previously so commented
return cancel;      
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .