Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Set due date last day of the month

lucky24
Tera Contributor

Hi Team,

I am working on an HR task in which I have to set the due date as the "last day of the new hire month" which should depend on the subject person's time zone.

 

Suppose I have my hire date of this month(January) so in the task due date should be the last day of January according subject person.?

 

Note - The "hire date" and "subject person" are the fields on the form

hire date- date field

subject person - reference field which is referring to user table

 

Can someone please help me here?

 

Thanks.

3 REPLIES 3

SravyaPangu
Tera Contributor

Hi @lucky24 I have the similar requirement. Were you able to achieve it. 
Thank you in advance 

M Iftikhar
Tera Sage

Hi @lucky24/ @SravyaPangu ,

I tested this requirement in a Background Script. You can use the same logic in your Business Rule to calculate the last day of the hire month based on the subject person’s time zone.

Here’s the working BG script I used:

(function() {
    // --- TEST CASE PARAMETERS ---
    var testHireDate = '2025-02-15'; 
    var testUserName = 'Abel Tuter'; 
    // ----------------------------

    var hireDate = testHireDate;
    var subjectTZ = ''; // Initialize the time zone variable

    // 2. Load user and their time zone
    var userGR = new GlideRecord('sys_user');

    // Find the user based on the name
    if (userGR.get('name', testUserName)) { 
        // Get the time zone property from the user's record
        var userTZField = userGR.time_zone.toString();

        if (userTZField == '' || userTZField.toLowerCase() == 'system') {
            subjectTZ = gs.getProperty('glide.sys.default.tz'); 
        } else {
            subjectTZ = userTZField;
        }

        gs.print('Found User: ' + userGR.name + ' (' + userGR.user_name + ')');
        gs.print('Retrieved Time Zone: ' + subjectTZ); 
    } else {
        gs.error('Subject Person not found for name: ' + testUserName + '. Using default TZ: UTC');
        subjectTZ = 'UTC';
    }

    if (!hireDate) {
        gs.error('Test Hire Date is missing.');
        return;
    }

    // 3. Anchor a GlideDateTime to the hire date
    var gdt = new GlideDateTime(hireDate + " 00:00:00");
    gs.print('Input Hire Date (System TZ): ' + gdt.getDisplayValue());

    // Set the Time Zone
    gdt.setTimeZone(subjectTZ); 
    gs.print('Input Hire Date (User TZ: ' + subjectTZ + '): ' + gdt.getDisplayValue());

    // 4. Calculate last day of the hire month using LOCAL time methods
    gdt.setDayOfMonthLocalTime(1);     // Move to 1st of current month
    gdt.addMonthsLocalTime(1);         // Move to 1st of next month
    gdt.addDaysLocalTime(-1);          // Go back one day -> last day of hire month

    // 5. Output the result
    gs.print('----------------------------------------------------');
    gs.print('  Calculated Due Date for ' + testUserName + ' (Hire Month: ' + gdt.getMonth() + ')');
    gs.print('   - Date Object (gdt): ' + gdt.getDisplayValue());
    gs.print('   - Expected Date: ' + gdt.getDate());

})();

Result Of Script:

*** Script: Found User: Abel Tuter (abel.tuter)
*** Script: Retrieved Time Zone: Hongkong
*** Script: Input Hire Date (System TZ): 2025-02-14 16:00:00
*** Script: Input Hire Date (User TZ: Hongkong): 2025-02-15 08:00:00
*** Script: ----------------------------------------------------
*** Script: Calculated Due Date for Abel Tuter (Hire Month: 2)
*** Script: - Date Object (gdt): 2025-02-28 08:00:00
*** Script: - Expected Date: 2025-02-28


If my response helped, please mark it as the accepted solution so others can benefit as well. 

 

Thanks & Regards,
Muhammad Iftikhar

If my response helped, please mark it as the accepted solution so others can benefit as well.

SupriyaWaghmode
Kilo Sage
Kilo Sage

@lucky24 , 

 

Can you check the solution posted by @M Iftikhar , if this resolve then please close the question. 

In case any query please update here.