Thank you for your feedback.
Form temporarily unavailable. Please try again or contact docfeedback@servicenow.com to submit your comments.
Versions
  • London
  • Kingston
  • Jakarta
  • Istanbul
  • Helsinki
  • Geneva
  • Store
Close

Using discovery script includes

Using discovery script includes

Discovery script includes define JavaScript classes that you can use to accomplish Discovery tasks.

Using GlideRecordUtil to Work with GlideRecords

GlideRecordUtil is a utility class that provides methods that are useful for working with GlideRecords during Discovery.

Getting a GlideRecord Instance

To get a GlideRecord instance for a given configuration item, and of the correct class and table, use the getCIGR(sys_id) method. For example, the following code gets the GlideRecord of a CI with the sys_id of 2dfd7c8437201000deeabfc8bcbe5d56:
var gr = new GlideRecordUtil().getCIGR("2dfd7c8437201000deeabfc8bcbe5d56");
To retrieve any hierarchical table without knowing its class type, use the getGR(base_table, sys_id) method. For instance, if you need to get a GlideRecord for a computer class CI, you may not know if it is a computer class or more specifically a Windows server class or Linux server class. Using this method guarantees that you have a GlideRecord with the correct class. This is important because different classes have different attributes—in this case, a Windows server has attributes different from those of a Linux server. You must get a GlideRecord in the correct class or attributes may be missing. The following is a typical example of how to do this:
var gr = new GlideRecordUtil().getGR( "cmdb_ci_computer", "2dfd7c8437201000deeabfc8bcbe5d56");

Getting All the Fields In a GlideRecord

The getFields(gr) method returns a JavaScript object, such as a hash map, of all the fields or attributes that exist in a given GlideRecord.
var gr = new GlideRecordUtil().getGR("cmdb_ci_computer", "2dfd7c8437201000deeabfc8bcbe5d56");
var fields = new GlideRecordUtil().getFields(gr);
gs.log(fields.join(" ")); // List all the fields that are in a computer CI

Populating GlideRecord Object Fields

The populateFromGR(hashmap, gr, ignore) method allows you to take a GlideRecord object and populate its fields and values into a JavaScript object. The third argument (ignore) is an optional JavaScript object that allows you to exclude certain fields. For example, you may not care about sys_created_by or sys_updated_by fields in a GlideRecord.
var objectToPopulate = { }; 
var gr = new GlideRecordUtil().getGR("cmdb_ci_computer", "2dfd7c8437201000deeabfc8bcbe5d56"); 
var ignore = {"sys_created_on": true, "sys_updated_by": true}; 
new GlideRecordUtil.03().populateFromGR(objectToPopulate, gr, ignore); 
// Now the objectToPopulate contains field/value pairs from the computer GlideRecord
The mergeToGR(hashmap, gr, ignore) method allows you to populate a GlideRecord with a field/value-paired object. The ignore argument stops specified fields from being updated. The following code example updates a computer record's name and os fields, but does not update the sys_created_by field:
var gr = new GlideRecordUtil().getGR("cmdb_ci_computer", "2dfd7c8437201000deeabfc8bcbe5d56"); 
var obj = {"name": "xyz", "os": "windows 2000", "sys_created_by", "aleck.lin"};
var ignore = {"sys_created_by": true}; 
new GlideRecordUtil().mergeToGR(obj, gr, ignore);
gr.update();

Getting Table Hierarchies

The getTables(table) method returns a list of table hierarchies, as shown in the following example:
var tables = new GlideRecordUtil().getTables("cmdb_ci_linux_server");
gs.log(tables.join(",")); 
// The result would be "cmdb_ci, cmdb_ci_computer, cmdb_ci_server, cmdb_ci_linux_server".

Using DiscoveryException and AutomationException

When writing Discovery sensors and sensor-related scripts, you may want to use DiscoveryException or AutomationException to indicate that an exception has come from Discovery.

The DiscoveryException script include extends AutomationException, which extends the GenericException class. The following example uses DiscoveryException to throw an exception:
function foo() { 
  if(//condition matches) throw new DiscoveryException("The message", "The cause"); }
The first argument takes the message of the exception and the second argument (optional) takes the cause of the exception. You may also want to catch the exception and log it as shown in the example below:
try {
  foo(); 
} 
catch(e) { 
   if(e instanceof DiscoveryException)
     gs.log("A DiscoveryException occurred. It is " + e. getMessage() + " caused by " + e.getCause()); }

The above example also applies for AutomationException. DiscoveryException is typically used to provide exception processing specifically for Discovery, while AutomationException is used for exception processing that applies to both Orchestration and Discovery.