- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-03-2024 06:21 AM
Not sure what is causing this, I've modified the script of a server-side UI Action but whenever the UI Action is used it weirdly re-directs to the previous page despite there being no call to redirect.
Removing the GlideRecord call and query and reverting back to the original 4 lines (ori) will fix it, but I don't see why a simple GlideRecord check would cause such an issue.
Might just be something I'm not seeing.
var prjGlid = new GlideRecord('pm_project_task');
prjGlid.addQuery('parent', current.sys_id);
prjGlid.addEncodedQuery('short_description=Initiation Phase Completion^key_milestone=true^active=true');
prjGlid.query();
// get row count for returned records from query
var openGateCount = prjGlid.getRowCount();
if(openGateCount > 0)
{
gs.addInfoMessage('ERROR row count is: ' + openGateCount);
}
else
{
current.phase = 'planning'; // ori
current.state = '2'; // ori
current.update(); // ori
action.setRedirectURL(current); // ori
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-03-2024 06:53 AM - edited 07-03-2024 06:55 AM
Looks like you also need a
action.setRedirectURL(current)
in the first if statement block so the message shows and you stay on the form and let the message display.
e.g.
if(openGateCount > 0)
{
gs.addInfoMessage('ERROR row count is: ' + openGateCount);
action.setRedirectURL(current);
}
else
If that doesn't fix it, set a breakpoint at the first line and step it through using the script debugger, to work out if it is erroring in anyway.
One other tip is to use GlideAggregate rather than getRowCount() it's more efficient. g and let the message displayetRowCount() is inferior due to the fact that it iterates the entire record set in order to count the elements
Regards
Paul
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-03-2024 06:53 AM - edited 07-03-2024 06:55 AM
Looks like you also need a
action.setRedirectURL(current)
in the first if statement block so the message shows and you stay on the form and let the message display.
e.g.
if(openGateCount > 0)
{
gs.addInfoMessage('ERROR row count is: ' + openGateCount);
action.setRedirectURL(current);
}
else
If that doesn't fix it, set a breakpoint at the first line and step it through using the script debugger, to work out if it is erroring in anyway.
One other tip is to use GlideAggregate rather than getRowCount() it's more efficient. g and let the message displayetRowCount() is inferior due to the fact that it iterates the entire record set in order to count the elements
Regards
Paul
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-03-2024 07:08 AM
Hi @Paul Curwen,
That missing action was the trick, greatly appreciate spotting that.
Could you demonstrate how you'd utilize GlideAggregate in this example? Looks like I'd use it in a similar way to GlideRecord.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-03-2024 07:18 AM
Hi, Glad it worked for you 😊, please mark the answer as Correct that would be appreciated.
Regarding using GlideAggregate, these are decent write-ups on it here:
https://developer.servicenow.com/blog.do?p=/post/glideaggregate/
https://www.karteekn.com/gliderecord-glideaggregate-and-when-to-chose-one-over-the-other/
Basically for your scenario you want to use the COUNT option of GlideAggregateit will just make your code run much more efficiently rather than using getRowCount();
Regards
Paul
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-03-2024 08:08 AM
Got GlideAggregate working! Will definitely use this for aggregate queries moving forward. Appreciate the help.