UI Action redirects to previous page

MBarrott
Mega Sage

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
}

 

 

 

1 ACCEPTED SOLUTION

Paul Curwen
Giga Sage

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

***If Correct/Helpful please take time mark as Correct/Helpful. It is much appreciated.***

Regards

Paul

View solution in original post

4 REPLIES 4

Paul Curwen
Giga Sage

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

***If Correct/Helpful please take time mark as Correct/Helpful. It is much appreciated.***

Regards

Paul

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. 

Paul Curwen
Giga Sage

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.servicenow.com/community/developer-blog/improving-script-performance-with-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();

***If Correct/Helpful please take time mark as Correct/Helpful. It is much appreciated.***

Regards

Paul

Got GlideAggregate working! Will definitely use this for aggregate queries moving forward. Appreciate the help.