Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Assigning value for reference field maps in Scripted IRE

Omar_Sanchez
Tera Contributor

Hello, 
I am currently working on a cmdb scoped application that imports external devices from a 3rd party into Servicenow. Currently working on importing software instances installed on devices. The devices themselves have already been imported into Servicenow. The code below creates the software instance record in the software instance table (cmdb_software_instance) and also creates the software record (smdb_ci_spkg) if it does not exist. What I am having issues with is assigning values to reference fields. software instance has a 'product name' column that creates a link to the software record. There is also another column which is the 'installed on' field which references the configuration item table (cmdb_ci). While the connection from software instnace to product table is included, the installed on field remains blank. Below is my current code where i use glide records to grab the sys id of the imported device. I have tried to cmdb_ci_hardware table as well as the cmdb_ci table. The devices themselves are included in the cmdb_win_server table and the cmdb_linux_server table. Going to try to change the glide record to search these tables, but at this point I am not sure if I am doing this correctly. There is also not much information (from my research) that explains how to do this. 

(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {
	
	//device instnace info
	var device_fk = source.u_device_fk.getDisplayValue();
	var device_name = source.u_device_name.getDisplayValue();
	var device_serial_no = source.u_serial_no.getDisplayValue();
	var device_uuid = source.u_uuid.getDisplayValue();

	//software instance info
	var software_name = source.u_software_name.getDisplayValue();
	var software_instance_pk = source.u_softwareinuse_pk.getDisplayValue(); //not used
	var software_fk = source.u_software_fk.getDisplayValue();
	var software_version = source.u_version.getDisplayValue(); 
	
	gr = new GlideRecord('cmdb_ci_hardware');
	gr.addQuery('correlation_id', device_fk);
	gr.query();
	
	
	var device;
	
	if(gr.hasNext())
	{
		device = gr.next();
	}
	
	if(device == null)
	{
		ignore = true;
		return;
	}
	
	var payload = {
		"items": [{
			"className": "cmdb_ci_spkg",
			"related": [{
				"className": "cmdb_software_instance",
				"values": {
					"name": software_name + "_instance",
					"installed_on": device.sys_id
				}
			}],		
			"values": {
				"name": software_name,
				"correlation_id": software_fk,
				"version": software_version,
				"key": software_name + '_::_' + software_version
			}
		}]
	};
	
	
    var input = new global.JSON().encode(payload);
    var output = sn_cmdb.IdentificationEngine.createOrUpdateCI("Device42", input);
    log.info(output);

    ignore = true; //ensures that the import set is not run
})(source, map, log, target);




1 ACCEPTED SOLUTION

Omar_Sanchez
Tera Contributor

Working now, in the original post there are a few syntax issues. Apologize for adding clutter to the community forum. I needed to declare the Gliderecord object. The next thing i needed to do was take the sys_id and convert it from UUID to string. record on cmdb_ci table is now being found. new code is as follows

(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {

	//device instance info
	var device_fk = source.u_device_fk.getDisplayValue();
	var device_name = source.u_device_name.getDisplayValue();
	//var device_serial_no = source.u_serial_no.getDisplayValue();
	//var device_uuid = source.u_uuid.getDisplayValue();

	//software instance info
	var software_name = source.u_software_name.getDisplayValue();
	//var software_instance_pk = source.u_softwareinuse_pk.getDisplayValue();
	var software_fk = source.u_software_fk.getDisplayValue();
	var software_version = source.u_version.getDisplayValue(); 
	
	//get the installed on device using device information from data source
	var gr = new GlideRecord('cmdb_ci');
	gr.addQuery('correlation_id', device_fk);
	gr.addQuery('name', device_name);
	gr.query();
	
	var device_sys_id;
	
	if(gr.hasNext())
	{
		gr.next();
		device_sys_id = gr.sys_id.toString();
	}
	
	if(device_sys_id == null)
	{
		log.info('failed to import software instance device due to missing sys_id');
		ignore = true;
		return;
	}

	//log.info('sys_id:' + device_sys_id);
	//log.info('Device: ' + device_name + ' Software: ' + software_name + ' Sys_ID: ' + device_sys_id);
	
	//create the payload
	var payload = {
		"items": [{
			"className": "cmdb_ci_spkg",
			"related": [{
				"className": "cmdb_software_instance",
				"values": {
					"name": software_name + "_instance",
					"installed_on": device_sys_id
				}
			}],		
			"values": {
				"name": software_name,
				"correlation_id": software_fk,
				"version": software_version,
				"key": software_name + '_::_' + software_version
			}
		}]
	};
	
	//send the payload to IRE
	
	log.info(JSON.stringify(payload));
    var input = new global.JSON().encode(payload);
    var output = sn_cmdb.IdentificationEngine.createOrUpdateCI("Device42", input);
    log.info(output);

    ignore = true; //ensures that the import set is not run
})(source, map, log, target);

View solution in original post

4 REPLIES 4

Omar_Sanchez
Tera Contributor

seems I may have been using the glide record object incorrectly, though I am still not able to get the installed on field populated. Below is a code snippet for how gr should be used (as far as i know)

 

gr = new GlideRecord('cmdb_ci_hardware');
	gr.addQuery('correlation_id', device_fk);
	gr.query();
	

	var device_syd_id;

	while(gr.hasNext())
	{
            gr.next();
            device_sys_id = gr.sys_id;       
	}

Did you update the installed on field to use the variable 

device_sys_id
Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022

hello, yes I did. everything is working now thanks

Omar_Sanchez
Tera Contributor

Working now, in the original post there are a few syntax issues. Apologize for adding clutter to the community forum. I needed to declare the Gliderecord object. The next thing i needed to do was take the sys_id and convert it from UUID to string. record on cmdb_ci table is now being found. new code is as follows

(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {

	//device instance info
	var device_fk = source.u_device_fk.getDisplayValue();
	var device_name = source.u_device_name.getDisplayValue();
	//var device_serial_no = source.u_serial_no.getDisplayValue();
	//var device_uuid = source.u_uuid.getDisplayValue();

	//software instance info
	var software_name = source.u_software_name.getDisplayValue();
	//var software_instance_pk = source.u_softwareinuse_pk.getDisplayValue();
	var software_fk = source.u_software_fk.getDisplayValue();
	var software_version = source.u_version.getDisplayValue(); 
	
	//get the installed on device using device information from data source
	var gr = new GlideRecord('cmdb_ci');
	gr.addQuery('correlation_id', device_fk);
	gr.addQuery('name', device_name);
	gr.query();
	
	var device_sys_id;
	
	if(gr.hasNext())
	{
		gr.next();
		device_sys_id = gr.sys_id.toString();
	}
	
	if(device_sys_id == null)
	{
		log.info('failed to import software instance device due to missing sys_id');
		ignore = true;
		return;
	}

	//log.info('sys_id:' + device_sys_id);
	//log.info('Device: ' + device_name + ' Software: ' + software_name + ' Sys_ID: ' + device_sys_id);
	
	//create the payload
	var payload = {
		"items": [{
			"className": "cmdb_ci_spkg",
			"related": [{
				"className": "cmdb_software_instance",
				"values": {
					"name": software_name + "_instance",
					"installed_on": device_sys_id
				}
			}],		
			"values": {
				"name": software_name,
				"correlation_id": software_fk,
				"version": software_version,
				"key": software_name + '_::_' + software_version
			}
		}]
	};
	
	//send the payload to IRE
	
	log.info(JSON.stringify(payload));
    var input = new global.JSON().encode(payload);
    var output = sn_cmdb.IdentificationEngine.createOrUpdateCI("Device42", input);
    log.info(output);

    ignore = true; //ensures that the import set is not run
})(source, map, log, target);