- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-18-2020 06:38 PM
Hi.
I created choice box fields like below.
Location (val_location)(Label)
-placeA(val_place_a)(CheckBox)
-placeB(val_place_b)(CheckBox)
-placeC(val_place_c)(CheckBox)
I want to get Label of check box which selected.
For Example, I tried script like below when place B checked.
if(g_form.getValue("val_place_b")){
g_form.addInfoMessage(g_form.getLabelOf("val_place_b"));
}
I expected that "placeB" message appear, but "Loctation" appeared.
How can I get label of check box's?
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2020 05:28 AM
Hi
I had some time to investigate, but I found some solution for you:
Look at the Client Script below:
There is an array variable "nameMap" on the "g_form" object, which you can use to find the elements of the form (1) - see line 7. I print out the content of this object in line 11 to see the content.
In line 15, I compare the "realName" element of each Array element to find the "id" of the Control, which was calling this Client Script (2).
If it was found, I print out the Label (see line 16).
As you asked in your question, you want to search for the variable name(4), which I do in line 22.
When found, again I pipe out the label.
The next screenshot shows the output results, when I clicked the first option:
Here is the code for Copy&Paste:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
if(g_form.getValue("val_place_b")){
var myFormObj = g_form.nameMap; // gets access to the Objects on the current form
var mySysID = control.id; // gets the Sys_ID of the Control for which the code runs
// Print out the "form's nameMap"
g_form.addInfoMessage(JSON.stringify(myFormObj, null, 4));
// Search for the Control of this script
var i = 0;
for (i = 0; i < myFormObj.length; i++) {
if (myFormObj[i].realName.toString() == mySysID.toString()) {
g_form.addInfoMessage('The Label of the clicked element is = ' + myFormObj[i].label);
}
}
// OR...
for (i = 0; i < myFormObj.length; i++) {
if (myFormObj[i].prettyName.toString() == 'val_place_b') {
g_form.addInfoMessage('The Label (2) of the clicked element is = ' + myFormObj[i].label);
}
}
}
}
Review this example in detail and let me know if that answers your question.
Please do not forget to mark my answer as correct and helpful to send me some donation on that 🙂
Enjoy & BR
Dirk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-18-2020 08:30 PM
Hi,
Since you have used Label variable and then those checkboxes it seems to give Location label.
I tried the same and it gave me similar
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2020 05:28 AM
Hi
I had some time to investigate, but I found some solution for you:
Look at the Client Script below:
There is an array variable "nameMap" on the "g_form" object, which you can use to find the elements of the form (1) - see line 7. I print out the content of this object in line 11 to see the content.
In line 15, I compare the "realName" element of each Array element to find the "id" of the Control, which was calling this Client Script (2).
If it was found, I print out the Label (see line 16).
As you asked in your question, you want to search for the variable name(4), which I do in line 22.
When found, again I pipe out the label.
The next screenshot shows the output results, when I clicked the first option:
Here is the code for Copy&Paste:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
if(g_form.getValue("val_place_b")){
var myFormObj = g_form.nameMap; // gets access to the Objects on the current form
var mySysID = control.id; // gets the Sys_ID of the Control for which the code runs
// Print out the "form's nameMap"
g_form.addInfoMessage(JSON.stringify(myFormObj, null, 4));
// Search for the Control of this script
var i = 0;
for (i = 0; i < myFormObj.length; i++) {
if (myFormObj[i].realName.toString() == mySysID.toString()) {
g_form.addInfoMessage('The Label of the clicked element is = ' + myFormObj[i].label);
}
}
// OR...
for (i = 0; i < myFormObj.length; i++) {
if (myFormObj[i].prettyName.toString() == 'val_place_b') {
g_form.addInfoMessage('The Label (2) of the clicked element is = ' + myFormObj[i].label);
}
}
}
}
Review this example in detail and let me know if that answers your question.
Please do not forget to mark my answer as correct and helpful to send me some donation on that 🙂
Enjoy & BR
Dirk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-27-2020 01:25 AM
Thanks a lot for investigation!
I didn't know g_form.nameMap. That code was very useful for my situation.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-27-2020 03:13 AM