function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Raveendra VakaRaveendra Vaka 

How to split the Date and creating more than 14days Event record in SFDC?

How to create more than 14 days Event record?

EX: i want run 6 months continually, but Salesforce allow max 14day,so how to split the date and creating multipule events

Any body help me...
Thanks,
PuneetsfdcPuneetsfdc
try below code, this will work. Modify code according to your requirements.

public class EventCreator {
 
 Date startDate;
 Date endDate;
 List<Event> newEventList;
    public EventCreator(Date srtDate, Date eDate) {
        try {
            startDate = srtDate;
            endDate = eDate;
            newEventList = new List<Event>();
            createEvents();
            insert newEventList;
        }catch(exception e) {
           
        }
    }
   
    public void createEvents() {
        while(startDate <= endDate) {
            createEvent(startDate);
            startDate = startDate.addDays(15);
            system.debug(' == startDate1 == ' + startDate);
        }
    }
   
 @TestVisible
    private void createEvent(Date startDate) {
        Event newEvent = new Event();
        newEvent.Subject = 'Sf Discussion Meet up';
        newEvent.StartDatetime = startDate;
        if( startDate.daysBetween(endDate) >= 14){
            newEvent.EndDateTime = startDate.addDays(14);   
        } else {
            newEvent.EndDateTime = startDate.addDays(startDate.daysBetween(endDate));   
        }
        newEventList.add(newEvent);
    }
   
}
Raveendra VakaRaveendra Vaka
Thanks [Puneetsfdc]

How to split the Date?

 Suppose I given StartDate(Mar/27/2015) and EndDate(Mar/27/2016) duration is 1 year. So, how to split 1 year and create event records every 14day...
 
Ex: Like 365/14=26 Events
1-14(1st Event), 15-28(2nd Event)…
 
I hope you understand my logic?
 
PuneetsfdcPuneetsfdc
In that case you need to convert the StartDate and EndDate into Date first, Salesforce do not provide any method where
you can get Month as integer after passing string, we need to implement the logic for that.

a small change in the logic will work.



public static Map<String, Integer> monthMap = new Map<String, Integer>{ 'Jan'=>1,
                                                            'Feb'=>2,
                                                            'Mar'=>3,
                                                            'Apr'=>4,
                                                            'May'=>5,
                                                            'Jun'=>6,
                                                            'July'=>7,
                                                            'Aug'=>8,
                                                            'Sep'=>9,
                                                            'Oct'=>10,
                                                            'Nov'=>11,
                                                            'Dec'=>12};

// Accepting String parameters instead of Date
public EventCreator(String srtDate, String eDate) {
        List<String> srtMonth = srtDate.split('/');
        List<String> eMonth = eDate.split('/');
        
        startDate = Date.newinstance(Integer.valueOf(srtMonth[2]), monthMap.get(srtMonth[0]), Integer.valueOf(srtMonth[1]));
        endDate = Date.newinstance(Integer.valueOf(eMonth[2]), monthMap.get(eMonth[0]), Integer.valueOf(eMonth[1]));        
    
        system.debug(' StartDate => ' + startDate + ' EndDate => ' + endDate);        
        
        newEventList = new List<Event>();
        createEvents();
        insert newEventList;        
    }