How to get only the displayed value in script?

jobin1
Tera Expert

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

6 REPLIES 6

Rahul Talreja
Mega Sage
Mega Sage

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!

Please mark my response correct/helpful as applicable!
Thanks and Regards,
Rahul

jobin1
Tera Expert

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

//trying validate which field is visible starts

validatefield();

function validatefield() {
//
// Check if the u_html field is visible on the form
if (g_form.isVisible('u_html_message')) {
    gs.log("htmltest1 visible");
    var DisplayValuehtml = current.getDisplayValue('u_html_message');
    gs.log("htmltest2 htmlvalue is " + DisplayValuehtml);
}

// Check if the u_message field is visible on the form
if (g_form.isVisible('u_message')) {
    gs.log("htmltest3 messagevisible");
    var DisplayValuemsg = current.getDisplayValue('u_message');
     gs.log("htmltest4 messagevalue is " + DisplayValuehtml);
}

//

}

//trying to validate which field is visible ends




@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?

Please mark my response correct/helpful as applicable!
Thanks and Regards,
Rahul

I tried this script in ui action only not working..