Prevent Recursive loop script

Pat Surtan
Tera Expert

Hello,

 

There is a parent/child feature in Incident Management where a record can be added to an incident as that incident's parent record. If the parent record has been added, that same record cannot be added as a child. This is a OOTB recursive script.


I want to create this same feature to prevent recursive loop in my custom table. Can someone provide a script for me to use? I found the OOTB script, modified it but it doesn't work. No errors and I can add one ticket as both the parent and a child, which isn't correct. 

 

Here is the OOTB script I modified.

 

var rt = new RecursionTester('u_additional_info', 'u_parent_info');
if (rt.isRecursive(current)) {
current.setAbortAction(true);
current.u_parent_info.setError(gs.getMessage('Invalid Parent Info'));
gs.addErrorMessage(gs.getMessage('The selected Parent Info loops back to this record (recursive sdq ticket loop)'));
}

 

Thanks in advance. 

1 ACCEPTED SOLUTION

Mike_R
Kilo Patron
Kilo Patron

is 'u_additional_info' your custom table name?

 

var rt = new RecursionTester('tableName', 'parentReferenceFieldName');
if (rt.isRecursive(current)) {
current.setAbortAction(true);
current.parentReferenceFieldName.setError(gs.getMessage('Invalid Parent Incident'));
gs.addErrorMessage(gs.getMessage('The selected Parent Incident loops back to this record (recursive incident loop)'));
}

View solution in original post

8 REPLIES 8

Mike_R
Kilo Patron
Kilo Patron

is 'u_additional_info' your custom table name?

 

var rt = new RecursionTester('tableName', 'parentReferenceFieldName');
if (rt.isRecursive(current)) {
current.setAbortAction(true);
current.parentReferenceFieldName.setError(gs.getMessage('Invalid Parent Incident'));
gs.addErrorMessage(gs.getMessage('The selected Parent Incident loops back to this record (recursive incident loop)'));
}

Hi Mike,

 

Yes, "u_additional_info" is my custom table and "u_parent_info" is my parent reference field. I believe the code is correct but its not working. I can add a record as the parent and still add it as a child for another ticket. 

1 .Check to make sure the script include is accessible from All Application Scopes

Mike_R_0-1667309173460.png

2. Check the errors logs for any errors.

 

3. Check the cross scope privileges to make sure there is nothing denied between scopes

instanceName.service-now.com/sys_scope_privilege_list.do?sysparm_query=&sysparm_view=

 

4. Run your code from a background script to see if it works there. Obviously you'll need to tweak the code first before running in a background script

Hi Mike,

 

1. The script include has the same setting as your screenshot. I never changed it.

2. Is there a specific error I should be looking for? Keywords to search for?

3. There isn't a separate application. I created this in the Global scope so it should be okay. 

4. I ran the code in the background script and got this error:'

 

[0:00:00.383] Script completed in scope global: script


Script execution history and recovery available here


Evaluator: com.glide.script.RhinoEcmaError: Cannot read property "u_parent_info" from null
   script : Line(16) column(0)