GlideElement - Global

The GlideElement API provides a number of convenient script methods for dealing with fields and their values. GlideElement methods are available for the fields of the current GlideRecord.

GlideElement - canCreate()

Determines if the user's role permits the creation of new records in this field.

Table 1. Parameters
Name Type Description
None
Table 2. Returns
Type Description
Boolean True if the field can be created, false otherwise.

Scoped equivalent

To use the canCreate() method in a scoped application, use the corresponding scoped method:Scoped GlideElement - canCreate().


      
    

GlideElement - canRead()

Determines if the user's role enables them to read from the GlideRecord table.

Table 3. Parameters
Name Type Description
None
Table 4. Returns
Type Description
Boolean True if the field can be read, false otherwise.

Scoped equivalent

To use the canRead() method in a scoped application, use the corresponding scoped method:Scoped GlideElement - canRead().


      
    

GlideElement - canWrite()

Determines if the user's role enables them to write to the field.

Table 5. Parameters
Name Type Description
None
Table 6. Returns
Type Description
Boolean True if the user can write to the field, false otherwise.

Scoped equivalent

To use the canWrite() method in a scoped application, use the corresponding scoped method: Scoped GlideElement - canWrite().


      
    

GlideElement - changes()

Determines if the current field has been modified, if the field is a reference, integer, or string field.

This method does not detect changes to Journal fields.
Note: The changes() method is not supported within ACL scripts.
Note: If the GlideRecord on which you are performing this method has only been initialized and read, and has not been written, the underlying before-and-after values are the same. In this case, the method returns "false", as there has been no change to the data store.
Table 7. Parameters
Name Type Description
None
Table 8. Returns
Type Description
Boolean True if the field has changed, false otherwise.

Scoped equivalent

To use the changes() method in a scoped application, use the corresponding scoped method: Scoped GlideElement - changes().


      
    

GlideElement - changesFrom(Object value)

Determines if the previous value of the current field matches the specified object.

Note: If the GlideRecord on which you are performing this method has only been initialized and read, and has not been written, the underlying before-and-after values are the same. In this case, the method returns "false", as there has been no change to the data store.
Table 9. Parameters
Name Type Description
value Object An object value to check against the previous value of the current field.
Table 10. Returns
Type Description
Boolean True if the previous value matches the parameter, false if it does not.

Scoped equivalent

To use the changesFrom() method in a scoped application, use the corresponding scoped method:Scoped GlideElement - changesFrom(Object o).

if (theState.changesTo(resolvedState)) {
  operation = 4; //Resolved
}
else if (theState.changesTo(closedState)) {
  operation = 11; //Resolution Accepted
}
else if (theState.changesFrom(resolvedState) || theState.changesFrom(closedState)) {
  operation = 10; //Re-open
}
else {
  operation = 6; //Update
}

GlideElement - changesTo(Object value)

Determines if the new value of a field, after a change, matches the specified object.

Note: If the GlideRecord on which you are performing this method has only been initialized and read, and has not been written, the underlying before-and-after values are the same. In this case, the method returns "false", as there has been no change to the data store.
Table 11. Parameters
Name Type Description
value Object An object value to check against the new value of the current field.
Table 12. Returns
Type Description
Boolean True if the new value matches the parameter, false if it does not.

Scoped equivalent

To use the changesTo() method in a scoped application, use the corresponding scoped method: Scoped GlideElement - changesTo(Object o).

if (theState.changesTo(resolvedState)) {
  operation = 4; //Resolved
}
else if (theState.changesTo(closedState)) {
  operation = 11; //Resolution Accepted
}
else if (theState.changesFrom(resolvedState) || theState.changesFrom(closedState)) {
  operation = 10; //Re-open
}
else {
  operation = 6; //Update
}

GlideElement - debug(Object o)

Debugs the object and adds debug messages using setError(String).

Table 13. Parameters
Name Type Description
o Object An object to debug.
Table 14. Returns
Type Description
void


      
    

GlideElement - getAttribute(String attributeName)

Returns the value of the specified attribute from the dictionary.

If the attribute is a boolean attribute, use getBooleanAttribute(String) to get the value as a boolean rather than as a string.

Table 15. Parameters
Name Type Description
attributeName String Attribute name
Table 16. Returns
Type Description
String Specified attribute's value

Scoped equivalent

To use the getAttribute() method in a scoped application, use the corresponding scoped method: Scoped GlideElement - getAttribute(String attributeName).

doit();
function doit() {
  var gr = new GlideRecord('sys_user');
  gr.query("user_name","admin");
  if (gr.next()) {
    gs.print("we got one");
    gs.print(gr.location.getAttribute("tree_picker"));
  }
 
}

GlideElement - getBaseTableName()

Gets the base table of the field.

Table 17. Parameters
Name Type Description
None
Table 18. Returns
Type Description
String Name of the base table. This may be different from the table that the field is defined on. See "Tables and Classes" in the product documentation.

GlideElement - getBooleanAttribute(String attributeName)

Returns the value of the attribute on the specified field from the dictionary as a Boolean.

To get the value as a string, use getAttribute(string).

Table 19. Parameters
Name Type Description
attributeName String Name of the attribute
Table 20. Returns
Type Description
Boolean Attribute value. Returns false if the attribute does not exist.

Scoped equivalent

To use the getBooleanAttribute() method in a scoped application, use the corresponding scoped method: Scoped GlideElement - getBooleanAttribute(String attributeName).

GlideElement - getChoices(String value)

Generates a choice list for a field. Returns the choice values from the base table only, not from the extended table.

Table 21. Parameters
Name Type Description
value String An optional dependent value.
Table 22. Returns
Type Description
array list The choice values for the field.

Scoped equivalent

To use the getChoices() method in a scoped application, use the corresponding scoped method: Scoped GlideElement - getChoices(String dependent).

var field = gr.getElement('os');
var choices = field.getChoices();

GlideElement - getChoiceValue()

Gets the choice label for the current choice value.

Table 23. Parameters
Name Type Description
None
Table 24. Returns
Type Description
String The choice label.

Scoped equivalent

To use the getChoiceValue() method in a scoped application, use the corresponding scoped method: Scoped GlideElement - getChoiceValue().


      
    

GlideElement - getDebugCount()

Gets the number of debug messages logged by debug().

Table 25. Parameters
Name Type Description
None
Table 26. Returns
Type Description
Number The number of debug messages.


      
    

GlideElement - getDependent()

Checks whether or not the field is dependent on another field.

Table 27. Parameters
Name Type Description
None
Table 28. Returns
Type Description
String Name of the field on which the current field depends.


      
    

GlideElement - getDependentTable()

Gets the table that the current table depends on.

Table 29. Parameters
Name Type Description
None
Table 30. Returns
Type Description
String The name of the table.


      
    

GlideElement - getDisplayValue(Number maxChar)

Gets the formatted display value of the field.

Table 31. Parameters
Name Type Description
maxChar Number Optional, maximum number of characters to return.
Table 32. Returns
Type Description
String Display value of the field.

Scoped equivalent

To use the getDisplayValue() method in a scoped application, use the corresponding scoped method: Scoped GlideElement - getDisplayValue(Number maxCharacters).

var fields = current.getFields();
for (var i = 0; i < fields.size(); i++) { 
  var field = fields.get(i);
  var name = field.getName(); 
  var value = field.getDisplayValue(); 
  gs.print(i + ". " + name + "=" + value); 
}

GlideElement - getDisplayValueExt(Number maxChar, String nullSub)

Gets the formatted display value of a field, or a specified substitute value if the display value is null or empty.

Table 33. Parameters
Name Type Description
maxChar Number Optional, the maximum number of characters to be returned.
nullSub String The value to return if the display value is null or empty.
Table 34. Returns
Type Description
String The formatted display value of the field, or the specified substitute value.


      
    

GlideElement - getED()

Gets an element descriptor.

Table 35. Parameters
Name Type Description
None
Table 36. Returns
Type Description
ElementDescriptor The field's element descriptor.

Scoped equivalent

To use the getED() method in a scoped application, use the corresponding scoped method: Scoped GlideElement - getED().

var fields = current.getFields();
for (i=0; i<fields.size(); i++) { 
  var field = fields.get(i);
  var descriptor = field.getED(); 
  gs.print("type=" + descriptor.getType() + 
    " internalType=" + descriptor.getInternalType()); 
}

GlideElement - getElementValue(String value)

Gets the value for a given element.

Table 37. Parameters
Name Type Description
value String An element
Table 38. Returns
Type Description
String The value of the element.


      
    

GlideElement - getError()

Gets error debug messages.

Table 39. Parameters
Name Type Description
None
Table 40. Returns
Type Description
String A string of debug messages


      
    

GlideElement - getEscapedValue()

Gets the escaped value for the current element.

Table 41. Parameters
Name Type Description
None
Table 42. Returns
Type Description
String The escaped value of the current element.


      
    

GlideElement - getFieldStyle()

Gets the CSS style for the field.

Table 43. Parameters
Name Type Description
None
Table 44. Returns
Type Description
String The CSS style for the field.

var fields = current.getFields();
for (var i = 0; i < fields.size(); i++) { 
  var field = fields.get(i);
  var name = field.getName(); 
  var value = field.getDisplayValue(); 
  gs.print(i + ". " + name + "=" + value); 
}

GlideElement - getGlideObject()

Gets a glide object.

Table 45. Parameters
Name Type Description
None
Table 46. Returns
Type Description
Object A Glide object.

function calcDateDelta(start, end, calendar) {
  var cal = GlideCalendar.getCalendar(calendar);
  if (!cal.isValid())
      return null;
  var realStart = start.getGlideObject();
  var realEnd = end.getGlideObject();  
  var duration = cal.subtract(realStart, realEnd);
  return duration;
}

GlideElement - getGlideRecord()

Gets a glide record.

Table 47. Parameters
Name Type Description
None
Table 48. Returns
Type Description
GlideRecord A glide record

function task_ci_allocate() {
   var cnt = g_list.getRowCount();
   if (cnt == 0)
      return;
 
   var pct = 100.0 / cnt;
   var pct = (parseInt((pct + .005) * 100)) / 100;
   var gr = g_list.getGlideRecord();
   gr.query();
   while (gr.next()) {
      gr.u_allocation = pct;
      gr.update();
   }
}

GlideElement - getHTMLValue(Number maxChars)

Returns the HTML value of a field.

Table 49. Parameters
Name Type Description
maxChars Number Optional. Maximum number of characters to return.
Table 50. Returns
Type Description
String HTLM value for the field.

Scoped equivalent

To use the getHTMLValue() method in a scoped application, use the corresponding scoped method: Scoped GlideElement - getHTMLValue(Number maxChars).

var inccause = new GlideRecord("incident");
inccause.short_description = current.short_description;
inccause.comments = current.comments.getHTMLValue();
inccause.insert();

GlideElement - getHTMLValueExt(Number maxChar, String nullSub)

Returns the HTML value of a field, or a specified substitute value if the HTML value is null or empty.

Table 51. Parameters
Name Type Description
maxChar Number Maximum number of characters to return.
nullSub String Value to return if the HTML value is null or empty.
Table 52. Returns
Type Description
String HTML value or the specified substitute value.


      
    

GlideElement - getJournalEntry(Number mostRecent)

Returns either the most recent journal entry or all journal entries.

Table 53. Parameters
Name Type Description
mostRecent Number If 1, returns the most recent entry. If -1, returns all journal entries.
Table 54. Returns
Type Description
String

For the most recent entry, returns a string that contains the field label, timestamp, and user display name of the journal entry.

For all journal entries, returns the same information for all journal entries ever entered as a single string with each entry delimited by "\n\n".

Scoped equivalent

To use the getJournalEntry() method in a scoped application, use the corresponding scoped method: Scoped GlideElement - getJournalEntry(Number mostRecent).

//gets all journal entries as a string where each entry is delimited by '\n\n'
var notes = current.work_notes.getJournalEntry(-1); 
//stores each entry into an array of strings
var na = notes.split("\n\n");  
                      
for (var i = 0; i < na.length; i++)                 
  gs.print(na[i]);

GlideElement - getLabel()

Returns the object's label.

Table 55. Parameters
Name Type Description
None
Table 56. Returns
Type Description
String Label

Scoped equivalent

To use the getLabel() method in a scoped application, use the corresponding scoped method: Scoped GlideElement - getLabel().

var gr = new GlideRecord("sc_req_item");
gr.addQuery("request", current.sysapproval);
gr.query();
while(gr.next()) {
    var nicePrice = gr.price.toString();
    if (nicePrice != ) {
        nicePrice = parseFloat(nicePrice);
        nicePrice = nicePrice.toFixed(2);
    }
    template.print(gr.number + ":  " + gr.quantity + " X " + gr.cat_item.getDisplayValue() + " at $" + nicePrice + " each \n");
    template.print("    Options:\n");
    for (key in gr.variables) {
      var v = gr.variables[key];
      if(v.getGlideObject().getQuestion().getLabel() != ) {
         template.space(4);
         template.print('     ' +  v.getGlideObject().getQuestion().getLabel() + " = " + v.getDisplayValue() + "\n");  
      }
    }
}

GlideElement - getName()

Returns the name of the field.

Table 57. Parameters
Name Type Description
None
Table 58. Returns
Type Description
String Field name

Scoped equivalent

To use the getName() method in a scoped application, use the corresponding scoped method: Scoped GlideElement - getName().


      
    

GlideElement - getRefRecord()

Gets a GlideRecord object for a given reference element.

Table 59. Parameters
Name Type Description
None
Table 60. Returns
Type Description
GlideRecord A GlideRecord object

Scoped equivalent

To use the getRefRecord() method in a scoped application, use the corresponding scoped method: Scoped GlideElement - getRefRecord().

var grINC = new GlideRecord('incident');
grINC.notNullQuery('caller_id');
grINC.query();
if (grINC.next()) {
 
  // Get a GlideRecord object for the referenced sys_user record
  var grUSER = grINC.caller_id.getRefRecord();
  gs.print( grUSER.getValue('name') );
 
}

GlideElement - getStyle()

Get the CSS style for the value.

Table 61. Parameters
Name Type Description
None
Table 62. Returns
Type Description
String The CSS style for the value.

// Get string of style field from Field Style record
var cssStyle = gr.state.getStyle();

GlideElement - getTableName()

Returns the name of the field's table.

Table 63. Parameters
Name Type Description
None
Table 64. Returns
Type Description
String Name of the table. This may be different from the table Class that the record is in. See Tables and Classes in the product documentation.

Scoped equivalent

To use the getTableName() method in a scoped application, use the corresponding scoped method: Scoped GlideElement - getTableName().

if (current.approver.getTableName() == "sysapproval_approver") {
  if (current.approver == email.from_sys_id)  {
     current.comments = "reply from: " + email.from + "\n\n" + email.body_text;
 
   // if it's been cancelled, it's cancelled.
  var doit = true;
  if (current.state=='cancelled')
      doit = false;
 
  if (email.body.state != undefined)
     current.state= email.body.state;
 
   if (doit)
      current.update();
} else {
   gs.log("Approval for task ("+current.sysapproval.getDisplayValue()+") rejected because user sending 
           email( "+email.from+") does not match the approver ("+current.approver.getDisplayValue()+")");
}
 
}

GlideElement - getTextAreaDisplayValue()

Gets the value and escapes the HTML.

Table 65. Parameters
Name Type Description
None
Table 66. Returns
Type Description
String The escaped HTML


      
    

GlideElement - getXHTMLValue()

Gets the XHTML value of a field.

Table 67. Parameters
Name Type Description
None
Table 68. Returns
Type Description
String The XHTML value


      
    

GlideElement - getXMLValue()

Gets the XML value of a field as a string.

Table 69. Parameters
Name Type Description
None
Table 70. Returns
Type Description
String The XML value


      
    

GlideElement - hasAttribute(String attributeName)

Determines whether a field has a particular attribute.

Table 71. Parameters
Name Type Description
attributeName String The attribute to check for
Table 72. Returns
Type Description
Boolean True if the field has the attribute, false otherwise.

var totalCritical = 0;
 
var filledCritical = 0; var fields = current.getFields(); gs.print(fields); for (var num = 0; num &lt; fields.size(); num++) { 
 
    gs.print("RUNNING ARRAY VALUE " + num);
   var ed = fields.get(num).getED();
   if(ed.hasAttribute("tiaa_critical")) {
       gs.print("CRITICAL FIELD FOUND");
       totalCritical ++;
       if (!fields.get(num).isNil()) {
           filledCritical ++;
       }
   }
 
} var answer = 0; gs.print("TOTAL - " + totalCritical); gs.print("FILLED - " + filledCritical); if (filledCritical &gt; 0 &amp;&amp; totalCritical &gt; 0){ 
 
    var pcnt = (filledCritical/totalCritical)*100;
   answer = pcnt.toFixed(2);;    
 
} answer;

GlideElement - hasRightsTo(String operationName)

Determines if the user has the right to perform a particular operation.

Table 73. Parameters
Name Type Description
operationName String Name of the operation to check for
Table 74. Returns
Type Description
Boolean True if the user has permission to perform the operation, false otherwise.


      
    

GlideElement - hasValue()

Determines if the field has a value.

Table 75. Parameters
Name Type Description
None
Table 76. Returns
Type Description
Boolean True if the field has a value, false otherwise.


      
    

GlideElement - nil()

Determines whether the field is null.

Table 77. Parameters
Name Type Description
None
Table 78. Returns
Type Description
Boolean True if the field is null or an empty string, false otherwise.

Scoped equivalent

To use the nil() method in a scoped application, use the corresponding scoped method: Scoped GlideElement - nil().

if (current.start_date.changes() || current.end_date.changes() || current.assigned_to.changes()) { 
  if (!current.start_date.nil() && !current.end_date.nil() && !current.assigned_to.nil()) {
 gs.eventQueue("change.calendar.notify", current, current.assigned_to, previous.assigned_to);
 
}

GlideElement - setDisplayValue(Object displayValue)

Sets the display value of the field.

Table 79. Parameters
Name Type Description
displayValue Object Value to be displayed.
Table 80. Returns
Type Description
void

Scoped equivalent

To use the setDisplayValue() method in a scoped application, use the corresponding scoped method: Scoped GlideElement - setDisplayValue(Object value).

current.assignment_group.setDisplayValue('Network');

GlideElement - setError(String message)

Adds an error message.

Can be retrieved using getError().

Table 81. Parameters
Name Type Description
None
Table 82. Returns
Type Description
void

Scoped equivalent

To use the setError() method in a scoped application, use the corresponding scoped method: Scoped GlideElement - setError(String errorMessage).

if ((!current.u_date1.nil()) && (!current.u_date2.nil())) {
  var start = current.u_date1.getGlideObject().getNumericValue();
  var end = current.u_date2.getGlideObject().getNumericValue();
  if (start > end) {
    gs.addInfoMessage('start must be before end');
    current.u_date1.setError('start must be before end');
    current.setAbortAction(true);
  }
}

GlideElement - setInitialValue(String value)

Sets the initial value of a field.

Table 83. Parameters
Name Type Description
value String Initial value for the field.
Table 84. Returns
Type Description
void


      
    

GlideElement - setJournalEntry(Object value, String userName)

Sets the journal entry.

Table 85. Parameters
Name Type Description
value Object The value to set the journal entry to.
userName String The user to attribute the journal entry to. Does not set the journal entry's created by field.
Table 86. Returns
Type Description
void


      
    

GlideElement - setValue(Object value)

Sets the value of a field.

Table 87. Parameters
Name Type Description
value Object The value the field is to be set to.
Table 88. Returns
Type Description
void

Scoped equivalent

To use the setValue() method in a scoped application, use the corresponding scoped method: Scoped GlideElement - setValue(Object value).

// Using GlideElement.setValue (equivalent to GlideRecord.setValue)
gr.short_description.setValue('This is the short description.');
 
// Using GlideRecord.setValue
gr.setValue('short_description', 'This is the short description.');

GlideElement - toString()

Converts the field's value to a string.

Table 89. Parameters
Name Type Description
None
Table 90. Returns
Type Description
String The field's value as a string.

Scoped equivalent

To use the toString() method in a scoped application, use the corresponding scoped method: Scoped GlideElement - toString().

doit();
 
function doit() { 
 
  var gr = new GlideRecord('sys_user');
  gr.query();
  while (gr.next()) {
    if ((gr.first_name.toString().length != gr.first_name.toString().trim().length) || (gr.last_name.toString().length 
         != gr.last_name.toString().trim().length)) {
      gr.first_name = gr.first_name.toString().trim();
      gr.last_name = gr.last_name.toString().trim();
      gr.autoSysFields(false);
      gr.update();
    }
  }
 
}