GlideElement - Scoped

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

Scoped 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 GlideElement - canRead()

Indicates whether the user's role permits them to read the associated GlideRecord.

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

Scoped GlideElement - canWrite()

Determines whether the user's role permits them to write to the associated GlideRecord.

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 GlideElement - changes()

Determines if the current field has been modified, if the field is a reference, number, 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 fields have been changed, false if the field has not.
// This method is often used in business rules. The following example shows is from a business rule, 
// if "assigned_to" field value is changed, create a event in the EventQueue. 
if (!current.assigned_to.nil() && current.assigned_to.changes()) {
  gs.eventQueue('incident.assigned', current, current.assigned_to.getDisplayValue() , previous.assigned_to.getDisplayValue());
  }

Output:

Scoped GlideElement - changesFrom(Object o)

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
o 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, false if it does not.
/ The following example shows that in a business rule, if "active" field is changed from true, 
// insert a event in the EventQueue.
if (current.active.changesFrom(true)) {
  gs.eventQueue("incident.inactive", current, current.incident_state, previous.incident_state);
}

Scoped GlideElement - changesTo(Object o)

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

Note: The changesTo() 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 11. Parameters
Name Type Description
o Object An object value to check against the new value of the current field.
Table 12. Returns
Type Description
Boolean True if the previous value matches, false if it does not.
// The following example shows that in a business rule, if "active" field is changed to false, 
// insert a event in the EventQueue.
if (current.active.changesTo(false)) {
  gs.eventQueue("incident.inactive", current, current.incident_state, previous.incident_state);
}

Scoped 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 13. Parameters
Name Type Description
attributeName String Attribute name
Table 14. Returns
Type Description
String Attribute value

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"));
  }
 
}

Scoped GlideElement - getBooleanAttribute(String attributeName)

Returns the Boolean value of the specified attribute from the dictionary.

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

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

Scoped GlideElement - getChoices(String dependent)

Generates a choice list for a field.

Table 17. Parameters
Name Type Description
dependent String Optional: a dependent value
Table 18. Returns
Type Description
Array An array list of choices.
var glideRecord = new GlideRecord('incident'); 
glideRecord.query('priority','1'); 
glideRecord.next(); 
 
// urgency has choice list: 1 - High, 2 - Medium, 3 - Low, with value: 1, 2, 3
var choices = glideRecord.urgency.getChoices(); 
gs.info(choices);

Output:

Scoped GlideElement - getChoiceValue()

Returns the choice label for the current choice.

A choice has a value (number) and a label (string). This method returns the label.

Table 19. Parameters
Name Type Description
None
Table 20. Returns
Type Description
String The selected choice's label.

var glideRecord = new GlideRecord('incident'); 
glideRecord.query('priority','1'); 
glideRecord.next(); 
 
// urgency has choice list: 1 - High, 2 - Medium, 3 - Low, with value: 1, 2, 3
var choiceLabel = glideRecord.urgency.getChoiceValue(); 
gs.info(choiceLabel);

Output:

1 - High

Scoped GlideElement - getDecryptedValue()

Returns the clear text value for Password (2 way encrypted) fields in scoped applications.

Table 21. Parameters
Name Type Description
None
Table 22. Returns
Type Description
String The clear text password.
var tablename = 'x_scoped_app_table'
var CI = new GlideRecord(tablename);  
CI.addQuery('number', '0001002'); 
CI.query(); 
CI.next(); 

var password = CI.password_field
var decrypted = password.getDecryptedValue(); 
gs.info(decrypted);
Output:
x_scoped_app: cleartextpassword

Scoped GlideElement - getDisplayValue(Number maxCharacters)

Gets the formatted display value of the field.

Table 23. Parameters
Name Type Description
maxCharacters Number Optional: Maximum characters desired
Table 24. Returns
Type Description
String The display value of the field
var glideRecord = new GlideRecord('incident');
glideRecord.query('priority','1');
glideRecord.next();
gs.info(glideRecord.priority.getDisplayValue());

Output:

Scoped GlideElement - getED()

Returns the field's element descriptor.

Table 25. Parameters
Name Type Description
None
Table 26. Returns
Type Description
Scoped GlideElementDescriptor The field's element descriptor.
var grInc = new GlideRecord('incident');
grInc.query('priority', '1');
 
var field = grInc.getElement('priority');
var ed = field.getED();

Scoped GlideElement - getGlobalDisplayValue()

Returns the phone number in international format.

Table 27. Parameters
Name Type Description
None
Table 28. Returns
Type Description
String The phone number in international format.

Scoped GlideElement - getHTMLValue(Number maxChars)

Returns the HTML value of a field.

Table 29. Parameters
Name Type Description
maxChars Number Optional. Maximum number of characters to return.
Table 30. Returns
Type Description
String HTML value for the field.

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

Scoped GlideElement - getJournalEntry(Number mostRecent)

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

Table 31. Parameters
Name Type Description
mostRecent Number If 1, returns the most recent entry. If -1, returns all journal entries.
Table 32. 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".

//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]);

Scoped GlideElement - getLabel()

Returns the object label.

Table 33. Parameters
Name Type Description
None
Table 34. Returns
Type Description
String Object label

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");  
      }
    }
}

Scoped GlideElement - getName()

Returns the name of the field.

Table 35. Parameters
Name Type Description
None
Table 36. Returns
Type Description
String Field name


      
    

Scoped GlideElement - getReferenceTable()

Gets the table name for a reference element.

Table 37. Parameters
Name Type Description
None
Table 38. Returns
Type Description
String The table name of the reference
var grINC = new GlideRecord('incident');
grINC.query('number','INC0010041'); // record assignment group assigned to "CAB Approval"
if (grINC.next()) { 
  // Get the table name 
  var tableName = grINC.assignment_group.getReferenceTable();
  gs.info( tableName ); 
}

Scoped GlideElement - getRefRecord()

Gets a GlideRecord object for a given reference element.

Table 39. Parameters
Name Type Description
None
Table 40. Returns
Type Description
GlideRecord The GlideRecord object
var grINC = new GlideRecord('incident');
grINC.query('number','INC0010041');
if (grINC.next()) {
  // Get a GlideRecord object for the referenced sys_user_group record
  var grGroup =grINC.assignment_group.getRefRecord();
  gs.info( grGroup.getValue('name') );
}

Output:

Scoped GlideElement - getTableName()

Returns the name of the table on which the field resides.

Table 41. Parameters
Name Type Description
None
Table 42. Returns
Type Description
String Name of the table. The returned value may be different from the table Class that the record is in. See Tables and Classes in the product documentation.
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()+")");
}
 
}

Scoped GlideElement - nil()

Determines if a field is null.

Table 43. Parameters
Name Type Description
None
Table 44. Returns
Type Description
Boolean True if the field is null or an empty string, false if not.
var glideRecord = new GlideRecord('incident'); 
glideRecord.query('priority','1'); 
glideRecord.next(); 
gs.info(glideRecord.state.nil());

Output:

Scoped GlideElement - setDateNumericValue(Number milliseconds)

Sets the value of a date/time element to the specified number of milliseconds since January 1, 1970 00:00:00 GMT.

When called, setDateNumericValue() automatically creates the necessary GlideDateTime/GlideDate/GlideDuration object, and then sets the element to the specified value.

Note: Before calling this method, the element must already exist (by querying an existing record or by initializing (gr.initialize()) a new record).
Table 45. Parameters
Name Type Description
milliseconds Number Number of milliseconds since 1/1/1970
Table 46. Returns
Type Description
void

var gr = new GlideRecord("incident");
gr.initialize();
gr.opened_at.setDateNumericValue(10000);

Scoped GlideElement - setDisplayValue(Object value)

Sets the display value of the field.

Table 47. Parameters
Name Type Description
value Object The value to set for the field.
Table 48. Returns
Type Description
void
var glideRecord = new GlideRecord('incident'); 
glideRecord.query('priority','1'); 
glideRecord.next();
 
//change the urgency to 3 
glideRecord.urgency.setDisplayValue('3 - Low');
gs.info(glideRecord.urgency);

Output:

Scoped GlideElement - setError(String errorMessage)

Adds an error message. Available in Fuji patch 3.

Table 49. Parameters
Name Type Description
errorMessage String The error message.
Table 50. Returns
Type Description
void
var glideRecord = new GlideRecord('incident');
glideRecord.query('priority','1');
glideRecord.next();
 
glideRecord.short_description.setError('Error text');

Scoped GlideElement - setPhoneNumber(Object phoneNumber, Boolean strict)

Sets the field to the specified phone number.

This method is only available on a phone number GlideElement.

Table 51. Parameters
Name Type Description
phoneNumber Object The phone number to set. This can be in either the international or local format.
strict Boolean When true, specifies that the number specified must match the correct format. When false, the system attempts to correct an improperly formatted phone number.
Table 52. Returns
Type Description
Boolean True if the value was set.

Scoped GlideElement - setValue(Object value)

Sets the value of a field.

Table 53. Parameters
Name Type Description
value Object Object value to set the field to.
Table 54. Returns
Type Description
void
var glideRecord = new GlideRecord('incident');
glideRecord.query('priority','1');
glideRecord.next();
glideRecord.short_description.setValue('Network failure');
gs.info(glideRecord.short_description);

Output:

Scoped GlideElement - toString()

Converts the value to a string.

Table 55. Parameters
Name Type Description
value Object Object value to set the field to.
Table 56. Returns
Type Description
String The value as a string
var glideRecord = new GlideRecord('incident');
glideRecord.query('priority','1');
glideRecord.next();
gs.info(glideRecord.opened_at.toString());

Output: