Custom business rule for creating change requests

To customize how Turbonomic Actions creates a Change Request (CR), the ServiceNow administrator can define a custom business rule. Select System Definition > Business Rules and create a rule for the Turbonomic Actions Approval table that is similar to the following example.

Business Rules dialog

Define this rule to run when Turbonomic Actions inserts a new record into the Turbonomic Action Approval table. Enable Advanced so that you can include custom JavaScript code.

Main fields

Field name Value
Name Create Matching CR for Action Approval
Table Turbonomic Action Approval x_turbo_turbonomic_turbonomic_action_approval]
Application Turbonomic actions
Active ON
Advanced ON

When to run

Field name Value
When after
Order 100
Insert ON
Update OFF
Delete OFF
Query OFF

For the other fields, use the defaults.

Advanced

The custom script links the new change request with the action approval record. It also offers the flexibility of setting other change request fields according to the company policy.

Add JavaScript code similar to the following code, where you specify the CR values that you want the rule to populate:

(function executeRule(current, previous /*null when async*/) {

  var changeRequest = null;
  var replacedActionId = current. replaced_action_id;
  if (replacedActionId != null) { // The action approval replaces an existing one
     var replacedState = new 
          GlideRecordSecure('x_turbo_turbonomic_turbonomic_action_state');
        replacedState.addQuery('name', 'REPLACED');
        replacedState.query();
        replacedState.next();

     var replacedApproval = new 
        GlideRecordSecure('x_turbo_turbonomic_turbonomic_action_approval');
      replacedApproval.addQuery('oid', replacedActionId);
      replacedApproval.addQuery('state_id', replacedState.sys_id);
      replacedApproval.orderByDesc('sys_updated_on');
      replacedApproval.query();

      if (replacedApproval.next()) {
        // Link the new action approval to the CR created for the replaced one
        current.change_request_id = replacedApproval.change_request_id;
        return;
      }
  }

  var combinedActionIds = current.combine_with;
  if (combinedActionIds) {
    var combinedOIDs = combinedActionIds.split(',');  
    for (var i = 0; i < combinedOIDs.length; i++) {
      var combinedApproval = new 
        GlideRecordSecure('x_turbo_turbonomic_turbonomic_action_approval');
      combinedApproval.addQuery('oid', combinedOIDs[i]);
      combinedApproval.orderByDesc('sys_updated_on');
      combinedApproval.query();

      // If a valid combined action exists
      if (combinedApproval.next()) {
        var actionState = new 
          GlideRecordSecure('x_turbo_turbonomic_turbonomic_action_state');
        actionState.addQuery('name', 'PENDING_APPROVAL');
        actionState.query();

        // If the combined action approval current state is PENDING_APPROVAL
        if (actionState.next() && (actionState.sys_id == combinedApproval.state_id)) {
          if (combinedApproval.change_request_id) {
            changeRequest = new GlideRecordSecure('change_request');
            changeRequest.addQuery('sys_id', combinedApproval.change_request_id);
            changeRequest.query();

            // If the combined action is linked to a change request on 'NEW' state
            if (changeRequest.next() && (changeRequest.state == 'NEW')) {
              current.change_request_id = changeRequest.sys_id;
              return;
            }
          }
        }
      }
    }
  }

  // Create new change request for the current action approval record
  changeRequest = new GlideRecordSecure("change_request");
  changeRequest.newRecord();

  changeRequest.type = 'standard';
  changeRequest.start_date = gs.daysAgo(-1);
  changeRequest.end_date = gs.daysAgo(-2);
  changeRequest.description = current.description;
  // TODO: Set additional change requests fields, as required

  // Replace with valid sys id of a standard change producer version record
  var chgProducerVersion = "d08ec5d9dbXXXXXXXXXXXXXXXXXXXXXXXX";

  var chgTemplate = new GlideRecordSecure("std_change_producer_version");
  chgTemplate.get("sys_id", chgProducerVersion);

  var template = GlideTemplate.get(chgTemplate.std_change_producer.template);
  template.apply(changeRequest);

  changeRequest.work_notes = "CR has been created using the template named: " +
    chgTemplate.std_change_producer.name;
     
  // Links the Action Approval entry to the newly created Change Request record
  current.change_request_id = changeRequest.insert();
})(current, previous);

Next steps