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.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 yoursaveCustomAttachment()
method, you'll likely run into issues (a variable redeclaration error, or perhaps the date being overwritten).To tie this all together, we need to update the binding that you're using in your Visualforce page
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 theDocument_Date__c
field of yourobjBVar
object, which will have the value from your input field after you presssave
(and not a moment before that!).