Service operation workspace- Under the interaction if All Associated records closed

keerthana10
Tera Contributor

Hi Al,

 one interaction i can have one or many incident, change , problem or request attached to this interaction.

The interaction must be closed if all incident .problem, change and  req are closed.

I tried this BR but unfortunately it s not working 

 

var result = [];
var array = [];
var ref = [7,3,107];
var gr = new GlideRecord ('interaction_related_record');
gr.addQuery('task',current.sys_id);
gr.query();


while (gr.next())
{
array.push(gr.state);
}
if(array.length > 0)
{
for(var i=0;i<array.length;i++)
{

var arrayUtil = new ArrayUtil();
if(arrayUtil.indexOf(ref, array[i])==-1)
{
result.push(array[i]);
}
}
}
if(result.length == 0)
{
var inter = new GlideRecord ('interaction');
inter.addQuery('task',current.sys_id);
inter.query();

if(inter.next())
{
inter.state='closed_complete';
inter.update();
}
}
})(current, previous);

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@keerthana10 

business rule is on which table and what are you trying to achieve here by using that array?

try this

I assume after update business rule on task table

// Business Rule: After Update on 'task'
// Condition: current.state changes

// Define your closed state values
var closedStates = ['7', '3', '107']; // Use strings for comparison

// Find all interaction_related_record entries that reference this task
var irrGR = new GlideRecord('interaction_related_record');
irrGR.addQuery('task', current.sys_id);
irrGR.query();

while (irrGR.next()) {
    var interactionId = irrGR.interaction; // Adjust if your field is named differently

    // Check all related tasks for this interaction
    var allClosed = true;
    var relatedGR = new GlideRecord('interaction_related_record');
    relatedGR.addQuery('interaction', interactionId);
    relatedGR.query();

    while (relatedGR.next()) {
        var taskGR = new GlideRecord('task');
        if (taskGR.get(relatedGR.task)) {
            if (closedStates.indexOf(taskGR.state.toString()) === -1) {
                allClosed = false;
                break;
            }
        }
    }

    // If all related tasks are closed, close the interaction
    if (allClosed) {
        var interactionGR = new GlideRecord('interaction');
        if (interactionGR.get(interactionId)) {
            interactionGR.state = 'closed_complete'; // Or your closed state value
            interactionGR.update();
        }
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

6 REPLIES 6

@keerthana10 

so if it's not working then what debugging did you perform?

Did you add gs.info() and see where it's failing?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar  . this code is working fine now , i am just change the AFTER INSERT BR. 

 

thanks for your support.