Is there some easier way to get all the field names of a form.

jieLian123
Tera Expert

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.

6 REPLIES 6

DYCM
Mega Sage

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()));

       }

}

 

Aniket Chavan
Tera Sage
Tera Sage

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