Need a client script to fetch date which is exactly one year from today in dd-MM-yyyy format

Gautam Raj
Tera Contributor

Need a client script to fetch date which is exactly one year from today in dd-MM-yyyy format

 

  1. In sam_sw_product_lifecycle, lookup for records with below conditions:
    • Lifecycle Phase (lifecycle_phase) is End of Life
    • Product (norm_product) is not empty
    • Phase start date (start_date) is exactly 1 year from today

that phase start date output should be exactly one year from today in dd-MM-yyyy format 

 

below is the record i should be able to fetch in the output

GautamRaj_0-1743848739369.png

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Gautam Raj 

As per your requirement it's server side code and seems to be scheduled job etc

You need to calculate 1 year in script, simply use this in server side

// Get today's date and format it to dd-MM-yyyy
    var today = new GlideDateTime();
    var oneYearFromToday = new GlideDateTime();
    oneYearFromToday.addYears(1);
    var formattedDate = oneYearFromToday.getLocalDate().toString().split('-').reverse().join('-');

    // GlideRecord to query sam_sw_product_lifecycle table
    var gr = new GlideRecord('sam_sw_product_lifecycle');
    gr.addQuery('lifecycle_phase', 'End of Life');
    gr.addNotNullQuery('norm_product');
    gr.addQuery('start_date', oneYearFromToday);
    gr.query();

    // Process the results
    while (gr.next()) {
        gs.info('Record found with start_date exactly one year from today: ' + formattedDate);
    }

Output: When I ran the script in background for sys_user and date field and it gave me correct output, 1 year from today is 6th April 2026

 

AnkurBawiskar_0-1743942775520.png

AnkurBawiskar_1-1743942803500.png

 

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

10 REPLIES 10

Shivalika
Mega Sage

Hello @Gautam Raj 

 

You can write below script include -

 

var GetFormattedFutureDate = Class.create();
GetFormattedFutureDate.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getOneYearFromToday: function() {
        var gdt = new GlideDate();
        gdt.addYearsLocalTime(1);
        return gdt.getByFormat('dd-MM-yyyy');
    }
});

 

You can use below client script - 

 

function getOneYearDateFromScriptInclude() {
    var ga = new GlideAjax('GetFormattedFutureDate');
    ga.addParam('sysparm_name', 'getOneYearFromToday');
    
    ga.getXMLAnswer(function(response) {
        var formattedDate = response;
        // Set it to your field
        g_form.setValue('your_field_name', formattedDate); // Replace with your field name
    });
}

// Optionally call it on form load
getOneYearDateFromScriptInclude();

 

Refer below screenshot:-

 

Shivalika_0-1743920739747.png

 

Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this questions from unsolved to solved bucket. 

 

Regards,

 

Shivalika 

 

My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194

 

My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeEISQCY

 

 

 

Ankur Bawiskar
Tera Patron
Tera Patron

@Gautam Raj 

As per your requirement it's server side code and seems to be scheduled job etc

You need to calculate 1 year in script, simply use this in server side

// Get today's date and format it to dd-MM-yyyy
    var today = new GlideDateTime();
    var oneYearFromToday = new GlideDateTime();
    oneYearFromToday.addYears(1);
    var formattedDate = oneYearFromToday.getLocalDate().toString().split('-').reverse().join('-');

    // GlideRecord to query sam_sw_product_lifecycle table
    var gr = new GlideRecord('sam_sw_product_lifecycle');
    gr.addQuery('lifecycle_phase', 'End of Life');
    gr.addNotNullQuery('norm_product');
    gr.addQuery('start_date', oneYearFromToday);
    gr.query();

    // Process the results
    while (gr.next()) {
        gs.info('Record found with start_date exactly one year from today: ' + formattedDate);
    }

Output: When I ran the script in background for sys_user and date field and it gave me correct output, 1 year from today is 6th April 2026

 

AnkurBawiskar_0-1743942775520.png

AnkurBawiskar_1-1743942803500.png

 

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Gautam Raj 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar ,

 

when i try the above code in background script i get the below output

 

[0:00:00.415] Script completed in scope global: script


Script execution history and recovery available here


Time: 0:00:00.377 id: scbnow05_1[glide.25] primary_hash=733765072 (connpid=16182813) for: SELECT sam_sw_product_lifecycle0.`sys_id` FROM sam_sw_product_lifecycle sam_sw_product_lifecycle0  WHERE sam_sw_product_lifecycle0.`lifecycle_phase` IN ('End of Life' , 'End of Support') AND sam_sw_product_lifecycle0.`norm_product` IS NOT NULL  AND sam_sw_product_lifecycle0.`start_date` = '2026-04-07' /* scbnow05006, gs:622CBF842B30221028DEF95FEE91BFEC, tx:136033f42b7ca61028def95fee91bf9e, hash:733765072 */ 

 

below are the 3 counts which i should get when i run getRowCount but am not getting any

GautamRaj_0-1744002382174.png

 

below is the code i tried running in background script. Also i want the date format in the output to be dd-MM-yyyy

// Get today's date and format it to dd-MM-yyyy
    var today = new GlideDateTime();
    var oneYearFromToday = new GlideDateTime();
    oneYearFromToday.addYears(1);
    var formattedDate = oneYearFromToday.getLocalDate().toString().split('-').reverse().join('-');

    // GlideRecord to query sam_sw_product_lifecycle table
    var gr = new GlideRecord('sam_sw_product_lifecycle');
    gr.addQuery('lifecycle_phase', 'IN', ['End of Life', 'End of Support']);
    gr.addNotNullQuery('norm_product');
    gr.addQuery('start_date', oneYearFromToday);
    gr.query();

    // Process the results
    while (gr.next()) {
        gs.info('Record found with start_date exactly one year from today: ' + formattedDate);
    }

 

@Gautam Raj 

did you add logs and see in background script?
try this and share the logs

// Get today's date and format it to dd-MM-yyyy
var today = new GlideDateTime();
var oneYearFromToday = new GlideDateTime();
oneYearFromToday.addYears(1);
var formattedDate = oneYearFromToday.getLocalDate().toString().split('-').reverse().join('-');
gs.info('formattedDate' + formattedDate);

// GlideRecord to query sam_sw_product_lifecycle table
var gr = new GlideRecord('sam_sw_product_lifecycle');
gr.addQuery('lifecycle_phase', 'IN', ['End of Life', 'End of Support']);
gr.addNotNullQuery('norm_product');
gr.addQuery('start_date', oneYearFromToday);
gr.query();
gs.info('my query is' + gr.getEncodedQuery());
// Process the results
while (gr.next()) {
    gs.info('Record found with start_date exactly one year from today: ' + formattedDate);
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader