Restrict user to submit form (via onSubmit Client script)

Elena Pomana
Tera Guru

Hello,

 

My requirement is to restrict submit option if a user is part of a certain group.

The user needs to access the catalog item and should be able to see all the variables in it (which are set to read only) but if submit button is clicked, an error should appear and the submission shouldn't be allowed.

 

Things I have tried:

Script include:

ElenaPomana_0-1676475061160.png

Client script onSubmit:

1st version:

ElenaPomana_2-1676475099136.png

2nd version:

ElenaPomana_3-1676475179119.png

I have tried with XMLWait as well but the method is no longer used.

 

Thank you.

Elena

1 ACCEPTED SOLUTION

Let me know what do you guys think, I managed to solve it this way:

Script include:

ElenaPomana_0-1676539602212.png

Client script:

ElenaPomana_1-1676539642248.png

Tested it both ways(user part of group, user that isn't part of the group) and it seems that it works as expected.

 

Thank you for all the ideas, you guys are amazing!

 

Have a great day,

Elena

 

View solution in original post

11 REPLIES 11

Shashank_18
Mega Guru

Hi @Elena Pomana - As mentioned by Allen you need to pass the user info from GlideAjax code to Script Include.

please refer this for more info link

Ankur Bawiskar
Tera Patron
Tera Patron

@Elena Pomana 

you are using asynchronous GlideAjax in onSubmit

So by the time the ajax function validates via script; the form gets submitted as it won't wait as it's async

Here are couple of approaches

1) use onSubmit with synchronous GlideAjax; this is somewhat tricky and you need to check below links for solution and enhance as per your case

https://snprotips.com/blog/2018/10/19/synchronous-lite-onsubmit-catalogclient-scripts 

https://community.servicenow.com/community?id=community_question&sys_id=acb3103a1bd6c11038739979b04b... 

OR

2) you can write onChange catalog client script on your user variable and pass the user details and then check by querying sys_user_grmember with this user and your group if record is present or not

If present then show error on variable

This will ensure that the form gets submitted when the error is not there i.e. the validation is passed

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Let me know what do you guys think, I managed to solve it this way:

Script include:

ElenaPomana_0-1676539602212.png

Client script:

ElenaPomana_1-1676539642248.png

Tested it both ways(user part of group, user that isn't part of the group) and it seems that it works as expected.

 

Thank you for all the ideas, you guys are amazing!

 

Have a great day,

Elena

 

@Elena Pomana 

Glad to know.

Please close the thread by marking appropriate response as correct so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Elena Pomana 

Did you verify the script worked for both Native + Portal view?

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader