The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Calculate Expiration date 14 days from todays date

manueleriklego
Tera Expert

Hello all,

I'm new in servicenow and still learning scripting on it and would appreciate your help on this.

Requirement:
- Look for all user accounts that will expire 14 days from the current date then send a reminder to the manager.

From user's table: we have a field called AD Account expires (date/time field)

I am trying to achieve this via flow designer but unable to capture the condition from look up records to show exactly the account lists that about to expire in 14 days. The closest I was able to achieve was using the relative operation which is not exactly what I need since it shows all accounts that will expire in 14 days less.

manueleriklego_0-1723703893268.png

 



May I ask your expertise if you have encounter this before? Thank you in advance!


1 ACCEPTED SOLUTION

manueleriklego
Tera Expert

Thank you everyone for the assistance. I was able to figure it out via creating action script in flow designer. 

For reference, here's the script I created:

(function execute(inputs, outputs) {
  // Get the current date and time
  var currentDateTime = new GlideDateTime();
 
  // Create a GlideDateTime object for adding 14 days
  var newDateTime = new GlideDateTime();
  newDateTime.addDays(14);
 
  // Set the outputs
  outputs.date_14_days_later = newDateTime;
})(inputs, outputs);

View solution in original post

4 REPLIES 4

abirakundu23
Mega Sage

Hi @manueleriklego ,

 

You can achieve through schedule job as well.

 var Expiration = 14;
var Date = new GlideDateTime();
    Date.addDays(Expiration);
    var expirationDate = Date.getValue();
    gs.info('current Expiration  date' +expirationDate);

    // Query to find user accounts expiring in 14 days
    var userGr = new GlideRecord('sys_user');
    userGr.addQuery('u_expiration_date', expirationDate); // 'u_expiration_date' is AD account expiration field
    userGr.query();
//gs.info('current Expiration  users profile' +userGr.toString());
    // Process each user record found
    while (userGr.next()) {
        // Retrieve the manager's email
        gs.info('current Expiration  users profile' +userGr.toString());
        var managerEmail = userGr.manager.email;
       
        gs.info('Manager of the users profile' +managerEmail);
        gs.eventQueue('event name',userGr,managerEmail, ' '); 

    }

gs.eventQueue('event name',userGr,managerEmail, ' '); //this API is used to send for a email notification.
1. You have to create Event from Event Registry & create a notification which is event based
Generating Events | ServiceNow Developers.

How to Create a Notification and Trigger Email in ServiceNow (youtube.com)
Please mark helpful & correct answer if it's worthy for you.

https://www.youtube.com/watch?embeds_referring_euri=https%3A%2F%2Fwww.bing.com%2F&embeds_referring_o... 

Matthew Smith
Kilo Sage

You'll need to test this to confirm, but I think you just need to change it to use 2 conditions:

 

1. AD account expires before 15 days ahead

2. AD account expires after 13 days ahead

 

In theory this should give you everything that expires in exactly 14 days.

MatthewSmith_0-1723719534950.png

 

HIROSHI SATOH
Mega Sage

In the Flow Designer

 

1. Define a variable and set a date 14 days from today

var fourteenDaysBefore = gs.dateAdd(gs.now(), 14);
return fourteenDaysBefore

2. Use the variable as a condition to extract the target user records

3. Send a notification for each extracted user record

manueleriklego
Tera Expert

Thank you everyone for the assistance. I was able to figure it out via creating action script in flow designer. 

For reference, here's the script I created:

(function execute(inputs, outputs) {
  // Get the current date and time
  var currentDateTime = new GlideDateTime();
 
  // Create a GlideDateTime object for adding 14 days
  var newDateTime = new GlideDateTime();
  newDateTime.addDays(14);
 
  // Set the outputs
  outputs.date_14_days_later = newDateTime;
})(inputs, outputs);