- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-21-2023 03:01 PM
Hey Guru's,
I've written a Before update Business Rule that should run before my "VP Review" Incident task is closed. The Script I have written checks for any other incident tasks that are open, and have the same parent "Incident" record. If there are any open, then prevent the update from taking place. Below is a image of the code I have written so far. The problem I am having is that it does not seem to be making it to the If statement and never cancels the submission. I've confirmed the line with "incidentTask.addQuery('incident', current.incident.number);" is returning the incident I want, however it does not seem to resolve the query. any help with this is much appreciated!
Script:
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-22-2023 07:00 AM
I was able to fix the code, below is the finished working product.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-21-2023 04:32 PM
Hi, this appears to be the same question\issue that you raised here
Prevent Submission of Incident Task when Incident ... - ServiceNow Community
There is no need to create multiple threads for the 1 question\issue and multiple threads\questions are less likely to result in a clear outcome.
If you want code reviewed then you should provide the code as plain text, as it is not possible to properly (or quickly) evaluate a screenshot. Clear details of your configuration (your screen-shot does not indicate when\how the BR is triggered), your debugging and diagnostics would also help the community understand your issue; as it's broken\doesn't work provides no useful information.
Reviewing your comments and screenshot,
I would start diagnostics by adding logging at the beginning of your BR to ensure that it runs, or confirm this by enabling BR debugging.
Then, log u_major_incident_task_type value and typeof, before your if statement in order to ensure that the value and data type matches your conditions expectations.
Looking at your query
addQuery('incident', current.incident.number);
will not work as the platform is expecting the sys_id of the incident, and to reference 'number' this would need to be
addQuery('incident.number', current.incident.number);
The encodedQuery doesn't need to check multiple state conditions as you can simply use 'active=true^'
I also think you also need to exclude the current record in the query, otherwise you will always get a result even if there are no other active incident_tasks for the incident.
As a solution I would replace the multiple queries with 1 encoded query, something like
var myQuery = 'active=true^incident=' + current.incident.sys_id + '^sys_id!=' + current.sys_id;
var incidentTask = new GlideRecord('incident_task');
incidentTask.addEncodedQuery(myQuery);
incidentTask.query();
if(incidentTask.hasNext()) {
gs.info('I have a result');
} else {
gs.info('No matching Incident tasks');
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-22-2023 06:13 AM - edited 06-22-2023 06:17 AM
so I've logged plenty of stuff, I thought that would have been evident in the code I supplied. I've confirmed and logged already that I am getting back the incident number, the field "u_major_incident_task_type" is returning the expected value. however the query itself returns "[object GlideRecord]". As I said before the If Statement does not resolve as the code appears to stop running at that point. I also want this BR to run based off the current incident task, as this is the only incident task that will ever trigger the BR to run. if anything else is open, we should stop submission of the request.
here's the code itself if you want to actually go through it.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-22-2023 06:16 AM
since incident field is reference you should use current.incident
update line 7 as this current.incident
Also did you check your incident task record has incident field populated with parent or parent field
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-22-2023 07:00 AM
I was able to fix the code, below is the finished working product.