
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2017 08:30 AM
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
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-08-2017 06:18 AM
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'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-08-2017 04:17 AM
Lets start from a step back,
To understand GlideAjax, first you need to know what is client side scripting and what is server side scripting. And when do we use those.
Client Script execution happens in user end, generally in browser. Example where we need client scripting is: Suppose we need to display an error based on a field value without refreshing the page.
Whereas server scripting execution happens in server side. Example where we need server side scripting is: based on some user input we update the value in database.
Now coming to the point: Generally in client scripts, we can't do any server side operation. But if we want to do any server side operation in a client script, we need to use GlideAjax Class.
Here in my scripts what I am doing is: using client scripts, we are calling script include where server side operation happen. Now you try the same in your local instance:
Using the mentioned script I am triggering an error message in client side whithout refreshing the page when we enter end date prior to start date.
I am sending both start date and end date to script include there we are calculating the difference and then sending that difference to client script and then based on the difference we are firing the error message.
Hope these would help.
Thanks,
Arindam

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-08-2017 04:57 AM
I got the general idea behind this - but THANK YOU anyhow!
Back to the issue.
For my understanding I need to send "g_user.getUserID()" to the script include and give back the values of fields I need to evaluate ... correct?
Still the coding is tricky for me here ...
I need to get back true or false for 2 values I'm checking on the sys_user table for the user available on the form ...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-08-2017 06:18 AM
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'
});

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-08-2017 07:53 AM
Got it! Works for 1 field ....and I also understood it more or less now 😉
Thank u so mutch!
Add on question .... if I need to check more fields in the sys_user record at the same time ... how to do that?
I tried by just adding a second function and calling the script include a 2nd time ... that works, just looks not really good.
Any way of doing it more nicely ... I tried like this, but this did not work ...
Client Script:
function onLoad() {
var sys_id = g_form.getUniqueValue();
var ga = new GlideAjax('FieldCheckUtil');
ga.addParam('sysparm_name','display1');
ga.addParam('sysparm_name','display2');
ga.addParam('sysparm_sys_id',sys_id);
ga.getXMLWait();
if(ga.getAnswer(answer_1) != 'true') { // assume this is the problem - but how to get the result depending on the field ... ?
g_form.setDisplay('u_1', false);
}
if(ga.getAnswer(answer_2) != 'true') {
g_form.setDisplay('u_2', false);
}
Script Include:
var FieldCheckUtil = Class.create();
FieldCheckUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {
display1: function(){
var answer_1 = '';
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()) {
answer_1 = gr.u_1;
return answer_1;
}
else {
answer_1 = false;
return answer_1;
}
},
display2: function(){
var answer_2 = '';
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()) {
answer_2 = gr.u_2;
return answer_2;
}
else {
answer_2 = false;
return answer_2;
}
},
type: 'FieldCheckUtil'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-08-2017 07:58 AM
could you mark the answer correct so that it does not appear in unanswered list.
Thanks,
Arindam