[SalesForce] How to save custom object data in salesforce with Apex controller

Following is my custom object controller in Apex. I think it will work fine with the data coming from VF page. But what for the variable datesFromCalendar which contains the contents of JS array. How to save this variable in salesforce?

public with sharing class EventsPageController {
    public Event__c Event {get; private set;}
    public String datesFromCalendar {get; set;}
    public pagereference saveData(){
        System.debug(datesFromCalendar);
        try {
            upsert(Event);
        } catch(System.DMLException e) {
            ApexPages.addMessages(e);
            return null;
        }
        // After Save, navigate to the default view page:
        return (new ApexPages.StandardController(Event)).view();
    }
}

Following is the VF code.

<apex:form>
    <apex:pageMessages id="errors"/>
    <apex:pageBlock title="Event Scheduler" mode="edit">
    <apex:pageBlock title="Event Detail" mode="edit" id="criteria">
        <apex:inputField id="eventType" value="{!Event.Event_Type__c}" required="true"/>
        <select name="occurrence" id="occurrence">
            <option>Select Occurrence</option>
            <option value="0">One Time</option>
            <option value="7">Weekly</option>
            <option value="30">Monthly</option>
            <option value="15">Bi-Monthly</option>
        </select>

    <input type="text" name="event_start_date" id="from" placeholder="On or after" style="height:20px; width:200px;"/>
    <input type="text" name="event_end_date" id="to" placeholder="On or before" style="height:20px; width:200px;"/>
    <apex:inputField id="eventDescription" value="{!Event.Event_Description__c}"/>
    <apex:inputField id="maxAttendees" value="{!Event.Maximum_Attendees__c}" required="true"/>
    <apex:inputField id="progName" value="{!Event.Program_Name__c}" required="true"/>
    </apex:pageBlock>
    <apex:pageBlock title="Calendar" id="calendarBlock">
    <div align="right">
        <select id="selectedDates" style="visibility:hidden"></select>
        <button id="saveCalendar"><span>Save Calendar</span></button>
        <apex:inputHidden value="{!datesFromCalendar}" id="hiddenDatesCollector"/>
        <apex:actionFunction name="passDatesToController" action="{!saveData}" rerender="hiddenDatesCollector"/>
    </div>
    <div>
        <apex:commandButton id="saveEventBtn" value="Save Event" onclick="sendSelectedDates(); return false;"/>
    </div>
    <div id="myCalendar"></div>
    </apex:pageBlock>
    </apex:pageBlock>
</apex:form>

Best Answer

From what you describe you will just have to do this:

try {
   Event.Your_Field__c = datesFromCalendar;
   upsert(Event);
} catch(System.DMLException e) {
   ApexPages.addMessages(e);
   return null;
}

So just before doing that upsert, you have to set the field on the Event object. Unless the data in the JS Array is different and you want to split the various values from it? Or the user can pick multiple dates?

I made this assumption as you declared the variable datesFromCalendar as a String.

UPDATE

If you know there will always be a comma between the dates you could use the Split method on the String class. You will still keep the dates in the String variable and do this:

List<String> listOfDates = datesFromCalendar.split(',');

Then you can easily do a for loop on the list. You could also parse the String in to a DateTime data type by using on each date after the split this code:

DateTime dt = DateTime.parse( Your individual date here );
Related Topic