The getFields() method returns a GlideElementCollection that must be iterated with hasNext() and next() — it's not a JavaScript array despite looking like one. This catches developers who try forEach() or array methods. The collection only populates after the GlideRecord executes a query, so you need at least one record to see the full field structure — empty tables return empty field collections even though the table definition exists.
When to use this
- Building dynamic integration mappings that need to discover available fields at runtime
- Creating debugging utilities that inspect record structure and field values
- Writing migration scripts that need to copy data between tables with different schemas
- Auditing customizations by comparing field lists between instances
When NOT to use this
- Don't use this in client scripts — the
GlideElementCollectionAPI doesn't exist client-side. Useg_form.getTableName()andGlideAjaxinstead - Don't iterate fields inside a loop over records — you'll get the same field list repeatedly. Cache the field structure outside the record loop
- Don't use this for table schema analysis — use the
sys_dictionarytable directly for complete metadata including inactive fields - Don't use this in performance-critical paths — field iteration is expensive. Pre-calculate and cache field lists when possible
Key behaviors and gotchas
- Extended field names contain dots (e.g.,
caller_id.name) and may not be writable depending on the reference field's access controls - The collection only includes fields visible to the current user's ACLs — two users may see different field lists for the same table
- Field order in the collection doesn't match form section order — it reflects the underlying table schema order
- Empty tables return empty field collections even though the table definition exists — you need at least one record
- The
getED().getInternalType()method returns the actual field type (e.g.,reference,choice) not the JavaScript type - Domain separation affects field visibility — fields may be hidden based on the record's domain and your domain access
The GlideElementCollection iterator is stateful and can only be traversed once. If you need to process the fields multiple times, collect them into an array on the first pass — calling getFields() again returns a fresh iterator but requires another query execution.
Enjoying this? Get one deep-dive per week.
Join 1,000+ ServiceNow pros — scripts, GlideRecord patterns, Flow Designer techniques, and career moves. Free.