To achieve this in ServiceNow, you can set up a Business Rule that captures the date and time when an item's state changes from "in stock" to "in use" or vice versa in the alm_hardware table. This Business Rule will update a custom field (say, x_state_change_info) in the same alm_hardware record, documenting the state change and the exact time it occurred.
Assuming you already have a custom field (e.g., x_state_change_info) in your alm_hardware table to store the information about the state change, follow these steps:
1. Navigate to Business Rules
- Go to System Definition > Business Rules in your ServiceNow instance.
2. Create a New Business Rule
- Click the "New" button to start the creation of a new Business Rule.
3. Configure the Business Rule Details
Here's how you might set it up:
- Name: Give your Business Rule a name, such as
"Capture State Change Time".
- Table: Select
alm_hardware from the dropdown menu. This indicates that the Business Rule applies to the alm_hardware table.
- When to run:
- When: Choose "after", since you want to capture the change after it has been confirmed and saved.
- Insert: Uncheck, as you are tracking state changes, not new records.
- Update: Check, because you're interested in updates that change the state.
- Delete: Uncheck, as deletions are not within the scope of this tracking.
- Filter Conditions: Configure it to only trigger for the state changes you're interested in. You may need to manually input the advanced condition to specifically capture transitions from "in stock" to "in use" and vice versa.
4. Script
Now, in the Script field, write your JavaScript code to capture the state change and log it in the x_state_change_info field with the date and time. Here's an example script:
(function executeRule(current, previous /*null when async*/) {
var newState = current.state.toString();
var oldState = previous.state.toString();
var dateTime = new GlideDateTime();
if ((newState == 'in_use' && oldState == 'in_stock') || (newState == 'in_stock' && oldState == 'in_use')) {
var changeInfo = 'State changed from ' + oldState + ' to ' + newState + ' at ' + dateTime.getDisplayValue();
current.x_state_change_info = changeInfo; // Assuming 'x_state_change_info' is your custom field's API name
current.update();
}
})(current, previous);
Note:
- Replace
'in_stock' and 'in_use' with the exact values used in your instance if they are different.
- Ensure
x_state_change_info is the correct field name for your custom date/time capture field in the alm_hardware table. If not, adjust accordingly.
- Be cautious with the
current.update() call within an "after" Business Rule as it can lead to recursion if not correctly checked. It's safe in this context as the condition is very specific, but generalize this logic with care.
5. Save the Business Rule
After you've configured the Business Rule with the appropriate script, save your changes. Now, whenever an item’s state changes between "in stock" and "in use" in the alm_hardware table, the x_state_change_info field of the respective record will be updated with a message noting the change and the exact time it happened.
This custom setup not only tracks the state changes efficiently but also embeds the information within the same record for easy reference and auditing.