redirection to the same record

kunal16
Tera Expert

Hi,

I have a requirement that if a non-admin user tries to save/update an existing project record as a template (Template - Save as Template) with the help of OOB context menu, an error message should pop-up as "Template creation has been disabled for Projects". Also, the page should redirect to the current project form.

Below is my business rule that I have written on the template table for this restriction:

Name: Template Restriction

Table: Template (sys_template)

When to Run: Before Insert and Update

Condition: !gs.hasRole('admin')

Script:

function onBefore(current, previous)

{

  //This function will be automatically called when this rule is processed.

  var name = current.table;

  if(name == 'pm_project')

  {

            var url = 'pm_project.do?sys_id' + name.sys_id;

            gs.addErrorMessage("Template creation has been disabled for Projects");

            current.setAbortAction(true);

            action.setRedirectURL(url);

  }

  if(name == 'pm_project_task')

  {

            var url2 = 'pm_project_task.do?sys_id' + name.sys_id;

            gs.addErrorMessage("Template creation has been disabled for Project Tasks");

            current.setAbortAction(true);

            action.setRedirectURL(url2);

  }

}

I am able to restrict the save as a template but unable to bring back user to the current project form. Kindly help!!

1 ACCEPTED SOLUTION

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Kunal,



gs.setRedirect will also not work in this specific use case what you have as the post processing submission is on the template table.


You have to adjust the condition in the UI context menu from where the template action take place.



On another note, there is an error here in this line of code


var url2 = 'pm_project_task.do?sys_id' + name.sys_id;   should be var url2 = 'pm_project_task.do?sys_id =' + name.sys_id;


I hope this helps


View solution in original post

6 REPLIES 6

srinivasthelu
Tera Guru

Hi Kunal,



use gs.setRedirect instead of action.setRedirect



gs.setRedirect(url2);


Srinivas is right. The action.setRedirectURL() is only for UI actions I believe. There should be some warning/error in the Logs if you try to use that for a business rule.



Warren


Hi Srinivas,


It seems even after using gs.setRedirect(url) and gs.setRedirect(url2), the user is not getting redirected to the Project/Project task page.


Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Kunal,



gs.setRedirect will also not work in this specific use case what you have as the post processing submission is on the template table.


You have to adjust the condition in the UI context menu from where the template action take place.



On another note, there is an error here in this line of code


var url2 = 'pm_project_task.do?sys_id' + name.sys_id;   should be var url2 = 'pm_project_task.do?sys_id =' + name.sys_id;


I hope this helps