Auto close the interaction record when the associated inc/req is closed

suryaogeti1
Tera Contributor

we have a requirement, when the incident or request is closed, automatically the interaction record needs to close. Already we have a Ui button in SOW interaction form, when we click the button in the interaction form, the incident or request will create. here in incident the associated interaction record is updating in the work notes. now the client wants additional changes which i mentioned in the first line. how can we achieve, please help with the process and the script. 
Thanks in advance

1 ACCEPTED SOLUTION

HI @suryaogeti1 ,
1.The configuration should be like this , add filter conditions as per requirement
Screenshot 2026-04-15 at 9.55.16 PM.png

2.Script:

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

	var gr=new GlideRecord("interaction_related_record");
	gr.addQuery("document_table","sc_request");  //updated table name
	gr.addQuery("document_id",current.sys_id);
	gr.query();


	if(gr.next())
	{
		var interaction=new GlideRecord("interaction");
		if(interaction.get(gr.getValue("interaction")))
		{
			interaction.state="closed_complete";
			interaction.update();
		}
		
	}


})(current, previous);



View solution in original post

11 REPLIES 11

DB1
Tera Contributor

Hi @suryaogeti1 ,

Try this 

// Business Rule After update on Incident table
// When Incident is closed, close associated Interaction

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

// Check if Incident is moving to Closed
if (current.state == 7 && previous.state != 7) { // 7 = Closed

// Get the associated Interaction record
var interactionId = current.u_interaction; // assuming you have a reference field
if (interactionId) {
var interactionGR = new GlideRecord('interaction'); // table name may be 'interaction' or custom
if (interactionGR.get(interactionId)) {
interactionGR.state = 7; // set to Closed
interactionGR.update();
}
}
}

})(current, previous);

Thanks,

BB

Rakesh_M
Tera Guru

Hi @suryaogeti1 ,
1.Create a After Update business rule on incident.
Screenshot 2026-04-15 at 3.53.35 PM.png

2.Use the following script

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

	var gr=new GlideRecord("interaction_related_record");
	gr.addQuery("document_table","incident");
	gr.addQuery("document_id",current.sys_id);
	gr.query();



	if(gr.next())
	{
		var interaction=new GlideRecord("interaction");
		if(interaction.get(gr.getValue("interaction")))
		{
			interaction.state="closed_complete";
			interaction.update();
		}
		
	}

})(current, previous);

Hi @Rakesh_M 

can we create additional field named 'interaction' in the incident form or how the code will check the associated record is present or not in the incident form. how it will read the associated interaction record. please elaborate if possible or give me the guidance.

suryaogeti1_0-1776251588719.pngsuryaogeti1_1-1776251697782.png

 

Hi @suryaogeti1 ,

  1. When an Incident is created from an Interaction, a record is automatically created in the interaction_related_record table.
  2. This table acts as a bridge between the Interaction and related records like Incident, Request, or Change.
  3. The relationship is maintained using key fields:
    • Interaction → reference to the Interaction record
    • Document Table → the table name of related record(e.g., Incident)
    • Document ID  → the specific record (e.g., current Incident )
      Screenshot 2026-04-15 at 5.14.58 PM.png
      so we use this record to find the interaction record that needs to be closed when incident is resolved/closed
  4. In the script:
var gr=new GlideRecord("interaction_related_record");  //
	gr.addQuery("document_table","incident");
	gr.addQuery("document_id",current.sys_id);
	gr.query();​
  • We query the interaction_related_record table using:
    • document_table = incident
    • document_id = current.sys_id (the current Incident sys_id)
  • Executes the query and retrieves matching records from the table.
  • This gives the record that connects interaction and incident

if(gr.next())  //if record is found enter if
	{
		var interaction=new GlideRecord("interaction");  
		if(interaction.get(gr.getValue("interaction")))
		{
			interaction.state="closed_complete";
			interaction.update();
		}
		
	}​
  • If a matching record is found, then we move further else script ends here as every incident resolved/closed is not created from a interaction
  • now "gr" contains all the details of interaction_related_record
  • now we query interaction table and find interaction record then update it's state.
  • the above script is equivalent to following script:
if (gr.next()) {
    var interaction_sysID = gr.getValue("interaction"); // gets sys_id of interaction

    var interaction = new GlideRecord("interaction");
    interaction.addQuery("sys_id", interaction_sysID);
    interaction.query();

    if (interaction.next()) {
        interaction.state = "closed_complete";
        interaction.update();
    }
}

So, we do not need to create an additional field on the Incident form. The association is already maintained through the interaction_related_record table, and the script uses this relationship to find and update the corresponding Interaction.