Links

This example shows how to query a record from within HPE Service Manager, and how to update fields in that record. Additionally, it shows how to transfer an array from Service Manager to JavaScript. An array from Service Manager cannot be directly copied into a JavaScript array, because the syntax of Service Manager {“IM1001”, “IM1002”} does not work in JavaScript. Properties such as the length property assume that the value is a function if it is enclosed in curly braces {}. To successfully convert a Service Manager array into a JavaScript array, you can either use the SCFile.toArray method, or create a while loop or a for loop to move through the elements and do an element-by-element transfer, as shown above.

Note In JavaScript, it is very important that you handle null values. Functions such as length may not work on null values.

In this example, the JavaScript code performs the following functions:

  • Queries the configuration item that was previously selected from a fill.
  • Increases a counter in that configuration item called num.issues.on.CI (this new field has to be added in the device file).
  • Adds the new record number to an array of issues in the device file called array.incidents.
  • Updates the device record.
  • Issues an error message if the configuration item does not exist.
  • Sends an alert to the person who is opening the record if the item is currently down.
var CI_Record=new SCFile("device"); /* opens the device file */
var CIName=system.vars.$File.logical_name; /* passes in the logical name from $File */
/* Selects the configuration item to update */
function getConfigurationItem( CIName )
{
var findCI_RC = CI_Record.doSelect( "logical.name=\""+ CIName + "\"" );
if (findCI_RC==RC_SUCCESS)
{
return CI_Record;
}
else
{
print( "Could not find configuration Item. " + 
RCtoString( findCI_RC ) );
return null;
}
}
/* Increases the use counter that counts how often this item was affected by an incident ticket and enters the incident ticket number into the array of incidents */
function increaseUseCounter(CI)
{
if (CI.is_down == true)
{
print("The configuration item is down at the moment. Please check 
existing tickets if the cause for this issue is related to the down 
system.");
}
CI.num_issues_on_CI++; /* increase counter */
var incNumber = system.vars.$File.number; /* get the incident ticket 
number */
var array_incidents = new Array();
var ind = 0;
while(CI.array_incidents[ind] != null) /* convert the SC array into a 
JS array (see paragraph below) */
{
array_incidents[ind]=CI.array_incidents[ind];
ind++;
}
var j=0;
var temp_string = array_incidents.toString();
var i = temp_string.indexOf(incNumber);
if (i < 0) /* Check if incident is already in the list */
{ /* If not, enter the incident number to the list */
if (array_incidents == null) 
{ 
j=0;
}
else 
{
j=array_incidents.length;
} 
array_incidents[j] = incNumber;
CI.array_incidents = array_incidents;
}
CI.doUpdate();
}
/* Call previously defined functions to execute code */
CI=getConfigurationItem(CIName);
increaseUseCounter(CI);