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

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

the table u_pd_expiration has any field which refers u_application_catalog table?

you can use that to redirect

var redirect = "u_pd_expiration.do?u_field_name=" + g_form.getUniqueValue();
top.window.open(redirect,"_blank");

Regards
Ankur

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

Yep, but it is not reference but just copy so its "name" and "u_name". I´ve tried

var redirect = "u_pd_expiration_list.do?u_number" + g_form.getUniqueValue("number");  top.window.open(redirect,"_blank");

but it doesnt work.

Hi,

update as this

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

@Jan JanouÅ¡ek 

Check my above response and it should work fine

Regards
Ankur

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