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

I have this in UI Action script but it doesnt work and I cant see anything in log:

var description = new GlideRecord('u_pd_expiration');
	description.addQuery('u_name', current.name);
	description.query();

	while (description.next() ) {
		gs.log("Match found - sys ID " + description.getUniqueValue());
	var redirect = 	"u_test_flow_list.do?sys_id" + description;
		top.window.open(redirect,"_blank");
	}

Probably I have something wrong there or missing a function.

 

edit: there is a wrong table but its from PDI. The button doesnt react.

You want to redirect to "u_test_flow" list or "u_pd_expiration" list?

 

Best Regards
Aman Kumar

It was part of a first code redirect should be to "u_pd_expiration" list but we already found the solution with Ankur for server-side script so it is not neccessary to solve client-side if it is too much work, thank you.

Glad it worked for you, feel free to mark helpful if my response gave you insight 🙂

Best Regards
Aman Kumar