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

Robert H
Mega Sage

Hello @Gautam Raj ,

 

Please provide more details about the requirement.

It sounds like you want to query some records from a table that match the given criteria, including start_date being exactly one year from now.

But what shall then happen with those records? Are they to be updated, or something else? Asking because depending on the actual requirement a Client Script might not be the best idea.

 

Regards,

Robert

below is the actual requirement .. but i need the code to get a record which is exactly one year from today

1. System to send a notification to Product Owners on product EOL / EOS with below conditions:

 

For software EOS / EOL:
  1. In sam_sw_product_lifecycle, lookup for records with below conditions:
    • Lifecycle Phase is End of Life
    • Product is not empty
    • Phase start date is exactly 1 year from today
    • Stop if no record found

  2. In cmdb_software_product_model, lookup each record found above with below conditions:
    • Product is <Product found in sam_sw_product_lifecycle>
    • Version is <Version found in sam_sw_product_lifecycle>
    • GTS ID is not empty
    • Stop if no record found and proceed with next product (if any)

  3. In cmdb_sam_sw_discovery_model, lookup each record found above with below conditions:
    • Software model is <Display name found in sam_sw_product_lifecycle>
    • Stop if no record found and proceed with next product (if any)

  4. In cmdb_sam_sw_install, lookup each record found above with below conditions:
    • Discovery model is <Display name found in cmdb_sam_sw_discovery_model>
    • Stop if search result count is 0 and proceed with next product (if any)
    • If search result count is more than 0, proceed with step 5

  5. In sn_apm_trm_standards_product_lifecycle, lookup each GTS ID that is left with below conditions:
    • GTS ID is <GTS ID found in sn_apm_trm_standards_product_lifecycle>
    • Strategic Investment Status is Evaluation or Invest or Contain
    • Stop if no record found and proceed with next product (if any)
  6. In sn_apm_trm_standards_product, lookup product record found above with below conditions:
    • Owner is not Blank
    • Owner.Action is not Termination
    • Category is one of:
      • Technology & Architecture
      • TA-End User Services
      • Desktop Hardware
      • Desktop Management
      • Desktop Software
      • Analytics
      • Browser Extensions
      • Client OS
      • Core Desktop Applications
      • Deployment Services
      • Desktop Software-Miscellaneous
      • Desktop Software-Miscellaneous Tools
      • Desktop Software-Others
      • Laptops
      • Microsoft Office Products
      • Miscellaneous
      • Miscellaneous Software
      • Monitors
      • Others
      • Web Browsers
      • Workstations
    • Stop if no record found and proceed with next product (if any)

  7. Only trigger notification for Software Product Lifecycle record which meets all the conditions above

Hello @Gautam Raj ,

 

Thanks for the clarification. So what you actually need is not a Client Script but a server side query to check if the notification is to be sent.

 

Your requirements do not specify what should trigger this process, e.g. whether it's a daily job or some Business Rule, but in any case here is the piece of code that will check if the first condition is met:

 

var gr = new GlideRecord('sam_custom_sw_product_lifecycle');
gr.addNotNullQuery('product_name');
gr.addQuery('lifecycle_phase', 'end_of_life');
gr.addQuery('start_date', gs.daysAgoStart(-365));
gr.query();

while (gr.next()) {
	gs.info('found record with start date ' + gr.start_date.getByFormat("dd-MM-yyyy"));
	// proceed with the other checks
}

 

Regards,

Robert

 

Hello @Gautam Raj ,

 

Did you check the script that I provided?

 

Given the following record in the table

 

RobertH_0-1744011790256.png

 

it gives the following output:

found record with start date 07-04-2026

 

Regards,

Robert