g_form.getFieldNames() on Portal Only - Why?

G24
Kilo Sage

Can someone please explain to me WHY there is no straightforward way to get a list of the field names on a regular old Form?

 

Apparently the g_form.getFieldNames() function is ONLY available if you are in the context of the Service Portal.

 

I can't for the life of me understand why that would be; although I'm sure there is a reason.

 

What is the reason?  Thanks.

3 REPLIES 3

Gunjan Kiratkar
Kilo Patron
Kilo Patron

Hi @G24 

I think there is no OOB method as of now available for this. But you can write code as below in client script:-

 

var arr = [];

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

arr.push(g_form.elements[x].fieldName);

}

alert("array value = " + arr);


Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy

You didn't answer my actual question, @Gunjan Kiratkar , but that was still helpful, thank you.

I ended up writing the following UI Policy script to make fields ReadOnly or NOT.  Hopefully this is OK to do:

Picture7.png

It's strange to me that there is a g_form method for enumerating the EDITABLE fields, but not ALL the fields.  I just don't get it.  Thanks again. 

Pavol
Tera Contributor

Hello there is also option

g_form.getFieldNames();

It is answer from other stream - https://www.servicenow.com/community/developer-forum/is-there-some-easier-way-to-get-all-the-field-n...