I have a visual-force page which displays an Apex form with some dynamic number of input text fields that I would like to save on the Opportunity object. Is the easiest way to serialize them as JSON and store on one textarea field? And then on page load use javascript to deserialize and populate the fields? Is there a better faster approach?
My visual force code that shows the form: (I want to save the Revenue input fields)
<apex:form >
<div class="columns">
<div class="column">
<div class="field">
<label class="label">Deal Amount</label>
<div class="control">
<apex:inputField value="{! Opportunity.Amount }" type="text" style="width:200px" />
</div>
</div>
</div>
<div class="column">
<div class="field">
<label class="label">Project Start Date</label>
<div class="control">
<apex:inputField value="{! Opportunity.Project_Start_Date__c }" type="date" showDatePicker="false" style="width:200px"/>
</div>
</div>
</div>
<div class="column">
<div class="field">
<label class="label">Project End Date</label>
<div class="control">
<apex:inputField value="{! Opportunity.Project_End_Date__c }" type="date" showDatePicker="false" style="width:200px"/>
</div>
</div>
</div>
<div class="column">
<div class="field">
<label class="label">Actions</label>
<div class="control">
<apex:commandButton action="{!quicksave}" value="Save" onclick="test();" styleClass="button is-light"/>
</div>
</div>
</div>
</div>
<table class="table">
<thead>
<tr>
<th><abbr title=""></abbr></th>
<apex:repeat value="{!MonthAndYearBreakdownByProjectStartAndEndDates}" var="col">
<th><abbr title="{!col}">{!col}</abbr></th>
</apex:repeat>
</tr>
</thead>
<tfoot>
<tr>
<th><abbr title=""></abbr></th>
<apex:repeat value="{!MonthAndYearBreakdownByProjectStartAndEndDates}" var="col">
<th><abbr title="{!col}">{!col}</abbr></th>
</apex:repeat>
</tr>
</tfoot>
<tbody>
<tr>
<th>Revenue</th>
<apex:repeat value="{!MonthAndYearBreakdownByProjectStartAndEndDates}" var="col">
<td>
<div class="field">
<div class="control">
<input class="input is-small" type="text" placeholder="Revenue" id="{!col}"/>
</div>
</div>
</td>
</apex:repeat>
</tr>
</tbody>
</table>
</apex:form>
My controller extension:
public class revenueForecastExtension {
private final Opportunity oppor;
// The extension constructor initializes the private member
// variable acct by using the getRecord method from the standard
// controller.
public revenueForecastExtension(ApexPages.StandardController stdController) {
this.oppor = (Opportunity)stdController.getRecord();
}
public Set<String> getColumnContent() {
Set<String> columnContent = new Set<String>();
columnContent.add('<input id="theTextInput" type="text" name="theTextInput" />');
return columnContent;
}
public Set<String> getMonthAndYearBreakdownByProjectStartAndEndDates() {
Date d1 = oppor.Project_Start_Date__c;
Date d2 = oppor.Project_End_Date__c;
Map<Integer,String> monthNameMap=new Map<Integer, String>{1 =>'January', 2=>'February', 3=>'March', 4=>'April', 5=>'May',
6=>'June', 7=>'July', 8=>'August', 9=>'September',10=>'October',
11=>'November', 12=>'December'};
Set<String> monthYearSet = new Set<String>();
List<String> monthYearList = new List<String>();
while(d1 <= d2)
{
monthYearList.add(monthNameMap.get(d1.Month()) + ' ' + d1.Year());
d1 = d1.AddDays(1);
}
monthYearSet.addall(monthYearList);
return monthYearSet;
}
public PageReference quicksave() {
update oppor;
PageReference orderPage = new PageReference('/apex/revenueForecast?&id='+oppor.Id);
orderPage.setRedirect(true);
return orderPage;
}
}