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
adroitusadroitus 

Public Holiday Formula needed for leave application

Hello Developer.

 

  •      i want to know how to exclude public holiday from business day if someone apply for a leave. Done with weekend days exclusion.

 

              i need u r help....

 

 

Thanks & Regards

Ashish Agrawal

    

Best Answer chosen by Admin (Salesforce Developers) 
apaiapai

Create Holidays. Admin Setup --> Comapany Info --> Holidays

 

Once you have configured the holidays, Write an Apex trigger (before insert) for the Custom object to check for holidays and add the other business logic to exclude that day, whenever a new leave application is submitted.

All Answers

apaiapai

Create Holidays. Admin Setup --> Comapany Info --> Holidays

 

Once you have configured the holidays, Write an Apex trigger (before insert) for the Custom object to check for holidays and add the other business logic to exclude that day, whenever a new leave application is submitted.

This was selected as the best answer
adroitusadroitus

Hello,

 

           Thanks for u r Hint....

 

 

 

  •  I have configured the Holiday Date from Admin setup-->company profile--->Holiday
  • But now need some sample code how to check for holidays and i am new in  Apex.

 

PLz help me...

apaiapai

Are you using a custom object to manage leave applications ?

 

Can you please provide the details of your use case...

adroitusadroitus

Hello,

 Yes i am using custom objects for leave apllications.

 

 

details:

managed_Appsamp__Start_Date__c(for start date)

managed_Appsamp__End_date__c(for end date)

managed_Appsamp__leave_days__c(number of days will stored in this field)



need a sample code /formula for this .

apaiapai

ok.

 

Let me verify that I understand your use case correctly...

 

Based on the leave start date and end date, you first check for weekends, then holidays and finally, the number of days is calculated and stored in the managed_Appsamp__leave_days__c field.

 

 

adroitusadroitus

YES ,well understood my need....

apaiapai

something quick : this will work if the year is same... for instance if you have start date as dec 20, 2011 and end date as Jan10, 2012... you will have to add some business logic for that........

 

 

trigger trgLeaveAppBeforeInsert on <Custom Object name>(before insert) {
       
    Date startDate, endDate;
    Integer startDayofYear, endDayofYear, totalDaysFinal,totalDaysInitial;
    if(trigger.isInsert){
        for(<Custom Object> obj : Trigger.new){
           
           
            startDate = obj.managed_Appsamp__Start_Date__c;
            endDate = obj.managed_Appsamp__End_date__c;
           
            if(startDate.year() == endDate.year()){
               
                totalDaysInitial = endDate.dayOfYear() - startDate.dayOfYear();
                totalDaysFinal = totalDaysInitial;
               
                for(integer i = 0; i< totalDays; i++){
                   
                    for (Holiday hObj : [Select activityDate from Holiday]){
                       
                        if(startDate.addDays(i) == activityDate){
                           
                            totalDaysFinal--;
                        }                       
                    }
                }
            }
            obj.managed_Appsamp__leave_days__c = totalDaysFinal;
           
        }

  }
}

adroitusadroitus

hello !!!

 

Error!!!

Error: Compile Error: Variable does not exist: activityDate at line 22 column 52

apaiapai

sorry.. it should be

 

if(startDate.addDays(i) == hObj.activityDate){

adroitusadroitus

Thanks a lot APAI i have done with it and its working fine..

 

 

 

Thank u Once Again for u r gr8 Support

 

 

 

Thanks & Regards

Ashish Agrawal

Salesforce CRM

apaiapai

Good to know that it works ! :-)

 

A

Sangharsh KambleSangharsh Kamble
Hello, everyone, I am working on a leave management project I want to make many too many relationships so I am confused about which object I make.
Anjali Gupta 110Anjali Gupta 110
Hello everyone, can someone help me how can i manage Weekends in my leave management app. i want to make a seperate custom object for that.
Thanks in advance
 
sreekar kumarsreekar kumar
No need to make seperate object for holidays make a for loop to exclude the week ends in your trigger.
 
sreekar kumarsreekar kumar
Holidays means weekends as per your
requirement