Business rule action.SetRedirectURL(url); or gs.setRedirect(url); not working for me

Ellie2
Kilo Expert

Hi All,

I have a business rule that creates 1 or more of records in a related table so far so good.

I need to redirect to the record list in the related table when all records have been created and I cannot get the redirect to work at all.

This is for Service Portal but doesn't work in the backend either.

(function executeRule(current, previous /*null when async*/) {

var catPerData = current.somedata.toString();
var PerData = catPerData.split(',');


for (var i = 0; i < PerData.length; i++){
var gr = new GlideRecord ('some_data');
gr.initialize();
gr.number = current.sys_id;
gr.some_data = PerData[i];
gr.name = gr.some_data.name;
gr.insert();
}

var url = gs.getProperty('glide.servlet.uri') + 'lpcx?id=lf&table=some_data&filter=number' + current.sys_id + '&view=sp';

action.SetRedirectURL(url);
// gs.setRedirect(url);

})(current, previous);

Has anyone got any idea on what might be the issue?

Thanks!

 

1 ACCEPTED SOLUTION

Chirag A
Kilo Expert

Hi Ellie,

Looks like this can be your solution.

Create a BR with below Code and a Client Script code as below. Both together works for me...

 

//BR

(function executeRule(current, previous /*null when async*/) {

	// Add your code here
	gs.setRedirect('/xyz');
	
})(current, previous);

 

Client script UIType ALL, Type OnLoad

function onLoad() {
	if (location.pathname.indexOf('xyz')==1){
		//no reload needed
	}else{
	location.reload();
	}
}
 

Please mark my solution, if that works for you.

 

Chirag A

Servicenow Dev

aavenir.com

View solution in original post

17 REPLIES 17

Mark Roethof
Tera Patron
Tera Patron

Hi there,

Looks like a minor typo, use:

action.setRedirectURL(url);

Lowercase s.

Obviously, if url is valid šŸ™‚ You could add some debugging to check url.

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Ellie2
Kilo Expert

Hi Mark,

Thanks I corrected this but its still not working.

When I click Save and the BR runs I can see that the records are added to the related list at the bottom of the form the related list link numbers increment but I stay on the form.

So did you debug if the URL is valid? And where exactly are you using / testing this. Your first message says service portal though also business rule?
Just server side System UI, this should work fine.

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Ellie2
Kilo Expert

I did I also displayed the url in a message and copy/paste into new window and list is displayed

I also tried with test url  var url = "http://www.w3schools.com";  and it does not redirect.