How to make start and end date auto populate after based on year selection?

Nani16
Tera Contributor

Hi Community,

How to make start and end date auto populate when you select the year.

 

Rules :The year of applicability will automatically fill the following fields :
-  start date = January 1st of the selected year
-  end date = December 31st of the selected year

 

Nani16_1-1719407450199.png

 

Nani16_0-1719407197349.png

 

 

1 ACCEPTED SOLUTION

Sid_Takali
Kilo Patron
Kilo Patron

Hi @Nani16 Try this. I have not tested

Create onChange client script on Year field and add below code in Client Script

(function executeRule(current, previous /*null when async*/) {

    // Get the year from the Year field
    var year = g_form.getValue('year');
    
    // Check if the year is a valid number
    if (!isNaN(year) && year > 0) {
        // Format the start and end dates
        var startDate = year + '-01-01';
        var endDate = year + '-12-31';

        // Set the Start Date and End Date fields
        g_form.setValue('start_date', startDate);
        g_form.setValue('end_date', endDate);
    } else {
        // Clear the date fields if the year is invalid
        g_form.clearValue('start_date');
        g_form.clearValue('end_date');
    }

})(current, previous);

 

Regards,

Sid

 

View solution in original post

1 REPLY 1

Sid_Takali
Kilo Patron
Kilo Patron

Hi @Nani16 Try this. I have not tested

Create onChange client script on Year field and add below code in Client Script

(function executeRule(current, previous /*null when async*/) {

    // Get the year from the Year field
    var year = g_form.getValue('year');
    
    // Check if the year is a valid number
    if (!isNaN(year) && year > 0) {
        // Format the start and end dates
        var startDate = year + '-01-01';
        var endDate = year + '-12-31';

        // Set the Start Date and End Date fields
        g_form.setValue('start_date', startDate);
        g_form.setValue('end_date', endDate);
    } else {
        // Clear the date fields if the year is invalid
        g_form.clearValue('start_date');
        g_form.clearValue('end_date');
    }

})(current, previous);

 

Regards,

Sid