How to configure auto close functionality of an interaction Once all associated tasks are closed?

HARI KISHAN GVS
Mega Sage

Hi Team,

i need to build a businessrule on Incident and requested item tables  or on Interaction table to check all the related tasks are closed or not before closing the interaction.

i.e.

1. Create an Interaction.

2. create an Incident from that Interaction.

3. create a request from the same interaction.

4. Close the Incident. Here my Businessrule should check whether remaing tasks in the interaction is closed or not. if closed, close the interaction along with the incident. if not, interaction should stay in work in progress state.

5. Close the request. Here my Businessrule should check whether remaing tasks in the interaction is closed or not. if closed, close the interaction along with the requested item. if not, interaction should stay in work in progress state.

6. Once all related tasks are complete interaction should close automatically.

Note: we can create multiple incidents, requests from a single interaction.

 

my issue building a business rule is, there is no reference field refer to Interaction table is available in Incident and Requested Item Tables. Related Tasks are getting populated in interaction via 'Related Tasks' Relationship. and this relationship is using 'interaction_related_record' table and Interaction and Task tables to pull the related tasks.

i don't have enough experience to build this kind of queries and  script to check these kind of scenarios. 

can anyone please provide the sample script for it.

Thanks in advance.

Hari Kishan.

 

1 ACCEPTED SOLUTION

HARI KISHAN GVS
Mega Sage

Hi Hari,

it would require two business rules. one is on the Task table and another one on the interaction related record table.

1. create a field called 'Interaction(u_interaction) on the Task table.

2. Create a Business rule on interaction related record table (before insert update)to copy the interaction field to task table.

3. Create a business rule on task table (after update and Active changes to false)to set the interaction to closed complete once all related task are in active false state.

(function executeRule(current, previous /*null when async*/) {
      var result = [];
      var array = [];
      var ref = [7,3];
      var gr = new GlideRecord ('task');
          gr.addQuery('u_interaction',current.u_interaction);
          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('sys_id',current.u_interaction);
            inter.query();
      if(inter.next())
   {
   inter.state='closed_complete';
    inter.update();
 }
 }
 })(current, previous);
(function executeRule(current, previous /*null when async*/) {

	var gr = new GlideRecord('task');
	gr.addQuery('sys_id', current.document_id);
	gr.query();
	if(gr.next()){
		gr.u_interaction = current.interaction;
		gr.update();
	}
	var gr1 = new GlideRecord('interaction');
	gr1.addQuery('sys_id', current.interaction);
	gr1.query();
	if(gr1.next()){
		gr1.state = 'work_in_progress';
		gr1.update();
	}
	

})(current, previous);

View solution in original post

6 REPLIES 6

HARI KISHAN GVS
Mega Sage

Hi Team,

i worked out solution how to solve this. but can't have enough experience on how to put that to script.

solution:

1. write a business rule on Task table

2. in BR, query interaction related record table for the task

3. then, get the interaction record sys_id

4. then, query the interaction related record table  for the interaction sys_id

5. then, loop through results, check for active on the task

6. if all tasks are inactive, close the interaction, if not leave it alone

Can anyone please provide me the script for it.

Thanks in advance!

Hari Kishan.

HARI KISHAN GVS
Mega Sage

Hi Hari,

it would require two business rules. one is on the Task table and another one on the interaction related record table.

1. create a field called 'Interaction(u_interaction) on the Task table.

2. Create a Business rule on interaction related record table (before insert update)to copy the interaction field to task table.

3. Create a business rule on task table (after update and Active changes to false)to set the interaction to closed complete once all related task are in active false state.

(function executeRule(current, previous /*null when async*/) {
      var result = [];
      var array = [];
      var ref = [7,3];
      var gr = new GlideRecord ('task');
          gr.addQuery('u_interaction',current.u_interaction);
          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('sys_id',current.u_interaction);
            inter.query();
      if(inter.next())
   {
   inter.state='closed_complete';
    inter.update();
 }
 }
 })(current, previous);
(function executeRule(current, previous /*null when async*/) {

	var gr = new GlideRecord('task');
	gr.addQuery('sys_id', current.document_id);
	gr.query();
	if(gr.next()){
		gr.u_interaction = current.interaction;
		gr.update();
	}
	var gr1 = new GlideRecord('interaction');
	gr1.addQuery('sys_id', current.interaction);
	gr1.query();
	if(gr1.next()){
		gr1.state = 'work_in_progress';
		gr1.update();
	}
	

})(current, previous);

Nishchitha K S
Tera Guru

@HARI KISHAN GVS Hello Hari,
Your steps mentioned were very helpful for me.
Although while performing the same, I felt that the step 3. Create a business rule on task table (after update and Active changes to false)to set the interaction to closed complete once all related task are in active false state, script can be more enhanced using GlideAggregate API.
Below is the code for the same:

 

 

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

//Check Active Related Tasks
var relatedTasks = new GlideAggregate('task');
relatedTasks.addQuery('u_interaction',current.u_interaction); //I made use of correlation-id field to avoid licensing issue.
relatedTasks.addQuery('active',true); //can use addActiveQuery() as well.
relatedTasks.addAggregate('COUNT');
relatedTasks.query();
while(relatedTasks.next()){
//If no Active tasks, update interaction state to closed complete
      if(relatedTasks.getAggregate('COUNT')==0){
            var interGr = new GlideRecord('interaction');
            interGr.addQuery('sys_id',current.u_interaction);
            interGr.query();
            if(interGr.next()){
                  interGr.active=false;
                  interGr.state='closed complete';
                  interGr.work_notes = 'All related tasks are closed.';
                  interGr.setWorkflow(false);
                  interGr.autoSysFields(false);
                  interGr.update();
           }
     }
}
})(current, previous);

 

 

 

 

 

 

 

Please mark my answer as helpful, if anyone did find it.Happy learning šŸ™‚
Thanks & Regards,
Nishchitha K S

 

 

 

 

How do you set up ServiceNow to automatically close an Interaction if there are no other tasks associated with it?  We have an application the will automatically create an interaction when an alert is presented. Today we had to manually close over 500 Interactions manually! Any ideas?