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

Hi Jaspal,

I tried gs.action.setRedirectURL(url); 

doesn't work either šŸ˜ž

I added a button but cannot see it in the Service Portal only in the backend and when I click it it does not work either. I tried it with w3 school url.

 

Something strange just happened in Service Portal, I update the record again to trigger the BR  and after BR I stayed on the form but I hit form reload and the w3 schools website loaded. 

the current record url was in the adddress bar so not sure but is this giving you any other ideas.

 

 

 

 

Hi Ellie,

 

gs.setRedirect('www.google.com'); actually takes to the <instance url>/www.google.com. So ideally you should provide string as url without the instance url stuff. so your code will be go like...

var url = '/lpcx?id=lf&table=some_data&filter=number' + current.sys_id + '&view=sp';

gs.setRedirect(url);

One more thing, somehow on my Dev org I am unable to make it working. It behaves as below.

On table record Insert Display, I wrote a BR to redirect to '/xyz'. On display of that records the BR gets executed, but it does not go to ../xyz. In fact on refreshing the page / reloading the page it goes to the ../xyz url.

Hope, I am able to explain the behavior here.

 

Chirag A

Servicenow Dev

aavenir.com

Hi Chirag,

 

I have exactly the same situation on page reload the redirect address is displayed.

Do you know if we can send a form reload with the BR and if so do you know how?

Thanks

Hi Ellie,

 

Looks like I found the 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();
	}
}

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