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

will do as soon I have solution for "fields" not "field" ... which was the intitial question ...


Sorry I haven't seen your question proprely. For multiple fields if you want to check then you need to add multiple parameter.



The way you tried is incorrect. Only one answer can be returned at a time not multiple.



In that case you need to write the script differently: You need to return a string with all the value together and then aplit those in client side and do the rest of the operation.



Here you can find that way:


using GlideAjax to retrieve multiple values off a user record


Kumar96
Kilo Contributor

The field needs to be on the form for the client script to work. You can hide it on form load with a UI Policy.



you need to use   g_form.setVisible('field', True);