ServiceNow Business Rule Script - CI

ServiceNow business rule script example.

Leave Feedback
(function executeRule(current, previous /*null when async*/) {
	//gs.addErrorMessage("ci table  :" + current.getTableName());

	//gs.addErrorMessage('OpsRamp CI ID: ' + current.u_opsramp_ci);
	//gs.addErrorMessage('OpsRamp client ID: ' + current.company.u_opsramp_client_id);
	//gs.addErrorMessage('SNOW CI sys_id: ' + current.sys_id);
	//gs.addErrorMessage('CI Updates: ' + current.sys_mod_count);

	//If it is Partner level integration, SNOW company is mapped to OpsRamp client
	//OpsRamp client id should be in available in SNOW company - Create custom field in Company 
	//and add the mapped OpsRamp client id here
	//var opsRampClientId = current.company.u_opsramp_client_id;

	//If it is client level integration, hard code the OpsRamp client id here
	var opsRampClientId = 'client_1341';

	try {
		var opsRampResourceType = 'Windows';
		if(current.getTableName() == 'cmdb_ci') {//Device Type in SNOW
			opsRampResourceType = 'POS Terminal';//Device Type in OpsRamp
		} else if (current.getTableName() == 'cmdb_ci_aix_server') {
			opsRampResourceType = 'AIX';
		} else if (current.getTableName() == 'cmdb_ci_appl_cisco_call_man') {
			opsRampResourceType = 'Cisco UCM Express';
		} else if (current.getTableName() == 'cmdb_ci_appl_groundwork') {
			opsRampResourceType = 'Work Station ';
		} else if (current.getTableName() == 'cmdb_ci_app_server_websphere') {
			opsRampResourceType = 'IBM STORWIZE';
		} else if (current.getTableName() == 'cmdb_ci_cluster_vip') {
			opsRampResourceType = 'Virtual IP';
		} else if (current.getTableName() == 'cmdb_ci_desktop_software') {
			opsRampResourceType = 'Desktop';
		} else if (current.getTableName() == 'cmdb_ci_config_automation_server') {
			opsRampResourceType = 'Management Controller';
		} else if (current.getTableName() == 'cmdb_ci_docker_engine') {
			opsRampResourceType = 'Docker';
		} else if (current.getTableName() == 'cmdb_ci_endpoint_dctm_index') {
			opsRampResourceType = 'OpsRamp Agent';
		} else if (current.getTableName() == 'cmdb_ci_generator_power_eq') {
			opsRampResourceType = 'Power';
		} else if (current.getTableName() == 'cmdb_ci_endpoint_storf') {
			opsRampResourceType = 'Xen Server';
		} else if (current.getTableName() == 'cmdb_ci_kvm') {
			opsRampResourceType = 'KVM';
		} else if (current.getTableName() == 'cmdb_ci_kvm_network') {
			opsRampResourceType = 'Network Device';
		} else if (current.getTableName() == 'cmdb_ci_lb') {
			opsRampResourceType = 'Load balancer';
		} else if (current.getTableName() == 'cmdb_ci_lb_cisco_csm') {
			opsRampResourceType = 'Cisco UCM';
		} else if (current.getTableName() == 'cmdb_ci_linux_server') {
			opsRampResourceType = 'Linux';
		} else if (current.getTableName() == 'cmdb_ci_personal_printer') {
			opsRampResourceType = 'Printer';
		} else if (current.getTableName() == 'cmdb_ci_server') {
			opsRampResourceType = 'Server';
		} else if (current.getTableName() == 'cmdb_ci_storage_switch') {
			opsRampResourceType = 'Switch';
		} 
	
		var r;
		var response;
		if(!JSUtil.nil(current.u_opsramp_resource_id)) { // Update CI
			//gs.addErrorMessage('CI Update...');
			r = new sn_ws.RESTMessageV2('OpsRamp CI REST', 'Update');
		
			r.setStringParameter('dsnNameToken',current.dns_domain);
			r.setStringParameter('hostNameToken',current.name);
			r.setStringParameter('resourceTypeToken',opsRampResourceType);
			r.setStringParameter('osToken',current.u_operating_system);
			r.setStringParameter('serialNumberToken',current.serial_number);
			r.setStringParameter('makeToken',current.u_make);
			r.setStringParameter('modelToken',current.model_id.getDisplayValue());
			r.setStringParameter('macAddressToken',current.mac_address);
			r.setStringParameter('ipAddressToken',current.ip_address);
			r.setStringParameter('extDeviceIdToken',current.sys_id);
			r.setStringParameter('tenantId',opsRampClientId);
			r.setStringParameter('deviceId',current.u_opsramp_ci);
			
			//Execute script
			response = r.execute();	
			//gs.addErrorMessage("status code: " + response.getStatusCode());
			//gs.addErrorMessage("status code: " + response.getErrorMessage());
			//var opsRampCIJSON = new JSON().decode(response.getBody());

		} else if(current.sys_mod_count == 0){ //Only CIs newly created	
			//gs.addErrorMessage('CI Create...');
			r = new sn_ws.RESTMessageV2('OpsRamp CI REST', 'Create');
		
			r.setStringParameter('dsnNameToken',current.dns_domain);
			r.setStringParameter('hostNameToken',current.name);
			r.setStringParameter('resourceTypeToken',opsRampResourceType);
			r.setStringParameter('osToken',current.u_operating_system);
			r.setStringParameter('serialNumberToken',current.serial_number);
			r.setStringParameter('makeToken',current.u_make);
			r.setStringParameter('modelToken',current.model_id.getDisplayValue());
			r.setStringParameter('macAddressToken',current.mac_address);
			r.setStringParameter('ipAddressToken',current.ip_address);
			r.setStringParameter('extDeviceIdToken',current.sys_id);
			r.setStringParameter('tenantId',opsRampClientId);
			
			response = r.execute();	
			//gs.addErrorMessage("status code: " + response.getStatusCode());
			//gs.addErrorMessage("status code: " + response.getErrorMessage());

			//Read OpsRamp CIId from response and update to SN CI.
			//gs.addErrorMessage('responseBody: ' + response.getBody());
			var opsRampCIJSON = new JSON().decode(response.getBody());
			var ci = new GlideRecord('cmdb_ci');
			ci.get(current.sys_id);
			ci.u_opsramp_ci = opsRampCIJSON.resourceUUID;
			ci.updateWithReferences();
		}
	} catch (e) {
		gs.addErrorMessage(e);
	}
})(current, previous);