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

Glad to help.

using server side not possible to open in new tab

only client script supports opening in new tab.

Regards
Ankur

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

I had just to add "Isolate script" to the UI Action form and unchecked it - client-side works perfectly now. Thank you again.

Aman Kumar S
Kilo Patron

Hey,

You need sys_id to redirect to a record through URL for opening the form view and  number will not do. 

Best Regards
Aman Kumar

Yes but how can I get it?

description.getUniqueValue(); // to fetch sys_id in your while loop for u_pd_expiration table gliderecord query

Best Regards
Aman Kumar