Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Get display value of variable from hr case variable editor

kali
Tera Contributor

Hi All,

I am using a system property to keep all the variable backend name and passing to a function to get the value , now the problem is while passing from the system property i am passing string as well as reference value, how to get the display value of reference,i am using gliderecord object hrcase. Please help me on this, thanks in advance

hrCase.variables[fieldname];

6 REPLIES 6

pratikjagtap
Giga Guru
Giga Guru

Hi @kali ,

Here’s how you can handle both string and reference types dynamically:

var fieldName = gs.getProperty('your.property.name'); // This gives you the variable name
var variableValue = hrCase.variables[fieldName];

if (variableValue != null) {
var displayValue = variableValue.getDisplayValue ? variableValue.getDisplayValue() : variableValue;
gs.info("Display Value: " + displayValue);
} else {
gs.info("Variable not found or is null.");
}

Explanation:

  • variableValue.getDisplayValue checks if the method exists (i.e., it's a GlideElement like a reference field).
    If it's a reference, getDisplayValue() returns the human-readable name.
    If it's a simple string or non-reference, it just returns the value directly.

If my response helped, please hit the 👍Thumb Icon and accept the solution so that it benefits future readers.

 

Regards,
Pratik



Hi @pratikjagtap ,

I would like to check if the variable is choice or Yes/No, then it should return the label not the backend choice, can you please modify the above code and let me know

Ravi Gaurav
Giga Sage
Giga Sage

Hi @kali 
can you check the below code with your requirement:

var fieldName = gs.getProperty('your.property.name'); // System property holds variable backend name
var variableValue = hrCase.variables[fieldName];

if (variableValue != null) {
var displayValue;


if (variableValue.getDisplayValue) {
displayValue = variableValue.getDisplayValue();
}
else {

displayValue = variableValue;
}

// Special handling for Yes/No and Choice variables

if (typeof variableValue == 'object' && variableValue.getQuestion) {
var question = variableValue.getQuestion();
var label = question.getLabel(variableValue);
if (label)
displayValue = label;
}

gs.info("Display Value: " + displayValue);
}
else {
gs.info("Variable not found or is null.");
}

--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

 YouTube: https://www.youtube.com/@learnservicenowwithravi
 LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/

Ankur Bawiskar
Tera Patron
Tera Patron

@kali 

please share your complete script here

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader