I am attempting to capture a date input from a VF page and then passing it onto a newly created record. All I want to do is bring up the VF page with a custom button in one object (objectA) and use it as a means of input for creating a record in another object (objectB). I am using what would be a blank Date field from objectA as the means for input for the date. I then just want to capture that input in a controller and pass it into a field of the newly created record (objectB). I don't want to save the value of the input into the objectA.

I am doing this in order to get a proper looking data picker in the VF page.

Here is my current VF page :

<apex:pageBlockSectionItem >

  <apex:inputfield id="theDateInput" value="{!objbvar.Document_Date__c}" required="false"/>


And I am attempting to pass that input into a field for a newly created record with this controller :

public class UploadControllerVendorProdRev {

public Date thedateinput {get;set;}
private objectA__c parent {get;set;} 

public objectB__c objbvar{get;set}
public UploadControllerVendorProdRev(ApexPages.StandardController controller) { 
    this.parent = (ObjectA__c)controller.getRecord();

  private Database.SaveResult saveCustomAttachment() {

    ObjectB__c  obj = new ObjectB__c();
    obj.Parent __c = parent.Id; 
    obj.Document_Date__c = thedateinput ;

    return Database.insert(obj);        

As it is now I am not capturing a value. I am only creating the ObjectB record with a blank Document_Date__c field.

I wanted to include the <***NOT USING THIS line just to show what I originally was doing with this before I decided to use an inputfield from ObjectA as a means to have user input, even though I am not saving that input to ObjectA, only trying to pass it onto the new ObjectB record.

Thank you very much for any help.

2 Answers 2


The code that you initially provided is going about things the hard way.

My preferred method of taking care of this would be to make ObjectB__c into a separate variable in your controller, and then use that in the visualforce binding.

public class UploadControllerVendorProdRev {

    // This member variable, thedateinput, won't be needed
    // public Date thedateinput {get;set;}

    private objectA__c parent {get;set;}

    // Declare an ObjectB__c variable to be used later
    public objectB__c objBVar{get;set}

    public UploadControllerVendorProdRev(ApexPages.StandardController controller) { 
        this.parent = (ObjectA__c)controller.getRecord();

        // Here, in your controller's constructor, we need to initialize
        //  objBVar. If it isn't initialized here, you'd probably get a 
        //  NullPointerException when you attempt to reference it in your page

        // We can also set the value of Parent__c in the constructor for ObjectB__c
        objBVar = new ObjectB__c(Parent__c = this.parent.Id);

Because we're taking care of the initialization in the controller's constructor, you don't need to do it in your saveCustomAttachment() method any more. In fact, if you leave that initialization code in your saveCustomAttachment() method, you'll likely run into issues (a variable redeclaration error, or perhaps the date being overwritten).

private Database.SaveResult saveCustomAttachment() {

    // This initialization is taken care of in the controller's constructor
    //objBVar = new ObjectB__c();

    // This line was moved into the call to the constructor for ObjectB__c
    //   (as a parameter).
    // This line is no longer needed either
    //objBVar.Parent __c = parent.Id; 

    // We can get rid of this line by changing your Visualforce to use
    //   {!objBVar.Document_Date__c}
    // Trying to use 'thedateinput' would cause any Date stored in 
    //   objBVar.Document_Date__c to be overwritten.
    //objBVar.Document_Date__c = thedateinput ;

    // In the end, this should be the only remaining line in this method
    return Database.insert(obj);    

To tie this all together, we need to update the binding that you're using in your Visualforce page

<apex:inputfield id="someId" value="{!objBVar.Document_Date__c}" required="false"/>

The way this works is that Salesforce will first attempt to find an objBVar in the standard controller. When it doesn't find such a variable in the standard controller, it looks for the variable in your custom controller (or controller extension).

Since we declare objBVar as public, Salesforce has no problem finding it. The input field is then bound to the Document_Date__c field of your objBVar object, which will have the value from your input field after you press save (and not a moment before that!).


There are two ways you can handle it.

  1. use the apex variable "thedateinput" in VF

and then copy its value in ObjectB in Apex.

obj.Document_Date__c = thedateinput ;
  1. Create a ObjectB variable and expose it on VF page


public ObjectB__c objBVar{get;set;}


<apex:inputfield id="someId" value="{!objBVar.Document_Date__c}" required="false"/>

and then use it to Insert data in DB

  • 1
    Ah, you sniped me. Using #2 would be my preferred approach. I do think you would need to initialize objBVar in the constructor. OP could also set Parent__c in the constructor as well, and use clone() if inserting multiple ObjectB__c records are required.
    – Derek F
    Aug 15, 2016 at 16:44
  • 1
    Missing piece of part "1": <apex:input type="date" id="someId" value="{!thedateinput}" required="false"/>
    – mritzi
    Aug 15, 2016 at 16:45
  • @mritzi - Thank you very much for your help. I tried to go with your #1 method. I think it is essentially what I had in my "NOT USING THIS" line above but you put in a value="{thedateinput}". But I am getting a (very vague) "Syntax Error" when trying to use it. I updated my original post to show where I am putting it. I took out the two inputfield lines when applying it so imagine that those are gone. But even if I can get it to work, won't it show up as an odd looking data picker ? (not a typical data input field ?).
    – Zoom_v
    Aug 15, 2016 at 17:03
  • 1
    @Zoom_v by constructor I mean inside your controller's constructor public UploadControllerVendorProdRev(...). As for referring to an instance of objectB__c, as long as your controller (or controller extension) has a public member variable (or at least a public getter), Salesforce will be able to work it out. Using a custom controller (or controller extension) means that you aren't solely limited to visualforce bindings like {!ObjectA__c.<fieldname>}
    – Derek F
    Aug 15, 2016 at 17:22
  • 1
    @Zoom_v You're getting there, but you seem to have ignored my advice of initializing objBVar in your constructor. You need to take the initialization out of saveCustomAttachment(). Also, you need to completely remove obj.Document_Date__c = thedateinput ; as the field will be set by the <apex:inputField> in your visualforce.
    – Derek F
    Aug 15, 2016 at 18:10

You must log in to answer this question.

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