Apex Code for Creating Recurring Event in Salesforce Lightning

In this article, I am sharing my approach and code required for fetching days for recurring events in Salesforce Lightning.  The use cases could be creating a recurring pickup, order placement etc. You would need basic knowledge of Apex and Salesforce Lightning.

This screen will give you an idea about the recurring UI.

.Capture rec









Scenario: –

User can select a “Start Date”, “End Date”, “Recurring weeks” and “Days”. Now according to selected fields, the record will get recur in future.

Step1: – We need to create an auraEnabled apex method to fetch dates between the “Start Date” and “End Date”.

Apex Method.


public static Integer getDays(string startDate ,string endDate){

integer noOfDays= date.valueOf(startDate).daysBetween(date.valueOf(endDate));

return noOfDays;


Step2: – Now in lightning component controller we need to store that dates accordingly.

Lightning controller.

getDates: function(component, event, helper){

        var startDate = component.find(“dateField”).get(“v.value”);

        var endDate = component.find(“dateFieldend”).get(“v.value”);

        var recurWeek = component.find(“week”).get(“v.value”);

        var recurWeek1 = (Number(recurWeek)+1)*7;

        var startDate1 = new Date(startDate);

        var endDate1 = new Date(startDate);


        var selectedDays = [“”,””,””,””,””,””,””];

        var checkboxes=component.find(“check”);

        for(var c in checkboxes){


                var dayvalue = checkboxes[c].get(“v.value”)




        alert(“startDate == “+startDate1+” endDate == “+endDate1+” recurWeek == “+recurWeek1+” selectedDays == “+selectedDays);

        var startDay = startDate1.getDay();

        var dateArray = [];

        var finaldates = [];

        var action=component.get(“c.getDays”);




                var noOfDays =response.getReturnValue();

                for(var i=startDay;i<noOfDays;i++){

                    if(startDay == selectedDays[i]){



                    else if(i%7==selectedDays[i%7] && selectedDays[i%7]!= “”){

                        var date = new Date(startDate1.setDate(startDate1.getDate()));



                    startDate1.setDate(startDate1.getDate() + 1);







 It is that simple, isn’t that? Feel free to send me your questions by adding comments to this blog.

Posted in Apex Controller, Apex Development, force.com app development, salesforce customization, salesforce development. Tagged with , , , .

Leave a Reply

Your email address will not be published. Required fields are marked *