To create a report in ServiceNow that identifies all records in the alm_hardware table whose state has been changed from "In Use" to "In Stock", along with the date and time of this change, you will indeed encounter the challenge that ServiceNow's standard reporting doesn't directly support reporting on historical state changes. However, you can approach this problem through several indirect methods, such as leveraging Audit records or creating a custom solution to track these changes. Here's a step-by-step guide focusing on a method that involves custom scripting and report creation. This method assumes you have the appropriate permissions to create scripts and reports in ServiceNow.
Step 1: Create a Custom Field (Optional)
First, you might want to create a custom field in the alm_hardware table to store the timestamp of when an item's state changed from "In Use" to "In Stock". This is optional but can simplify reporting.
- Navigate to System Definition > Tables & Columns.
- Search for and open the
alm_hardware table.
- Click on "New" to add a new column. Name it something like "In Use to In Stock Date" and select "Date/Time" as the type.
- Save the new column.
Step 2: Create a Business Rule to Catch the State Change
Create a business rule that triggers when the state of an alm_hardware record changes, then checks if this change is from "In Use" to "In Stock" and captures the change time.
- Go to System Definition > Business Rules and click "New" to create a new rule.
- Give your rule a name, e.g., "Track In Use to In Stock".
- In the "When to run" tab, choose to run the rule on "Update", and make it so that it only runs when the
state field changes.
- In "Advanced" condition, ensure it targets the
alm_hardware table.
- Check the "Advanced" checkbox and provide the script to capture the state change. Your script might look like this:
(function executeRule(current, previous /*null when async*/) {
// Check if state changed from 'In Use' to 'In Stock'
if (previous != null && current.state == 'your_in_stock_sys_id' && previous.state == 'your_in_use_sys_id') {
current.u_in_use_to_in_stock_date = new GlideDateTime(); // Assuming 'u_in_use_to_in_stock_date' is your custom field
current.update();
}
})(current, previous);
Replace 'your_in_stock_sys_id' and 'your_in_use_sys_id' with the actual sys_ids of the "In Stock" and "In Use" states, respectively. Replace u_in_use_to_in_stock_date with the actual field name if different.
Step 3: Create the Report
Now, create a report that displays this data.
- Navigate to Reports > Create New.
- Choose a report type; List is a good choice here.
- As your data source, select the
alm_hardware table.
- In the "Columns" section, add the fields you're interested in, including the "State" and your custom "In Use to In Stock Date" columns.
- Add a condition to filter records where your custom date field
is not empty.
This report will now show you all alm_hardware records that have changed state from "In Use" to "In Stock", including the date and time this transition occurred, based on the tracking implemented by your business rule.
Additional Notes
Remember, implementing this solution requires testing, especially the Business Rule part, to ensure it correctly captures the data you need without unintended side effects. Keep in mind that any update within the Business Rule (like current.update();) will re-trigger all the Business Rules for the alm_hardware table, so consider the implications or look into alternative methods for recording the date/time if necessary.