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

Hi Robert, nice, would you mind trying the following and let me know what do you get in the two alerts you're going to get? I'm sure that once we have this we're one step away of resolving the problem



function onSubmit() {


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


    alert('User has role ITIL:' + g_user.hasRole('itil'));


    alert('The impact value is:' + g_form.getValue('impact'));


    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;


   


 


}


Berny,



So apparently the script is being triggered when I'm the admin but not when I'm Joe Employee. I must've forgotten to validate for both of them before. See my response to Cory below for more details.


Hi Robert,



I'd also like to know what happens when you use Berny's version. It should give you three alerts:


User has role ITIL:false



The impact value is:1



Record submission aborted because it is high priority and you do not have itil role



If the first two alerts are different than above, that indicates some cache didn't get cleared, or a value isn't what is seems to be.



If the alerts are exactly the same as above, then something weird is going on.


Ok so here's the deal: what's happening is that the script is running when I'm an admin but it's not when I'm Joe Employee. I thought I checked both of them before when I commented out the if condition as Berny suggested but I guess I didn't. Absolutely nothing happens when I submit an incident as Joe Employee, but I get those alerts as the admin if I put them before the if condition, and if I comment them out I get the additional alert from the original script and the incident won't submit. Do you know what might be happening?


Hi Robert,



I have a strange feeling you are going through a record producer to create the incident when you are Joe Employee, and going through the Incident form itself when you are admin.



Does it trigger when, while impersonating Joe Employee, you navigate directly to /incident.do?sys_id=-1, fill out the form, and try to submit?