Hide fields on form (via onLoad client script) not working ...

Zod
Giga Guru

HI,

I need to hilde some fields on a form view - dependend on values of the same table, but NOT visible on the current view.

I need to use a client script due to other reasons - so NO UI policy can be used - has to be via the client script.

When adding the fields on the form, it's working.

When removing them on the form - the check will not work anymore. So far I even can understand it.

So what I thought is to then just run a querry on the table to get the check fields ... but this seem not to work ... any idea?

To reproduce:

Table sys_user has a field CHECK (True/False).

On view "VIEW" of table sys_user the field "Field" should only be displayed if sys_user.CHECK is true ...

What i wrong with this client script?

function onLoad() {

    var gr = new GlideRecord('sys_user');

    gr.addQuery('sys_id', sys_id);       // current not to be used on onLoad CS

    gr.query();

if(gr.next()) {

if (gr.CHECK != true )       {                                                           // if FIELD not TRUE

            g_form.setDisplay('FIELD', false);                                 // Hide FIELD

                  }

}

... as I said, if the field is added to the view, this works ... but I do not want it on the view

if (g_form.getValue('CHECK') != 'true' )       {                       // if not MDM User

g_form.setDisplay('FIELD', false);                                 // Hide guideline smartphone

}

1 ACCEPTED SOLUTION

Here is the sample script, Try this:



Client Script:



//Write this onLoad script on 'sys_user' table



function onLoad() {


 


var sys_id = g_form.getUniqueValue();


var ga = new GlideAjax('FieldDisplayUtil');


ga.addParam('sysparm_name','displayField');


ga.addParam('sysparm_sys_id',sys_id);


ga.getXMLWait();



if(ga.getAnswer() == 'true') {


g_form.setDisplay('FIELD', 'false');


}  


}



Script Include:



//Client Callable should be checked and script include name should be "FieldDisplayUtil"



var FieldDisplayUtil = Class.create();


FieldDisplayUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {



displayField: function(){



var answer = '';


var sys_id = this.getParameter('sysparm_sys_id');


var gr = new GlideRecord('sys_user');


gr.addQuery('sys_id', sys_id);    


gr.query();


if(gr.next()) {


if(gr.CHECK != 'true'){


answer = 'false';


return answer;


}


else{


answer = 'true';


return answer;


}


                  }


},



type: 'FieldDisplayUtil'


});


View solution in original post

17 REPLIES 17

Chuck Tomasi
Tera Patron

What is 'sys_id' in this field? Are you looking to get the record's sys_id that you are viewing?



If so, then put this before your query...



var sys_id = g_form.getUniqueValue();



http://wiki.servicenow.com/index.php?title=GlideForm_(g_form)#getUniqueValue


Hi Chuck,


seems not to work.


I'm on a user record with a specific view ... and need to check the sys_user table of that user I'm currently watching for a specific field value (but this one is not on the current form view ...


If the field isn't on the form then you cannot use a client script or UI policy on it.



You CAN make client scripts run on a specific view by unchecking the Global field and setting the view name in the View field. (eg. ess)


Arindam Ghosh
Mega Guru

You can't use GlideRecord in Client Scripting. Here you need to use Glide Ajax.



Also I was not able to understand what you are trying to do here. Can you please elaborate



Thanks,


Arindam