- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-17-2025 04:55 AM
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);
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-17-2025 05:08 AM - edited 06-17-2025 05:28 AM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-17-2025 06:31 AM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-19-2025 12:36 AM
Hi @Ankur Bawiskar . this code is working fine now , i am just change the AFTER INSERT BR.
thanks for your support.