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 Chirag, I changed the above to on submit to prevent a loop but it would work. This just confirmed that there is no reload from server and I will as the widget developer to take a look at this.

I will accept your answer as correct.

Hi Chirag,

 

This solution is good but this will not work while we are using current.setAbortAction(true);. if you have any use case of this please let us know.

 

Ansh Mahajan

ServiceNow Dev

Hi @Chirag A ,

what do you mean by xyz in gs.setRedirect ('/xyz'), I have wrote a Business Rule that create a problem when incident state changes to On Hold and I want that after saving, the page redirecte to the new Problem Record created, could you help me please, below the script

Thank you

 

(function executeRule(current, previous /*null when async*/) {
   
    var gr = new GlideRecord('problem')
    gr.initialize();
    gr.short_description = current.short_description;
    gr.description = "Problem created from incident "+current.number+" which its State changed to On Hold";
    gr.assignment_group = current.assignment_group;
    gr.insert();
    gs.addInfoMessage("Problem "+gr.number+" is created after changing incident "+current.number+" to On Hold");
    gs.setRedirectURL(gr);
}
)(current, previous);