A business rule in ServiceNow is a server-side script that runs when a record is displayed, inserted, updated, deleted, or when a table is queried. Business rules can be beneficial for maintaining data integrity and automating processes based on specific criteria.
Here's an example of a business rule that you might use in the context of the scenario you described: Automatically removing a server from a Business Service when it is marked as "Non-operational" and its install status is set to "Retire."
Business Rule: Automatically Remove Retired Server from Business Service
Name: Remove Retired Server from Business Service
Table: Server [Or the specific table name for servers in your instance]
When to run: Before Update (you can also run it before delete if the record is being removed entirely)
Filter Conditions:
- Operational Status changes to Non-Operational
- Install Status changes to Retire
Script:
(function executeRule(current, previous /*null when 'insert'*/) {
// Check if the server is marked non-operational and set to retire
if (current.operational_status == 'non-operational' && current.install_status == 'retire') {
// Query to find related business service CI relationships
var grRel = new GlideRecord('cmdb_rel_ci');
grRel.addQuery('child', current.sys_id); // Assuming 'child' is the field that stores the server, adjust based on your relationship configuration
grRel.query();
// Loop through found relationships and delete them
while (grRel.next()) {
// Assuming the parent CI in the relationship is the business service, check and delete
var parentCI = new GlideRecord('cmdb_ci_service'); // This should be the table where Business Services are stored
if (parentCI.get(grRel.parent)) {
// Only delete the relationship if the parent is a Business Service
// You can add additional checks here based on type or other attributes if multiple CI types are in the same table
grRel.deleteRecord(); // Deletes the relationship record, effectively removing the server from the business service
}
}
}
})(current, previous);
Explanation:
- When to run: Chosen to run "Before Update" to intercept the changes and update related records before the server record itself is finalized. Running "before delete" might be necessary if servers are often removed entirely.
- Filter Conditions: These conditions ensure the rule only executes when relevant changes are made to prevent unnecessary executions.
- Script: This business rule script looks for relationships where the current server (now marked as retired and non-operational) is a child record. It checks if the parent in each relationship is a Business Service, and if so, deletes the relationship.
Considerations:
- Ensure you're using the correct field names (
operational_status, install_status, etc.) as these might differ depending on how your ServiceNow instance is configured.
- Keep in mind the impact of deleting records and ensure that it complies with your organization's data handling policies.
- Monitor the effects of this business rule initially to catch and rectify any unintended consequences or configurations.
Adjust the script and setup according to the specific conventions and schema used in your ServiceNow instance. Deploying this business rule should help automate the management process for servers being retired.