Client Script - Get value of boolean reference field

jenritchie
Tera Contributor

Hi all,

I have a request to require a related problem record before priority 1 and 2 incidents can be resolved, which we've accomplished with an onSubmit client script. But I'd like to restrict this to certain groups, while also making it easy for groups to 'opt in/out' of the functionality in the future. I created a new true/false field on the sys_user_group table, named u_require_problem, but I'm not able to pull the value of the field into my script.

I added the field to the form and plan to hide it with a UI Policy, and have validated that the field is checked on the group for my test incidents.

Lines 6 & 7 are where I'm trying to get the value of the new field so I can use it in line 9. The script successfully prevents resolution for P1/P2s with no related problem, but I haven't been able to successfully populate my probMgmt variable.

function onSubmit() {

            //If p1 or p2 incident state == resolved and problem ID is blank

            var pri = g_form.getValue('priority');

            var state = g_form.getValue('state');

            var prob = g_form.getValue('problem_id');

            var probMgmt = g_form.getValue('u_require_problem');

            alert(probMgmt);

                         

              if(state == '6' && (pri == '1' || pri == '2') && prob == ''){

                              alert('A problem is required for all P1 and P2 incidents.');

                              return false;

                              }

I've tried getValue, getDisplayValue and getBooleanValue (referenced in GlideForm (g form) - ServiceNow Wiki), getBooleanValue returns a 'not a function' error on the form and the others return a null value. Is there an easier way to accomplish this?

1 ACCEPTED SOLUTION

Michael Ritchie
ServiceNow Employee
ServiceNow Employee

The issue is likely because the field you are trying to access is in the related group table, I assume you are just showing this value on the form by dot walking. You really don't need it on the form to accomplish what you need. But you will need to get the related group record to check the value. Out of the box there is a client script on Incident that populates the incident location value with the callers location. I would look at that script, specifically how it's using getReference to get the caller user record to lookup the location value. You can use a similar approach to get the u_require_problem value from the group record. Another option is to make an Ajax call to lookup the value.


View solution in original post

9 REPLIES 9

Nevertheless, getReference() practically unexpectedly more efficient that Wiki tells.Proved by Experience.


Let me try to explain why the callback function did not work...



The glidereference with a callback function is similar to glideajax with getXML function.. The client script makes a call to the server but since callback function is async in nature, it will not wait for the response from the server and the form continues to the submission. So there are cases where you will need to pick and choose between a async call and sync call ...



Usually, onload function are better left to async calls and onsubmit with sync ones...



Long story short, use a getreference without callback function in onsubmit script ....


True, getReference is the function that should answer this question.


Capture.PNG


getReference() function requires addition time, please use Glide Ajax to call Script Include.


Yes - you are right - getReference() requires additional time(theoreticaly, but practicaly - behaviour is different time to time) - but UI Action also takes time to connect to server. I just proposing a solution - let the person who asks to   define what will suite him the best.