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 

 

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

 

Regards, 

Shivalika 

 

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

 

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