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

Reason is, that I can not use ui policy - as this has also something to do with serviceportal functionality where ONLY Clients scripts will do the job ...



Glide Ajax ... could u please provide a script example ... thank you!


If you want to use server side tables in client script, you need to used GlideAjax, you can't use GlideRecord.


Here is the example of GlideAjax:



http://wiki.servicenow.com/index.php?title=GlideAjax#gsc.tab=0


Thanks.


Unfortunately this is to much for me ... struggling with JS already ... 😉



Any change you could provide a more specific example?! 😉


Thank you


Using Glide Ajax we write a client Script and from client script we send the client side values to server side to a script include. In script include we do allDB operations and then set back the result to client side and perform the rest of the operations in client side.



Here is an example for you:



Client Script


function onSubmit() {


var startdate = g_form.getValue('start_date');


var enddate = g_form.getValue('end_date');


if(enddate != '' && startdate != '') {


var ga = new GlideAjax('DateValidationUtil');


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


ga.addParam('sysparm_start_date', startdate);


ga.addParam('sysparm_end_date', enddate);


ga.getXMLWait();



if(ga.getAnswer() <0) {


g_form.setValue('end_date','');


alert("The End Date cannot be prior to the Start Date");


return false;


}


}


}



Script Include


//The name of the script include should be same as we mention in Client script. Here "DateValidationUtil"



var DateValidationUtil= Class.create();


DateValidationUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {



checkDate: function(){


var sdate = this.getParameter('sysparm_start_date');


var edate = this.getParameter('sysparm_end_date');


var answer = gs.dateDiff(sdate ,edate,true);


return answer;


},



type: 'DateValidationUtil'


});


Hi Arindam,



just checked. Somehow I do not really get it. I think your script checks is a value on the form matches some field on other table ... .g_form.getValue('start_date') tells me you have the "start_date" on the form ... .



I need to check a field value (true/false) on a table ... if this gives me back "true" I will display a (other) field on my current form ...



Could u please help out again.