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
Devendra@SFDCDevendra@SFDC 

Trigger to calculate End Date after adding No_Of_Days to Start Date

 

Hi,

 

I have written below trigger to calculate due date based on addition of no of days to start date(excluding Saturday and Sunday)

 

trigger calculateDueDate on Job__c (before insert,before update) {
    for (Job__c j : Trigger.New)   

{        if(j.Start_Date__c != null && j.No_of_Days__c != null)        {            DateTime st = j.Start_Date__c;            double d=j.No_of_Days__c;            Integer i=d.intValue();            DateTime et = st.addDays(i);                 while(st<et)            {                Integer count=0;                if(st.format('E')=='Sat' || st.format('E')=='Sun')                {                    count=count+1;                }                st=st.addDays(1);            }       }   } }

 

In the above code, how can i add count to variable et?

 

Regards,

Devendra S

Best Answer chosen by Admin (Salesforce Developers) 
kritinkritin


Datetime dtStartDate=system.today();
Date dtDueDate;
integer DueDate=5;

 

integer jCount=0;

while(true){

 if(dtStartDate.format('E')!='Sat' && dtStartDate.format('E')!='Sun'){
  
  
  jCount=jCount +1;
  if(jCount>DueDate){
   break;
  }
  system.debug('jCount======' + jCount);
  system.debug('==========' + dtStartDate);
  
  system.debug('===========' + dtStartDate.format('E'));
  dtStartDate=dtStartDate.addDays(1);
  
  
  
  
 }else{
  system.debug('==========' + dtStartDate);
  //system.debug('===========' + dtStartDate.format('E'));
  dtStartDate=dtStartDate.addDays(1);
  continue;
  
 
 }
}

system.debug('============' +dtStartDate);

 

 

 

 

Please see the above logic it will help you.

All Answers

kritinkritin


Datetime dtStartDate=system.today();
Date dtDueDate;
integer DueDate=5;

 

integer jCount=0;

while(true){

 if(dtStartDate.format('E')!='Sat' && dtStartDate.format('E')!='Sun'){
  
  
  jCount=jCount +1;
  if(jCount>DueDate){
   break;
  }
  system.debug('jCount======' + jCount);
  system.debug('==========' + dtStartDate);
  
  system.debug('===========' + dtStartDate.format('E'));
  dtStartDate=dtStartDate.addDays(1);
  
  
  
  
 }else{
  system.debug('==========' + dtStartDate);
  //system.debug('===========' + dtStartDate.format('E'));
  dtStartDate=dtStartDate.addDays(1);
  continue;
  
 
 }
}

system.debug('============' +dtStartDate);

 

 

 

 

Please see the above logic it will help you.

This was selected as the best answer
Devendra@SFDCDevendra@SFDC

 

 

Thanks a lot Kritin.

 

Cheers,

Devendra S