Add a CI to an Incident

Tomas Linde
Tera Expert

Hello everyone, I have this story.
- When an Aloha Incident.Assigned to field changes, the new value should be set in all Affected CIs. EXCEPT if the new value is empty. In this case, no updates are needed.
I have tried to try modifications to these two codes with no results, any ideas. Thanks in advance and greetings.

 

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

    var assignedToAloha = current.task.assigned_to;

    var taskCiRecords = new GlideRecord('task_ci');
    taskCiRecords.addQuery('task', current.task.sys_id);
    taskCiRecords.query();

    while (taskCiRecords.next()) {
        taskCiRecords.assigned_to = assignedToAloha;
        taskCiRecords.update();
    }

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

    var assignedToAloha = current.task.assigned_to;
    var ciTableName = current.ci_item.getRefTable();

    var ciRecords = new GlideRecord(ciTableName);
    ciRecords.addQuery('task', current.task.sys_id);
    ciRecords.query();

    while (ciRecords.next()) {
        ciRecords.assigned_to = assignedToAloha;
        ciRecords.update();
    }

})(current, previous);
1 ACCEPTED SOLUTION

Iraj Shaikh
Mega Sage
Mega Sage

Hi @Tomas Linde 

You're trying to create a business rule in ServiceNow that updates the 'assigned_to' field of all Affected CIs when the 'assigned_to' field of an Aloha Incident changes, except when the new value is empty. To achieve this, you need to check if the new 'assigned_to' value is not empty before updating the Affected CIs.

Here's a revised version of your first script that includes the necessary check:

 

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

    // Check if the new 'assigned_to' value is not empty
    if (current.assigned_to.nil()) {
        // If the new value is empty, do not proceed with updates
        return;
    }

    // Get the new 'assigned_to' value
    var assignedToAloha = current.assigned_to;

    // Initialize a GlideRecord for the 'task_ci' table
    var taskCiRecords = new GlideRecord('task_ci');
    // Add a query to find CIs related to the current task
    taskCiRecords.addQuery('task', current.sys_id);
    // Execute the query
    taskCiRecords.query();

    // Loop through each related CI record
    while (taskCiRecords.next()) {
        // Update the 'assigned_to' field with the new value
        taskCiRecords.assigned_to = assignedToAloha;
        taskCiRecords.update();
    }

})(current, previous);

 


Please note the following changes:

1. I removed the `task.` prefix from `current.task.assigned_to` and `current.task.sys_id` since `current` already refers to the incident record, and you can directly access its fields.
2. I added a check at the beginning of the script to return early if the new 'assigned_to' value is empty (`current.assigned_to.nil()`).

Please mark this response as correct or helpful if it assisted you with your question.

View solution in original post

2 REPLIES 2

Iraj Shaikh
Mega Sage
Mega Sage

Hi @Tomas Linde 

You're trying to create a business rule in ServiceNow that updates the 'assigned_to' field of all Affected CIs when the 'assigned_to' field of an Aloha Incident changes, except when the new value is empty. To achieve this, you need to check if the new 'assigned_to' value is not empty before updating the Affected CIs.

Here's a revised version of your first script that includes the necessary check:

 

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

    // Check if the new 'assigned_to' value is not empty
    if (current.assigned_to.nil()) {
        // If the new value is empty, do not proceed with updates
        return;
    }

    // Get the new 'assigned_to' value
    var assignedToAloha = current.assigned_to;

    // Initialize a GlideRecord for the 'task_ci' table
    var taskCiRecords = new GlideRecord('task_ci');
    // Add a query to find CIs related to the current task
    taskCiRecords.addQuery('task', current.sys_id);
    // Execute the query
    taskCiRecords.query();

    // Loop through each related CI record
    while (taskCiRecords.next()) {
        // Update the 'assigned_to' field with the new value
        taskCiRecords.assigned_to = assignedToAloha;
        taskCiRecords.update();
    }

})(current, previous);

 


Please note the following changes:

1. I removed the `task.` prefix from `current.task.assigned_to` and `current.task.sys_id` since `current` already refers to the incident record, and you can directly access its fields.
2. I added a check at the beginning of the script to return early if the new 'assigned_to' value is empty (`current.assigned_to.nil()`).

Please mark this response as correct or helpful if it assisted you with your question.

Aniket Chavan
Tera Sage
Tera Sage

Hello @Tomas Linde ,

Please give a try to the script below and see how it works for you.

For 'task_ci' records:

(function executeRule(current, previous /* null when async */) {
    // Check if the new 'assigned_to' value is not empty
    if (current.assigned_to.nil()) {
        // If the new value is empty, do not proceed with updates
        return;
    }

    // Get the new 'assigned_to' value
    var assignedToAloha = current.assigned_to;

    // Initialize a GlideRecord for the 'task_ci' table
    var taskCiRecords = new GlideRecord('task_ci');
    // Add a query to find CIs related to the current task
    taskCiRecords.addQuery('task', current.sys_id);
    // Execute the query
    taskCiRecords.query();

    // Loop through each related CI record
    while (taskCiRecords.next()) {
        // Update the 'assigned_to' field with the new value
        taskCiRecords.assigned_to = assignedToAloha;
        taskCiRecords.update();
    }

})(current, previous);

 

For Affected CIs:

(function executeRule(current, previous /* null when async */ ) {
    // Check if the new 'assigned_to' value is not empty
    if (current.assigned_to.nil()) {
        // If the new value is empty, do not proceed with updates
        return;
    }

    // Get the new 'assigned_to' value
    var assignedToAloha = current.assigned_to;
    
    // Assuming 'incident' is the reference field to the Incident table in your CI records
    var ciRecords = new GlideRecord('Your_CI_Table_Name');
    ciRecords.addQuery('incident', current.sys_id);
    ciRecords.query();

    // Loop through each related CI record
    while (ciRecords.next()) {
        // Update the 'assigned_to' field with the new value
        ciRecords.assigned_to = assignedToAloha;
        ciRecords.update();
    }

})(current, previous);

 

Let me know your views on this and Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks,

Aniket