java script code is not working in discovery pattern

mehak1890
Tera Expert

Hi All,

I'm facing issue in below code in pattern. when I'm running this code in background script, its working fine and I'm getting output but when I'm running under pattern then I'm not getting any output.

 

var DBHostname = ${DBinfo[*].DBhost};
var DBServiceName = ${DBinfo[*].Servicename};

var pval;

var serv = DBServiceName.substring(0, DBServiceName.indexOf('.'));
var host = DBHostname.substring(0, DBHostname.indexOf('-'));


var tablename = "cmdb_ci_unix_cluster_resource";
var gr = new GlideRecord ( 'cmdb_ci_unix_cluster_resource' );

gr.addQuery('cluster.name','CONTAINS', host);
gr.addQuery('name','CONTAINS', serv);
gr.query()
if (gr.next())
{

pval = gr.properties;


}

 

rtrn = pval;

 

Pls see the below screenshot where I'm not getting any output

 

mehak1890_1-1693239369794.png

Could you pls help me on this?

 

Thanks,

Mehak

8 REPLIES 8

Hi,

If  you have declared the rtrn variable your script should look like this, right?

 

var DBHostname = ${DBinfo[*].DBhost};
var DBServiceName = ${DBinfo[*].Servicename};

var pval;
var rtrn;
var serv = DBServiceName.substring(0, DBServiceName.indexOf('.'));
var host = DBHostname.substring(0, DBHostname.indexOf('-'));

var tablename = "cmdb_ci_unix_cluster_resource";
var gr = new GlideRecord ( 'cmdb_ci_unix_cluster_resource' );

gr.addQuery('cluster.name','CONTAINS', host);
gr.addQuery('name','CONTAINS', serv);
gr.query()
if (gr.next())
{
       pval = gr.properties;
}
rtrn = pval;

 

If I follow that logic and connect it your variables in the previous step. You are trying to fetch the properties attribute of the first matching CI in the cmdb_ci_unix_cluster_resource table where the Name contains "FINOLTIT" and has a Cluster reference with a Name that contains "LVLR4ORC3041Z".

If no such CI exists or if the properties attribute is empty, the returned value would be empty.

Can you log out the values using 

 

gs.log (serv);

gs.log (host);

gs.log (pval);

 

Regards,
Niklas

mehak1890
Tera Expert

Hi Niklas,

Based on condition, Record is available in table. It is printing the same while running via background script. However in Pattern gs.log is giving below error .

 

mehak1890_0-1693258433949.png

 

Thanks,

Mehak

 

Hi, 

Yes, sorry about that. Seems the logic in the script is correct but doing the gliderecord call in the pattern step is not allow due to it running on the MID server. You need to use a Pre processing script.
Here is a good blog article about it.

https://www.servicenow.com/community/in-other-news/using-gliderecord-scripting-in-service-mapping-pa...


Regards,
Niklas

Hi Niklas,

I tried below code for Pre processing script where I'm trying to pass all the records of unix cluster table into the pattern. So that I can filter them in pattern. 

 

mehak1890_1-1693297083650.png

 

But this variable "customer_db_server" is not available in pattern.

As per below link provide by you this variable should automatically available under temporary variable in pattern.

 

Pls advice.

Pls see the below code for Pre processing script.

/*
This script gets all Unix cluster resources that we have in the CMDB.
 
The sys_id is needed to create relationships in the pattern.
 */
var DEBUG = false;
var ports = "";
var separator = "";
var pre_pattern_statements = true;
if (pre_pattern_statements) {
    var data = new SNC.PrePatternExecutionData(); /* initialize the data table object **/
}
 
 
var DBCluster = [];
var glideRecord = new GlideRecord("cmdb_ci_unix_cluster_resource");
glideRecord.addQuery("resource_status", "ONLINE");
glideRecord.orderBy("resource_type");
glideRecord.query();
 
while (glideRecord.next()) {
 
    if (pre_pattern_statements) {
        /* write this server record to the customer db server table */
       DBCluster.push({
            /* create the table entry in the object */
            'name': glideRecord.getValue('name'),
            'properties': glideRecord.getValue('properties'),
            'cluster': glideRecord.getDisplayValue('cluster'),
            'sys_id': glideRecord.getValue('sys_id')
        });
 
    }
 
}
 
 
 
if (DEBUG)
    gs.log("Unix Cluster Resource discovery: " + DBCluster);
 
/*
 * Step 3: Define Credentialless Discovery Network device "ports" input parameter
 */
data.addTableEntry('customer_db_server', DBCluster);
 
//use this method if you want the pattern not to be executed
//data.executePattern(false);
 
//must return the data at end
if (pre_pattern_statements)
  {
rtrn = data;
  }

 

Thanks,

Mehak