[SalesForce] Entering a Date into a Visualforce page and passing it along to a new Record

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"/>

</apex:pageBlockSectionItem> 

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.

Best Answer

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!).

Related Topic