Is there some easier way to get all the field names of a form.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-28-2024 11:16 PM - edited 01-28-2024 11:18 PM
Is there some easier way to get all the field names of a form. I suppose to get all the field names
and do something common in a for loop.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-28-2024 11:49 PM
Hi @jieLian123
Please check the following code snippet
var incident = new GlideRecord("incident");
incident.addActiveQuery();
incident.setLimit(1);
incident.query();
incident.next();
var elements = incident.getElements();
if (typeof elements.size != 'undefined') {
for (var i=0; i<elements.size(); i++) {
var element = elements.get(i);
gs.info("Element label is {0}, name is {1}, value is {2}", element.getLabel(), element.getName(), incident.getValue(element.getName()));
}
} else {
for (var i=0; i<elements.length; i++) {
var element = elements[i];
gs.info("Element label is {0}, name is {1}, value is {2}", element.getLabel(), element.getName(), incident.getValue(element.getName()));
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-29-2024 12:09 AM
Hello @jieLian123 ,
To retrieve all field names on the client side, you can explore the following syntax:
var allFields = g_form.getFieldNames();
Additionally, I've discovered an alternative method that may also provide the field names. Please test the code below. I acknowledge that using GlideRecord on the client side is generally not recommended. However, for the initial evaluation, you can check if it meets your requirements. If it aligns with your needs, we can proceed to implement a script include and client script to avoid direct GlideRecord usage. Let me know your feedback on the code, and we can tailor the solution accordingly.
var dict = new GlideRecord("sys_dictionary");
dict.addQuery("name", g_form.getTableName());
dict.addQuery("internal_type", "!=", "collection");
dict.query();
while (dict.next()) {
var elem = dict.getValue("element");
if (g_form.getElement(elem)) {
// show only fields that exist on this form...
g_form.addInfoMessage(elem);
}
}
Feel free to refer to the fetch all fields in a g_form for additional context on the source of the provided script.
Let me know your views on this and Mark ✅Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.
Thanks,
Aniket