How to get only the displayed value in script?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-29-2023 10:37 PM - edited ‎11-29-2023 10:43 PM
Hi All,
We have 2 fields called u_html and u_message and these 2 fields are running on the u_email client table now when we click on the HTML button we will get only the u_html field but while checking in logs when doing this HTML button click I am getting both values in logs for u_html and message since we have stored both values in backend and just hiding from front end. Now the requirement is if the u_html filed is only visible in the form then i need to get the logs only for u_htm and u_message should be empty how can this be achieved tried the below 2 scripts but not working getting values for both field even if it not visible on the form
//issue getting both values at the same time
var DisplayValuehtml = current.getDisplayValue('u_html');//syntax1
//var DisplayValuehtml = current.getvalue('u_html');//syntax2
gs.log("ui1DisplayValuehtmlis"+DisplayValuehtml);
var DisplayValuemsg = current.getDisplayValue('u_message');//syntax1
//var DisplayValuemsg = current.getvalue('u_message');//syntax2
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-29-2023 10:54 PM
Hi @jobin1 ,
The issue occurs because the getDisplayValue() and getValue() methods retrieve values directly from the database, regardless of whether they are visible in the form or not.
To solve this, you need to incorporate the isVisible() method to check if the field is visible in the form. If the field is visible, then log its value, otherwise log it as empty or null.
Try This:
var DisplayValuehtml, DisplayValuemsg;
if(g_form.isVisible('u_html')) {
DisplayValuehtml = current.getDisplayValue('u_html');
} else {
DisplayValuehtml = "";
}
gs.log("ui1DisplayValuehtmlis " + DisplayValuehtml);
if(g_form.isVisible('u_message')) {
DisplayValuemsg = current.getDisplayValue('u_message');
} else {
DisplayValuemsg = "";
}
gs.log("ui1DisplayValuemsgis " + DisplayValuemsg);
This script first checks if the fields are visible on the form. If they are, it retrieves their values. If they are not, it assigns an empty string to the variable. This way, you should only see the value of the visible field in your logs.
Please note that this script is written in JavaScript, the scripting language used in ServiceNow. Also, it assumes that you have the necessary access rights to modify scripts in ServiceNow.
Let me know if you are looking for something else!
Thanks and Regards,
Rahul
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-29-2023 10:58 PM
Hi Rahul,
tried already the similar script but got below error
error in log
com.glide.script.RhinoEcmaError: "g_form" is not defined.
sys_ui_action.4c80c8d6371c6a0040ba70f543990ed3.script : Line(8) column(0)
5: function validatefield() {
6: //
7: // Check if the u_html field is visible on the form
==> 8: if (g_form.isVisible('u_html_message')) {
9: gs.log("htmltest1 visible");
10: var DisplayValuehtml = current.getDisplayValue('u_html_message');
11: gs.log("htmltest2 htmlvalue is " + DisplayValuehtml);
//script tried
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-29-2023 11:03 PM
@jobin1 ,
This method(isVisible) only works on the client side, so you need to use a client script or a UI action with "Client" checked to use it?
Thanks and Regards,
Rahul
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-29-2023 11:07 PM
I tried this script in ui action only not working..