Chris Pearson
Tera Contributor

What the heck is bracket notation???

Disclaimer: The ideas, thoughts, and opinions in this post reflect my own views and do not necessarily represent the views of my employer, Accenture. 

Bracket notation can be used to get values from attributes of a Glide Record object when you don't know which attributes (fields) you need to retrieve at the time you're writing your code.

Wait...what?

Let's explain it with an example.

Requirement: Create a scripted REST endpoint which returns a JSON object of ongoing Outages. The endpoint should be easily configurable by ServiceNow administrators so that the data being returned (the fields from the Outage) is defined without modifying code. (Hint: Use a system property)

Note: I'm not going to go over how to create a scripted endpoint. Maybe that will be a different blog post in the future. What comes below is only detailing how to retrieve data from a system property and use it along with bracket notation to build an object dynamically.

First, let's create a system property to hold our list of fields:

  • Name: outage_endpoint.fields_to_return
  • Description: A comma separated list of field names from the Outage [cmdb_ci_outage] table which is used to build our object in the scripted REST endpoint named outage_utils
  • Type: string
  • Value: short_description,begin,cmdb_ci

Next, in our scripted endpoint, we can now retrieve this data from the system property and use it to build our outage objects.

Look for the line of code inside our FOR loop: outageObject[fieldName] = outageGR[fieldName].getDisplayValue()

Notice we're not adding a period between the object and the attribute like we normally would? That's really the only tricky part to remember when doing this. It's NOT outageGR.[fieldName].getDisplayValue()

// Establish our return object
var retObj = {};

// Establish an array which will hold 1 or many objects of outages
var outageArray = [];

// Get the value of our system property
var fieldList = gs.getProperty('outage_endpoint.fields_to_return');

// Turn our fieldList into an array, splitting it on the comma
var fieldArray = fieldList.split(',');

// Look up our ongoing outages
// We define an 'ongoing' outage as any record on the Outage table which
// has a value in the 'begin' field but does not have a value in the 'end' field
var outageGR = new GlideRecord('cmdb_ci_outage');
outageGR.addEncodedQuery('beginISNOTEMPTY^endISEMPTY');
outageGR.query();
while(outageGR.next()){
	// For each active outage record we found, build an object using just the
	// fields we 'care' about from our system property
	var outageObject = {};
	for (var i = 0; i < fieldArray.length; i++) {
		var fieldName = fieldArray[i];
		// Using bracket notation, set an the object attribute and display value of 
		// the field from our system property
		outageObject[fieldName] = outageGR[fieldName].getDisplayValue();
	}
	// Push our outage object into our array 
	// (because there might be more than one active outage happening)
	outageArray.push(outageObject);
}

// If we found at least one active outage, return the array of objects
if(outageArray.length > 0){
	retObj.outage_array = outageArray;
} else{
	// We didn't find any active outages, so return a simple message
	retObj.message = 'No active outages were found';
}

// Set the body of our REST response to our return object
response.setBody(retObj);

If you set this up in your personal development instance, it's fully functional. Here's what the output looks like (after kicking this endpoint off in the REST API Explorer):

Response Body:

{
  "result": {
    "outage_array": [
      {
        "short_description": "SAP Human Resources Outage",
        "begin": "2017-08-18 15:11:32",
        "cmdb_ci": "SAP Human Resources"
      },
      {
        "short_description": "EXCH-SD-05 Outage",
        "begin": "2017-08-16 15:05:59",
        "cmdb_ci": "EXCH-SD-05"
      },
      {
        "short_description": "SAP Materials Management Outage",
        "begin": "2017-09-15 14:49:01",
        "cmdb_ci": "SAP Materials Management"
      }
    ]
  }
}