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
hamza akouayrihamza akouayri 

Trigger Start date End date

Hello All,
I have two custom objects. project_c and week_c 
In Project_c.object  i have fileds start date and end date 
I want to create a trigger that will create records of weeks in week_c object based on start date and end date 
For example:
---------Project---------
start date : 10/02/2020
end date : 27/02/2020
----------week----------
week 9Feb
week 16Feb
week 23Feb
*PS (first day of the week will be sunday )

Thanks in Advance
 
PRAKASH JADA 13PRAKASH JADA 13
Hi,


Trigger:
-------------------
trigger ProjectTrigger on Project__c (before Insert) {
    if(Trigger.isBefore) {
        if(Trigger.isInsert) {
            ProjectHandler.onBeforeSave(Trigger.New);
        }
    }
}


Apex class:
--------------------------

public with sharing class ProjectHandler {
    public static void onBeforeSave(List<Project__c> projects) {
        
        List<Week__c> newWeeks = new List<Week__c>();
        
        // Loop to iterate over the list of Project records
        for(Project__c project : projects) {
            
            //Date that accepts the Start Date
            Date startDate =  project.Start_Date__c ;
            System.debug('Date of Start : ' + startDate);
            
            // It will get the start date of the week based on Date so here it is 2020 02 09
            StartDate = StartDate.toStartofWeek();
            System.debug('Start of week for Start Date : ' + startDate);
            
            Date endDate = project.End_Date__c;
            System.debug('Date of EndDate : ' + EndDate);
            
            // It will get the start date of the week based on Date so here it is 2020 02 09
            endDate = endDate.toStartofWeek() + 7;
            System.debug('Start of week for end Date : ' + endDate);
            
            while(startDate < endDate) {
                Week__c week = new Week__c();
                
                DateTime dt = Datetime.newInstanceGmt(startDate.year(), startDate.month(), startDate.day()) ;
                week.Name = String.valueOf(startDate.day())  + dt.format('MMM');
                System.debug(week.Name);
                startDate = startDate+7;
                newWeeks.add(week);
            }
            
        }
        
        try {
            if(!newWeeks.isEmpty()) {
                insert newWeeks;
            }
            
        }catch(DMLException e) {
            System.debug('Unable to insert the weeks : ' + e.getMessage());
        }
    }
}

Input:
-----------------------
Start Date:
Feb 10, 2020
End Date:
Feb 27, 2020

Output: 
---------------------
9Feb
16Feb
23Feb

I hope this helps.

Thanks,
 
hamza akouayrihamza akouayri
Thanks Prakash, 
but i still face issue when i try to update the recorde it create a dupliaction of weeks 
Example
Project level
startDate: 02/09/2020 endDate: 02/25/2020
weeks level
9 Feb
16 Feb
23 Feb
updated Example startDate: 02/09/2020 endDate: 03/03/2020
weeks level
9 Feb
16 Feb
23 Feb
9 Feb
16 Feb
23 Feb
1 Mar
instead
9 Feb
16 Feb
23 Feb
1 Mar
 
PRAKASH JADA 13PRAKASH JADA 13
check for the trigger events for updates. My code will work only for the create records not for the updates because there is no logic over the above code but if you have any logic related to that please add checks to that logic.
hamza akouayrihamza akouayri
so i should add a logic for update in handler class and call it from trigger right ?? 
PRAKASH JADA 13PRAKASH JADA 13
Do you have already logic on the update?
PRAKASH JADA 13PRAKASH JADA 13
when you update the record it is creating the new records meaning to say you have logic on update. so please check if the week the difference is same for old and new values of start date and new date if yes you can ignore them. if the difference is more than the previous values then you need to create the weeks only for the extended week not for the existing ones in that way you can eliminate the duplicates.
hamza akouayrihamza akouayri
no i don't have update logic 
PRAKASH JADA 13PRAKASH JADA 13
can you please set the debug log to see how those records are creating on update. we don't have any update logic in trigger. so you need to know from where the update logic is firing to create the weeks.
hamza akouayrihamza akouayri
Actually i have add after update event  to the trigger but i have used same logic as insert that's why it is casuing a duplication there! 
now i think i have to create a logic for update than add update event to the trigger 
 
PRAKASH JADA 13PRAKASH JADA 13
Exactly update logic is different from insert. Insert is straight forward but update logic is not you need to check before you insert. I added how you can insert on update. I hope that helps you to avoid duplicates.