How to find element id of any varaible ?

Bhushan_Salseka
Mega Expert

Hello All,

I have created a variable with variable type 'Macro with Label'. So how can i find element id of this variable.

Regards

Bhushan

4 REPLIES 4

coryseering
ServiceNow Employee
ServiceNow Employee

Hi Bhushan,



Can you clarify what you are trying to accomplish? When you say you want to find the "element id", are you talking about some server-side script, client-side script, or you literally want to find the sys_id of the underlying database record that holds the variable definition?



If you can tell us where and how you want to use the information, and exactly what you mean by "element id" we can offer some suggestions.



Thanks


Cory


JJ1
Kilo Guru

Did you try getControl() method ?


GlideForm (g form) - ServiceNow Wiki


lexifouts
Tera Expert

I realize this is an old question, but figured I would update with a solution in case anyone else was curious in the future.

Today, the element ID of a variable is formed using this formula:

ni.VE<sys_id of variable response record (sc_item_option)>

From a client script, you can programmatically find the element ID of a variable using the following method:

var nameOfVariable = 'var_example';
var idOfVariable = g_form.resolveNameMap(nameOfVariable); //returns the sys_id of the variable response record from the sc_item_option table
var elementID = 'ni.VE' + idOfVariable;

You can then use elementID to perform the desired function, such as g_form.getElement(elementID), etc.

By the way: the g_form.resolveNameMap function compares the name of a variable against g_form's nameMap property, an array of objects where each object contains the following information about the catalog item's variables (and you can explore this in your browser's JavaScript console): prettyName (raw variable name, 'var_example' in this case), realName (sys_id of variable response record in the sc_item_option table), and label (display name/label of variable). 

One thing to be weary of is that ServiceNow may change their element ID naming convention in the future, and of course, this does not work on Service Portal/Mobile.

Hope this helps someone!

You.

Are.

Truly.

Awesome!

 

Note: no need for the var elementID part. The mapping shows the entire string with ni.VE

This does not diminish your awesomeness, though.