This code is able to upsert a single record in salesforce. I want it to save multiple records by breaking the long string of dates contained in the datesFromCalendar
. datesFromCalendar
contains data like this [5]|DEBUG|01/13/2015,01/20/2015,01/27/2015,02/03/2015,02/10/2015,02/17/2015,02/24/2015,03/03/2015,03/10/2015,03/17/2015,03/24/2015,03/31/2015,04/07/2015,04/14/2015,04/21/2015,06/10/2015
. Each date should be a separate record in salesforce with all the other information intact and one random unique id
with each set of records. For example above mentioned dates will have same unique id(any random string generated within the controller) but each should have to be a separate record in salesforce. Below is the controller which is saving all dates as a string.
public with sharing class EventsPageController {
public Event__c Event{
get {
if (Event == null)
Event = new Event__c();
return Event;
}
set;
}
public String datesFromCalendar {get; set;}
public List<String> eachDt = new List<String>();
public void EventsPageController() {
Event = [SELECT Id, EventDates__c,Event_Description__c,Event_Type__c,Maximum_Attendees__c,Occurrence__c,Program_Name__c FROM Event__c WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
}
public Event__c getEvent(){
return Event;
}
public String[] eventDates(){
list<String> EventDates = new list<String>();
list<Event__c> eventList = [SELECT Id, EventDates__c FROM Event__c WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
for(Event__c event : eventList) {
EventDates.add(event.EventDates__c);
}
return EventDates;
}
public PageReference saveData(){
System.debug(datesFromCalendar);
try {
Event.EventDates__c = datesFromCalendar;
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();
}
}
dates
are coming from JS variable & assigned todatesFromCalendar
in controller. I've an idea to split each date and assign it to an array. Then loop over array and do some logic of inserting/upserting multiple records.