Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

Set date value in a custom field after insert

MaharshiC
Tera Contributor

Hi,

 

I have a year(type - integer) & month(type - choice having 12 choice values) & date field in my custom table. Now whenever I insert a record in the table I want to get the date field populated based on the year and month. So if my year is 2025 and month is January I want the date field to be automatically populated as 1st January 2025. Similarly for February I want to populate it as 1st Februrary 2025. Date we can have as 1st of every month. Some assistance on how can I write a script for this to populate the date in this way will greatly help?

 

Regards,

Maharshi

 

1 ACCEPTED SOLUTION

folusho
Tera Guru

@MaharshiC 

You can achieve this by using a Business Rule on your custom table that triggers on insert (or insert/update) and populates the date field as the 1st of the selected month and year.

 

(function executeRule(current, gsr) {
    // Mapping of month names to their numeric representation
    var monthMap = {
        'January': '01',
        'February': '02',
        'March': '03',
        'April': '04',
        'May': '05',
        'June': '06',
        'July': '07',
        'August': '08',
        'September': '09',
        'October': '10',
        'November': '11',
        'December': '12'
    };

    var year = current.year;
    var monthName = current.month.toString();
    var day = '01';

    if (year && monthMap[monthName]) {
        var dateString = year + '-' + monthMap[monthName] + '-' + day;
        current.date = dateString; // This assumes 'date' field is of type 'Date' (not date/time)
    }

})(current, gsr);

 

View solution in original post

2 REPLIES 2

YaswanthKurre
Tera Guru

Hi @MaharshiC ,

 

Before insert/update of those 3 fields, trigger a BR with before insert/ update rule and in the scripting section , use GlideDateTime api call to get the date format and populate to your date field

 

Code snippet:

// Create a JavaScript Date object for the dayof the selected month and year
            var jsDate = new Date(current.year, current.monthnumber, current.day);

            // Convert to GlideDateTime and set the date field
            var gdt = new GlideDateTime();
            gdt.setDisplayValue(gs.getDateTimeUtil().get().formatDate(jsDate, "yyyy-MM-dd"));
           current.date_field = gdt; // Replace 'date_field' with your actual field name

 

Please mark this as helpful/ correct if this answers your question.

 

Thanks,

Yaswanth

 

folusho
Tera Guru

@MaharshiC 

You can achieve this by using a Business Rule on your custom table that triggers on insert (or insert/update) and populates the date field as the 1st of the selected month and year.

 

(function executeRule(current, gsr) {
    // Mapping of month names to their numeric representation
    var monthMap = {
        'January': '01',
        'February': '02',
        'March': '03',
        'April': '04',
        'May': '05',
        'June': '06',
        'July': '07',
        'August': '08',
        'September': '09',
        'October': '10',
        'November': '11',
        'December': '12'
    };

    var year = current.year;
    var monthName = current.month.toString();
    var day = '01';

    if (year && monthMap[monthName]) {
        var dateString = year + '-' + monthMap[monthName] + '-' + day;
        current.date = dateString; // This assumes 'date' field is of type 'Date' (not date/time)
    }

})(current, gsr);