List Return Status Based on Date

Introduction

In this guide, we will explore how to configure a List to return a specific status based on a date value compared to the current date. This functionality is useful for tracking and displaying statuses such as Expired, Active, or Expiring Soon" based on the value of a date column. This can be achieved using Bean Shell Formatter to evaluate and return the status based on the comparison between the date in the list and the current date.

How does it work?

In this case, we want to return the status Expired or Active based on the date value in the List compared to the actual date.

  1. Edit the List Column where you want to display the status. Select the Bean Shell Formatter option to enter a custom code.
  2. Implement the following Bean Shell scripts to return the appropriate status based on the date value.

Code sample 1: Basic expiration check

This code snippet will return Expired if the date is before today and Active otherwise.

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
 
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
try {
    Date date = formatter.parse(value);
    Calendar cal = Calendar.getInstance();
    if (date.before(cal.getTime())) {
        return "Expired";
    } else {
        return "Active";
    }
} catch (ParseException e) {
    e.printStackTrace();
}

Code sample 2: Expiration check with expiring soon

This code has added logic to show the nearing expiry date with the status Expiring Soon in 30 days.

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
 
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
try {
    Date date = formatter.parse(value);
    Calendar cal = Calendar.getInstance();
     
    if (date.before(cal.getTime())) {
        return "Expired"; //before today = expiried
    }
    cal.add(Calendar.DAY_OF_MONTH, 30); //increase today's date by 30 days
    if (date.before(cal.getTime())) {
        return "Expiring Soon in 30 days"; //between now and next 30 days
    }else{
        return "Active"; //more than 30 days ahead
    }
} catch (ParseException e) {
    e.printStackTrace();
}
Created by Julieth Last modified by Aadrian on Dec 13, 2024