1

In my leave management module i am applying error messages for below conditions-

if(w.le.Start_Date__c >w.le.End_Date__c){
   counter++;                    
   ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,'End Date should be greater than or equal to Start Date'));
}
if(w.le.End_Date__c<system.TODAY()){
   counter++;                    
   ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,'End Date should be greater than or equal to Today'));
}
if(w.le.Start_Date__c <system.TODAY()){
   counter++;                    
   ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,'Start Date should be greater than or equal to Today'));   
}

what if i dont want my user to select in between dates of already submitted start date and end date. Suppose i applied leave for start date: 19/01/2017 to end date: 21/01/2017 Then again if i apply for leave, error message should display if i choose start date: 18/01/2017

What should i do for this?

1
  • you will first need to fetch the leaves already in the system. Then compare the new leave with those existing leaves. Jan 19, 2017 at 13:14

2 Answers 2

1

The simplest way to enforce that date ranges do not overlap is to abuse Custom Types in Map Keys and Sets. If you have every instance of your key type return a hashCode of 1, then you can use the equals function to evaluate overlap.

Here's a minimal script you can run via Execute Anonymous to demonstrate the concept:

class DateRange
{
    final Date startDate, endDate;
    DateRange(Date startDate, Date endDate)
    {
        this.startDate = startDate;
        this.endDate = endDate;
    }
    public Integer hashCode() { return 1; }
    public Boolean equals(Object instance)
    {
        DateRange that = (DateRange)instance;
        Boolean containsStartDate = that.startDate >= startDate &&
            that.startDate <= endDate;
        Boolean containsEndDate = that.endDate >= startDate &&
            that.endDate <= endDate;
        return containsStartDate || containsEndDate;
    }
}

Set<DateRange> ranges = new Set<DateRange>
{
    new DateRange(
        Date.newInstance(2017,1,17),
        Date.newInstance(2017,1,21)
    )
};

system.assert(ranges.contains(new DateRange(
    Date.newInstance(2017,1,18),
    Date.newInstance(2017,2,1)
)));

So you can incorporate a key type like this DateRange class to enforce that none of your leave ranges overlap.

0

You'll want to create a boolean test condition along the lines of the following where old.w.le.Start_Date__c and old.w.le.End_Date__c represent the existing values for the Start and End dates.

if((w.le.Start_Date__c > old.w.le.Start_Date__c) && (w.le.Start_Date__c < old.w.le.End_Date__c)) {

ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,('New Start Date cannot be between Existing Start Date and End Date'))
}

if((w.le.End_Date__c > old.w.le.Start_Date__c) && (w.le.End_Date__c < old.w.le.End_Date__c)) {
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,('New End Date cannot be between Existing Start Date and End Date'))
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .