+ Start a Discussion

trigger to add consecutive due dates

Hi Friends,

I have a requirement when I need to create records based on dates with incrementl due dates.

I have a custom obj with start date and end date fields. After saving the record I want to create a records in Child Object with due date as incremental dates.

Eg: My Start Date is 05-03-2020 and End Date is 10-03-2020.
Date Difference is 6 days. I will create 6 records with different due dates with next date 
1st record Due Date - 06-03-2020. 2nd Record Due Date - 07-03-2020.

Below is the code for creating dates but I could not able to do incremental due dates.

rigger insertDailyPractices on Study_Plan__c (after insert) 
    System.debug('--- Inside Trigger ---');
    List<Daily_Practice__c> dplist = new List<Daily_Practice__c>();
    for(Study_Plan__c sp : Trigger.New)
        Integer daycount = sp.Phase_Start_Date__c.daysBetween(sp.Phase_End_Date__c);
        System.debug('--- Inside For Loop ---' + sp.Phase_Start_Date__c);
        System.debug('--- Day Count ---' + daycount);
        for(integer i=0; i<=daycount; i++)
            Daily_Practice__c dps = new Daily_Practice__c();
            dps.Due_Date__c = sp.Phase_Start_Date__c + 1; // I have given currently as stratdate + 1.
            dps.StudyPlan__c = sp.Id;
            dps.Status__c = 'Assigned';
    if(dplist.size() > 0)
        insert dplist;
    system.debug('--- Inserted Daily Practice ---'+ dplist.size());

Need your assistance to complete.

Christan G 4Christan G 4
Hi HTANIRS, I have one question regarding your requirement. Will the different due dates always be a month apart from each other? I am inquiring since, in your example, you said the date difference is 6 days rather than 6 months. Also, did you mean 5 months rather than 6? Thanks in advance!
Hi Christan,

Yes. It is days only.
I need to create a records for daily assessment based on start date and end date. It is not different months.

Another Example: Start Date : 06-03-2020 and End Date : 09-03-2020.
Date difference is 4.
I need to create 4 record's with due dates as 
07-03-2020, 08-03-2020, 09-03-2020, 10-03-2020.