0

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;
    }

}
5
  • Where are you planning to save all the revenue inputs? Can you show us some controller code as well? Apr 8, 2019 at 7:08
  • done @BrianMiller
    – kratos
    Apr 8, 2019 at 12:07
  • Thanks for the update, but I still don't see where you're trying to save/store the Revenues. Do you have a custom field on the Opportunity for these revenues? Are you trying to add them all together before saving? Apr 8, 2019 at 13:01
  • Exactly @BrianMiller. I am going to add a text field to opportunity and attempt to serialize the fields and set the text field to that. Is this is a viable approach?
    – kratos
    Apr 8, 2019 at 13:57
  • The database-type approach to this would probably be to make a child object and create one child record for each input (and relate them to your target Opportunity). Would that be a workable solution for you?
    – Derek F
    Apr 8, 2019 at 15:31

0

You must log in to answer this question.

Browse other questions tagged .