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/2‌​015,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;
    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) {
      return EventDates;

    public PageReference saveData(){
        try {
            Event.EventDates__c = datesFromCalendar;
        } catch(System.DMLException e) {
            return null;
        // After Save, navigate to the default view page:
        return (new ApexPages.StandardController(Event)).view(); 

  • Is it can be implemented it by separate the data, put in the list then upsert list?
    – unidha
    Feb 1 '15 at 3:35
  • @unidha dates are coming from JS variable & assigned to datesFromCalendar 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.
    – Superman
    Feb 1 '15 at 9:13

So first of all you have to do a split on the variable datesFromCalendar and you split it by comma ,

  List<Event__c> events = new List<Event__c>();
  List<String> dates = datesFromCalendar.Split(',');
  String randomId = generateRandomString(5);
  for(String date : dates){
       Datetime dt = Datetime.parse(date); //Convert the string to a date
       events.add(new Event__c(
          GeneratedId__c = randomId,
          Date__c = dt,
          Event_Description__c = Event.Event_Description__c,
          Event_Type__c = Event.Event_Type__c));
          //and so on for the rest of the data you want to keep the same. 
  insert events //Then you insert or upsert events

For the random id for a set of records you can generate a random string. So it will be the same for a set of dates and once inserted into the database you will also have a Salesforce unique Id.

  public static String generateRandomString(Integer len) {
    final String chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
    String randStr = '';
    while (randStr.length() < len) {
       Integer idx = Math.mod(Math.abs(Crypto.getRandomInteger()), 62);
       randStr += chars.substring(idx, idx+1);
    return randStr; 

Here is the split method in the documentation.

Here is the parse method in the documentation.

Here is the random string generator from an answer on salesforce stack exchange.

Hope it helps, let me know if you have any questions.

  • What about this data? 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')]; Will it be intact same with each record?
    – Superman
    Feb 1 '15 at 9:46
  • I'm little confused. randomId should be same for each batch of dates and the remaining data like Event_Description__c,Event_Type__c,Maximum_Attendees__c,Occurrence__c,P‌​rogram_Name__c also be the same for each date. The only thing that will be unique for each record will the Id that is an Autonumber.
    – Superman
    Feb 1 '15 at 9:51
  • Updated my answer. The randomId will be the same for this set of dates as I'm calling the method once and loop through the dates and assign the id to all of them. Feb 1 '15 at 9:57
  • Then you don't really need the random string generator, you should leave the auto-number in the standard Name field for your custom object and then just loop through the dates and make sure that you have the same data for each event. Feb 1 '15 at 10:58
  • 1
    I come from a C#/ASP.NET MVC background so most of the things I learned are from there. A good book is the Advanced Apex Programming advancedapex.com but I recommend reading it once you are comfortable with apex. Some good tutorials about VisualForce/Apex can be found here developer.salesforce.com/trailhead. Plus lots and lots of practice, it will come with experience and browsing salesforce stack exchange ... a lot. You are on the good track! Feb 1 '15 at 16:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.