- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-11-2024 03:12 PM
I'm trying to create automation through which a Flow on a semi-regular basis checks our Server CI's and their Most Recent Discovery. If the Most Recent Discovery is within the last 90 days, make sure the status of the device is all Operational/In Use/etc.
The first thing I noticed is that when attempting to create Flow Logic, trying to do a comparison on the date field forced me to utilize a data reference. I wasn't aware that this was a limitation, but I tried to work around it.
I created a Flow Variable called Last 90 Days and tried to calculate it based on a new GlideDateTime and subtracting 90 days to get the previous 90 days.
//Get the current date and time as of running this Flow.
var currentDateTime = new GlideDateTime();
//Set a variable 90 days in the past.
var pastDateTime = currentDateTime.addDays(-90);
//Return this variable to the Flow Variable.
return pastDateTime;
Now, when I set that data reference in and try to test with a handful of records, it doesn't appear to return any date in the Flow logs.
Finally, it ends up spitting out "Error occurred while updating record: null", even though it does actually return a record.
I'm not sure if this is being caused by the strangeness with the date, or if it's something else causing this entirely, but any help would be appreciated. Not really sure what's going wrong.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-16-2024 09:57 AM - edited ‎07-19-2024 08:39 AM
I'll have to give this a try, but I ended up just giving a scheduled script execution a try. Seems that it has worked out well and does exactly what I need. It's actually slightly simpler because I don't need to do any logic in the script, I can just let the encoded query only include devices I want to update.
Code snippet if anyone wants/needs it:
// Create a new Glide Record for the CMDB Server table.
var serverCI = new GlideRecord("cmdb_ci_server");
// Add an encoded query for all server CI's discovered at or before
// the last 90 days.
serverCI.addEncodedQuery("last_discovered<=javascript:gs.beginningOfLast90Days()");
// Perform the Glide Query.
serverCI.query();
// Set the Install Status to Retired (7).
serverCI.install_status = 7;
// Set the Operational Status to Retired (6).
serverCI.operational_status = 6;
// Set the Life Cycle Stage to End of Life.
serverCI.life_cycle_stage = "End of Life";
// Set the Life Cycle Stage Status to Retired.
serverCI.life_cycle_stage_status = "Retired";
// Update all records found by the query.
serverCI.updateMultiple();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-11-2024 08:57 PM
Instead of creating a flow variable, you could try to code the value directly into the field using the edit function button.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-12-2024 11:22 AM
Thanks for the suggestion! I had originally thought to try and do that, but there isn't any script option available, only the data reference.
Here's the option on the flow variable as an example:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-12-2024 12:38 PM
In the docs: GlideDateTime - Global (servicenow.com)
Under the addDays section, "Use addDaysLocalTime() and addDaysUTC() instead of this method."
Maybe give those a try.
Then for the record, try passing the record's sys_id rather than just the record ref itself. I recall needing to that somewhere before.
As for the record found,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-16-2024 09:57 AM - edited ‎07-19-2024 08:39 AM
I'll have to give this a try, but I ended up just giving a scheduled script execution a try. Seems that it has worked out well and does exactly what I need. It's actually slightly simpler because I don't need to do any logic in the script, I can just let the encoded query only include devices I want to update.
Code snippet if anyone wants/needs it:
// Create a new Glide Record for the CMDB Server table.
var serverCI = new GlideRecord("cmdb_ci_server");
// Add an encoded query for all server CI's discovered at or before
// the last 90 days.
serverCI.addEncodedQuery("last_discovered<=javascript:gs.beginningOfLast90Days()");
// Perform the Glide Query.
serverCI.query();
// Set the Install Status to Retired (7).
serverCI.install_status = 7;
// Set the Operational Status to Retired (6).
serverCI.operational_status = 6;
// Set the Life Cycle Stage to End of Life.
serverCI.life_cycle_stage = "End of Life";
// Set the Life Cycle Stage Status to Retired.
serverCI.life_cycle_stage_status = "Retired";
// Update all records found by the query.
serverCI.updateMultiple();