The CreatorCon Call for Content is officially open! Get started here.

Get sys_id from UI Action script to set URL

Jan Janou_ek
Tera Expert

Hello guys!

I have two tables - "u_application_catalog" which is the source and "u_pd_expiration" where I have a copy of Name and description. I have a BR to check if anything changes in the genuine table and modify then also in the second one.

BR is:

(function executeRule(current, previous /*null when async*/) {
	
	//var new_name = new GlideRecord('u_pd_expiration');
	//new_name.addQuery('u_name', previous.name);
	//new_name.query();
	
	//while ( new_name.next() ) {
	
		//gs.log("Match found - updating " + new_name.getDisplayValue());
		//new_name.setValue('u_name', current.name);
		//new_name.update();
	//}
		
	var description = new GlideRecord('u_pd_expiration');
	description.addQuery('u_name', current.name);
	description.query();
	
	while ( description.next() ) {
	
		gs.log("Match found - updating " + description.getDisplayValue());
		description.setValue('u_description', current.short_description);
		description.update();
	}
	


})(current, previous);

I also have a new UI Action on table "u_application_catalog" which shows button to redirect to "u_pd_expiration.list" when the checkbox PD expiration is true and it work also:

Onclick: var redirect = "u_pd_expiration_list.do";     top.window.open(redirect,"_blank");

Condition: current.u_pd_expiration==true

The alternative way which is preffered is to show it by UI Policy which works ok:

If true:
function onCondition(){
$$('#show_pd_expiration_button')[0].show();
}

If false:
function onCondition() {
$$('#show_pd_expiration_button')[0].hide();
}

Now I would like to set redirect directly to matching record but I dont know how to get sys_id from the table "u_pd_expiration". I´ve tried to use a similar code from BR but I am not able to get it work. Also it would great if it could save the current record on "u_application_catalog" and then redirect.

The principle is when user checks "Personal data expiration" on the form of "u_application_catalog" the button for redirect appears and redirects direct to match record - for example CPP Connect "u_application_catalog" to CPP Connect "u_pd_expiration".

Thank you for your help.

1 ACCEPTED SOLUTION

@Jan JanouÅ¡ek 

Keep server side and use this

var description = new GlideRecord('u_pd_expiration');
description.addQuery('u_name', current.name);
description.query();
if(description.next() ) {
	var url = "u_test_flow.do?sys_id" + description.sys_id;
	action.setRedirectURL(url);
}

OR

Keep the UI action as client side

Client checkbox - true

Onclick - openURL()

Script:

function openURL(){

	var description = new GlideRecord('u_pd_expiration');
	description.addQuery('u_name', g_form.getValue('name'));
	description.query();
	if(description.next() ) {
		var redirect = 	"/u_test_flow.do?sys_id" + description.sys_id;
		top.window.open(redirect,"_blank");
	}

}

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

18 REPLIES 18

It redirects me directly to the last page. I found this in log:

 

com.glide.script.RhinoEcmaError: "g_form" is not defined.
sys_ui_action.caaaa57a979f4510b8a1fef3a253afce.script : Line(2) column(0)
1: var name = new GlideRecord('u_test_flow');
==> 2: name.addQuery('u_number', g_form.getValue('number'));

When I check "Client" on UI Action the button doesnt react at all. I can´t figure it out whats wrong.

Hi,

I thought since you are using top.window the UI action must be client side

Is the UI action client side or server side?

Client checkbox is checked?

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

It is only for backend so it could be server side I think. When i check "Client" it doesnt react.

@Jan JanouÅ¡ek 

Keep server side and use this

var description = new GlideRecord('u_pd_expiration');
description.addQuery('u_name', current.name);
description.query();
if(description.next() ) {
	var url = "u_test_flow.do?sys_id" + description.sys_id;
	action.setRedirectURL(url);
}

OR

Keep the UI action as client side

Client checkbox - true

Onclick - openURL()

Script:

function openURL(){

	var description = new GlideRecord('u_pd_expiration');
	description.addQuery('u_name', g_form.getValue('name'));
	description.query();
	if(description.next() ) {
		var redirect = 	"/u_test_flow.do?sys_id" + description.sys_id;
		top.window.open(redirect,"_blank");
	}

}

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Wow, great, server side works now I only had to add "=" to address:

"u_test_flow.do?sys_id"

Is it possible to open it in a new window? As I can see it is working only from client side, right?

Client code still doesnt react but nevermind the server side is good enough! Thank you very much!