Client script to hide fields

Koki
Tera Contributor

I have tried the following script with no success.

I want to hide the field in the attachment that is circled in red.

Is there something wrong in the way I'm writing it?

 

function onLoad() {

 if (g_user.hasRoleExactly('x_540069_cmdb.test_roll1')) {

  g_form.setVisible('kisyu_name', true);
  g_form.setVisible('gyousya_name', true);
 }
 else {
  g_form.setVisible('kisyu_name', false);
  g_form.setVisible('gyousya_name', false);
 }
}

1 ACCEPTED SOLUTION

Anil Lande
Kilo Patron

I see you have added fields from one your reference field by dot walking.

To manage dot walked fields use below logic:

 

function onLoad() {

 if (g_user.hasRoleExactly('x_540069_cmdb.test_roll1')) {

  g_form.setVisible('kisyu_name', true);
  g_form.setVisible('kisyu_name.maker_id.gyousya_name', true);
 }
 else {
  g_form.setVisible('kisyu_name', false);
  g_form.setVisible('kisyu_name.maker_id.gyousya_name', false);
 }

always use field name in client script and if it is dot walked field then use highlighted part:

find_real_file.png

 

 

Thanks,

Anil Lande

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

View solution in original post

7 REPLIES 7

Mark Roethof
Tera Patron
Tera Patron

Hi there,

What exactly is not working? What did you debug?

For example:
- Is the Client Script triggered at all?
- What is the outcome of g_user.hasRoleExactly, is it wat you expect?
-- Double check how you are testing, since it is g_user.hasRoleExactly!
- Is the IF or ELSE reached at all?

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020, 2021 ServiceNow Community MVP
2020, 2021 ServiceNow Developer MVP

---

LinkedIn
Community article, blog, video list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Anil Lande
Kilo Patron

I see you have added fields from one your reference field by dot walking.

To manage dot walked fields use below logic:

 

function onLoad() {

 if (g_user.hasRoleExactly('x_540069_cmdb.test_roll1')) {

  g_form.setVisible('kisyu_name', true);
  g_form.setVisible('kisyu_name.maker_id.gyousya_name', true);
 }
 else {
  g_form.setVisible('kisyu_name', false);
  g_form.setVisible('kisyu_name.maker_id.gyousya_name', false);
 }

always use field name in client script and if it is dot walked field then use highlighted part:

find_real_file.png

 

 

Thanks,

Anil Lande

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

Koki
Tera Contributor

Thank you for the explanation.
I tried to do as you told me.
However, it did not succeed.

 

As a test, I did the following.

 

function onLoad() {

 if (g_user.hasRoleExactly('x_540069_cmdb.test_roll1')) {

  g_form.setVisible('kisyu_name.maker_id.gyousya_name', true);
  g_form.setVisible('kisyu_name', true);
  g_form.setVisible('install_date', true);

 }

 else {
  g_form.setVisible('kisyu_name.maker_id.gyousya_name', false);
  g_form.setVisible('kisyu_name', false);
  g_form.setVisible('install_date', false);

 }
}

 

 

Then, when the user does not have a role, the 'install_date' is indeed not displayed.

(Only 'install_date' was successful.)


It seems that only the other two fields, which are the most important ones, are not working correctly.

Is there still something wrong with the script?

Okay, you can use debugger to see why these fields are still visible.

There is possibility that other UI Policies conflicting with this field.

Open record in your content window and watch field:

find_real_file.png 

It will open field watcher at bottom, reload the form and check logs in field watcher to see whoch script making it visible.

find_real_file.png

 

Note: this field watcher will not work if you open record in new tab/window. Open it in ServiceNow content window only. As shown in first screenshot.

 

Thanks,

Anil Lande

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande