Retrieve all Form Variables and Fields on the Client Side

Matt Dumont1
Mega Expert

After some substantial digging, I discovered a way to retrieve a list of all field and variable names on the client side.   I figured that since g_form has the ability to retrieve values based on names passed as parameters (g_form.getValue), the names must be stored somewhere.   I'm aware that this can be accomplished by passing values from server to client-side, but this seems like something that should be doable completely from the client.   I resorted to the following:

//Retrieve a list of variable names

var arrVars = [];

var handler = g_form.prefixHandlers['variables'];

var nameMap = handler.handlerObject['nameMap'];

  for(var i=0; i < nameMap.length;i++) {

  arrVars.push('variables.' + nameMap[i].prettyName);

//Retrieve a list of field names

var arrFields = [];

for(var i=0; i<g_form.elements.length;i++) {

arrFields.push(g_form.elements[i]['fieldName']);

}

I'm aware that this is a messy approach.   I would much prefer an exposed g_form object that contained these values.   My question is two-fold.

1.   Is there a better way to accomplish this on the client side about which I am not aware?

2.   Does anyone see this causing us problems in the future (e.g. does g_form's internal structure get modified on a regular basis, etc?)

Thanks in advance for insights!

4 REPLIES 4

manikorada
ServiceNow Employee
ServiceNow Employee

Matthew,



Instead you can use a Display Business rule which will set a scratch pad variable with the information you need and you can use that in the Client Script.


While this is a good way to handle it, as I mentioned above, I am looking for a solution that does not involve passing the values from the server-side, as they already exist on the client-side.


matthias_pohlin
ServiceNow Employee
ServiceNow Employee

Hi Matthew,




g_form has a map: g_form.nameMap


This has two entries per variable: prettyName and realName.



Example usage:


for (var index = 0; index < g_form.nameMap.length; index++) {


    jslog("Name Map # " + index + ": " + g_form.nameMap[index].prettyName);


}



BR Matthias


That suggestion does bring back names for the variables, but does not appear to do so for fields.   I see that in both of our methods, it appears to also return the variable set titles as separate entries that would need to be filtered out.   Thanks for the input.