Why is my client side abort record submission script not working?

bookman1502
Giga Expert

I've been doing these exercises in my personal instance to improve my ServiceNow skills. I'm working on "Stop the submission of any Priority 1 incident created by a user without the 'itil' role. Alert the user when this happens." This page is from 2010, and so it's a little dated and nowadays personal development instances come out of the box giving non-itil users only the ability to edit the impact, so I that's what I used in the below script:

function onSubmit() {

    //check to see if user doesn't have itil and if impact is 1

    if(!g_user.hasRole('itil') && g_form.getValue('impact') == 1){

          alert('Record submission aborted because it is high priority and you do not have itil role');

          //Make sure dirty form still works

          g_form.submitted = false;

        //Abort submission

        return false;

}

   

}

For whatever reason, whenever I impersonate Joe Employee (who has no roles), I can still submit an incident with an impact of 1. Can someone help me out here?

Thanks

1 ACCEPTED SOLUTION

Hi Robert,



You are going to want your script in two places- on the Incident form itself (because navigating by URL is more common than you think), and on the Record Producer itself.



Revert your script to the working version you had before- the one where Admin could submit- and test (again, on /incident.do?sys_id=-1) with Joe User to make sure it blocks correctly. Keep that on the Incident form.



Then go to Service Catalog > Catalog Policies > Catalog Client Scripts. Create a New script with a similar name to the one on the Incident form. It should apply to "A Catalog Item", be Active, and UI Type "Desktop".


Type is still onSubmit, and the Catalog Item is "Create Incident" (that's the one Joe Employee uses out-of-box when he clicks on "New" from the Incident list).



The script will be basically the same:



function onSubmit() {


      if(!g_user.hasRole('itil') && g_form.getValue('impact') == 1){


              alert('Record submission aborted because it is high priority and you do not have itil role');


              //Make sure dirty form still works


              g_form.submitted = false;



              //Abort submission


              return false;


      }


}



That should cover both cases where that user might be submitting an incident.


View solution in original post

13 REPLIES 13

Yes, it does. So how would I make the script work for the standard way of accessing it then? After all, in a real situation the end user will create an incident from the left nav bar. Do I just apply the script to the table of that record producer? If so, how do I find out what table that is?


Hi Robert,



You are going to want your script in two places- on the Incident form itself (because navigating by URL is more common than you think), and on the Record Producer itself.



Revert your script to the working version you had before- the one where Admin could submit- and test (again, on /incident.do?sys_id=-1) with Joe User to make sure it blocks correctly. Keep that on the Incident form.



Then go to Service Catalog > Catalog Policies > Catalog Client Scripts. Create a New script with a similar name to the one on the Incident form. It should apply to "A Catalog Item", be Active, and UI Type "Desktop".


Type is still onSubmit, and the Catalog Item is "Create Incident" (that's the one Joe Employee uses out-of-box when he clicks on "New" from the Incident list).



The script will be basically the same:



function onSubmit() {


      if(!g_user.hasRole('itil') && g_form.getValue('impact') == 1){


              alert('Record submission aborted because it is high priority and you do not have itil role');


              //Make sure dirty form still works


              g_form.submitted = false;



              //Abort submission


              return false;


      }


}



That should cover both cases where that user might be submitting an incident.


Thanks, creating the catalog client script worked.


shalinichiluka
Kilo Contributor

Hello All,



I also had the same requirement. Everything works fine.



But when I select to priority as 2 and submit via the Record Producer it creates an incident with priority 5. For any priority selected except 1, as both Sys admin and Joe Employee, the priority on the incident form shows as 5 by default.


find_real_file.png



  • I updated the default value to be empty
  • The Fields are mapped correctly on the Record Producer
  • added the line under Record Producer script 'current.priority = producer.priority'(screenshot)

    find_real_file.png


It does not work still. Not sure if there are any BR or other Client Scripts running. Please explain