<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>question Fix Script to Create CI's on Assets in Developer forum</title>
    <link>https://www.servicenow.com/community/developer-forum/fix-script-to-create-ci-s-on-assets/m-p/1688074#M345000</link>
    <description>&lt;P&gt;Hello!&lt;/P&gt;
&lt;P&gt;I'm trying to create a fix script that will create CI records on assets (alm_asset) that do not have a CI.&lt;/P&gt;
&lt;P&gt;I'm using this Business Rule and Script Include to aid me in actually creating the CI.&lt;/P&gt;
&lt;P&gt;https://&amp;lt;instance-name&amp;gt;.service-now.com/nav_to.do?uri=sys_script.do?sys_id=26ebfeef1b3210002502fbcd2c07136f https://&amp;lt;instance-name&amp;gt;.service-now.com/nav_to.do?uri=sys_script_include.do?sys_id=19c0f3603703100044e0bfc8bcbe5d9b&lt;/P&gt;
&lt;P&gt;Here is my script below. I'm basically searching for all of my assets then going to loop though each calling the script include to create the CI for each.&lt;/P&gt;
&lt;PRE class="language-javascript"&gt;&lt;CODE&gt;var queryStr = 'ciISEMPTY^model_category!=fed01818db93ff00c0ede66505961962^ORmodel_category=NULL^model_category!=996b568ec3102000b959fd251eba8f19^model_category!=218323293743100044e0bfc8bcbe5d61^model_category!=35bf2d4137101000deeabfc8bcbe5dbd^model_category!=c9d5bc50c3031000b959fd251eba8fbc^model_category!=83c92540732623008b516cb63cf6a7b1^model_category!=58fdb454c3031000b959fd251eba8ff0';
var assets_no_ci = new GlideRecord('alm_asset');
//assets_no_ci.addEncodedQuery(queryStr); //Commented to test with a single record
assets_no_ci.addQuery('serial_number', '1234567890'); //test this script with a single record
assets_no_ci.orderBy('model_category');
assets_no_ci.query();

while (assets_no_ci.next()) {
   (new AssetandCI()).createCI(assets_no_ci);
    gs.log("Adding CI to Asset: " + assets_no_ci.display_name);
}&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;However when I run the script it gives me an error:&lt;/P&gt;
&lt;PRE class="language-javascript"&gt;&lt;CODE&gt;*** Script: Asset count: 1
Evaluator: org.mozilla.javascript.EvaluatorException: GlideRecord.insert() - invalid table name: null (sys_script_include.19c0f3603703100044e0bfc8bcbe5d9b.script; line 180)
   EvaluatorException(JavaScript evaluation error on:
var queryStr = 'ciISEMPTY^model_category!=fed01818db93ff00c0ede66505961962^ORmodel_category=NULL^model_category!=996b568ec3102000b959fd251eba8f19^model_category!=218323293743100044e0bfc8bcbe5d61^model_category!=35bf2d4137101000deeabfc8bcbe5dbd^model_category!=c9d5bc50c3031000b959fd251eba8fbc^model_category!=83c92540732623008b516cb63cf6a7b1^model_category!=58fdb454c3031000b959fd251eba8ff0';
var assets_no_ci = new GlideRecord('alm_asset');
//assets_no_ci.addEncodedQuery(queryStr); //Commented to test with a single record
assets_no_ci.addQuery('serial_number', '1234567890'); //test this script with a single template
assets_no_ci.orderBy('model_category');
assets_no_ci.query();

while (assets_no_ci.next()) {
   (new AssetandCI()).createCI(assets_no_ci);
    gs.log("Adding CI to Asset: " + assets_no_ci.display_name);
}
)
[0:00:00.055] Total Time&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Any thoughts or help would be appreciated.&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;
&lt;P&gt;Dom&lt;/P&gt;</description>
    <pubDate>Wed, 23 Sep 2020 20:08:00 GMT</pubDate>
    <dc:creator>NFGDom</dc:creator>
    <dc:date>2020-09-23T20:08:00Z</dc:date>
    <item>
      <title>Fix Script to Create CI's on Assets</title>
      <link>https://www.servicenow.com/community/developer-forum/fix-script-to-create-ci-s-on-assets/m-p/1688074#M345000</link>
      <description>&lt;P&gt;Hello!&lt;/P&gt;
&lt;P&gt;I'm trying to create a fix script that will create CI records on assets (alm_asset) that do not have a CI.&lt;/P&gt;
&lt;P&gt;I'm using this Business Rule and Script Include to aid me in actually creating the CI.&lt;/P&gt;
&lt;P&gt;https://&amp;lt;instance-name&amp;gt;.service-now.com/nav_to.do?uri=sys_script.do?sys_id=26ebfeef1b3210002502fbcd2c07136f https://&amp;lt;instance-name&amp;gt;.service-now.com/nav_to.do?uri=sys_script_include.do?sys_id=19c0f3603703100044e0bfc8bcbe5d9b&lt;/P&gt;
&lt;P&gt;Here is my script below. I'm basically searching for all of my assets then going to loop though each calling the script include to create the CI for each.&lt;/P&gt;
&lt;PRE class="language-javascript"&gt;&lt;CODE&gt;var queryStr = 'ciISEMPTY^model_category!=fed01818db93ff00c0ede66505961962^ORmodel_category=NULL^model_category!=996b568ec3102000b959fd251eba8f19^model_category!=218323293743100044e0bfc8bcbe5d61^model_category!=35bf2d4137101000deeabfc8bcbe5dbd^model_category!=c9d5bc50c3031000b959fd251eba8fbc^model_category!=83c92540732623008b516cb63cf6a7b1^model_category!=58fdb454c3031000b959fd251eba8ff0';
var assets_no_ci = new GlideRecord('alm_asset');
//assets_no_ci.addEncodedQuery(queryStr); //Commented to test with a single record
assets_no_ci.addQuery('serial_number', '1234567890'); //test this script with a single record
assets_no_ci.orderBy('model_category');
assets_no_ci.query();

while (assets_no_ci.next()) {
   (new AssetandCI()).createCI(assets_no_ci);
    gs.log("Adding CI to Asset: " + assets_no_ci.display_name);
}&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;However when I run the script it gives me an error:&lt;/P&gt;
&lt;PRE class="language-javascript"&gt;&lt;CODE&gt;*** Script: Asset count: 1
Evaluator: org.mozilla.javascript.EvaluatorException: GlideRecord.insert() - invalid table name: null (sys_script_include.19c0f3603703100044e0bfc8bcbe5d9b.script; line 180)
   EvaluatorException(JavaScript evaluation error on:
var queryStr = 'ciISEMPTY^model_category!=fed01818db93ff00c0ede66505961962^ORmodel_category=NULL^model_category!=996b568ec3102000b959fd251eba8f19^model_category!=218323293743100044e0bfc8bcbe5d61^model_category!=35bf2d4137101000deeabfc8bcbe5dbd^model_category!=c9d5bc50c3031000b959fd251eba8fbc^model_category!=83c92540732623008b516cb63cf6a7b1^model_category!=58fdb454c3031000b959fd251eba8ff0';
var assets_no_ci = new GlideRecord('alm_asset');
//assets_no_ci.addEncodedQuery(queryStr); //Commented to test with a single record
assets_no_ci.addQuery('serial_number', '1234567890'); //test this script with a single template
assets_no_ci.orderBy('model_category');
assets_no_ci.query();

while (assets_no_ci.next()) {
   (new AssetandCI()).createCI(assets_no_ci);
    gs.log("Adding CI to Asset: " + assets_no_ci.display_name);
}
)
[0:00:00.055] Total Time&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Any thoughts or help would be appreciated.&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;
&lt;P&gt;Dom&lt;/P&gt;</description>
      <pubDate>Wed, 23 Sep 2020 20:08:00 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/fix-script-to-create-ci-s-on-assets/m-p/1688074#M345000</guid>
      <dc:creator>NFGDom</dc:creator>
      <dc:date>2020-09-23T20:08:00Z</dc:date>
    </item>
    <item>
      <title>Re: Fix Script to Create CI's on Assets</title>
      <link>https://www.servicenow.com/community/developer-forum/fix-script-to-create-ci-s-on-assets/m-p/1688075#M345001</link>
      <description>&lt;P&gt;Hi Dom,&lt;/P&gt;
&lt;P&gt;Have you / someone in your organisation edited AssetandCI script include as line 180 (which is erroring) is a curly bracket in my OOB script.&lt;/P&gt;
&lt;P&gt;My only other suggestion, do the alm_asset records have a model category and do those categories have a CI class?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 23 Sep 2020 20:39:12 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/fix-script-to-create-ci-s-on-assets/m-p/1688075#M345001</guid>
      <dc:creator>Kieran Anson</dc:creator>
      <dc:date>2020-09-23T20:39:12Z</dc:date>
    </item>
    <item>
      <title>Re: Fix Script to Create CI's on Assets</title>
      <link>https://www.servicenow.com/community/developer-forum/fix-script-to-create-ci-s-on-assets/m-p/1688076#M345002</link>
      <description>&lt;P&gt;Hey Kieran,&lt;/P&gt;
&lt;P&gt;Thanks for the response!&lt;/P&gt;
&lt;P&gt;We have not edited the script include AssetandCI, it is as is from OOB.&lt;/P&gt;
&lt;P&gt;The alm_asset records in my query do indeed have a model category and do have a CI class.&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;Dom&lt;/P&gt;</description>
      <pubDate>Thu, 24 Sep 2020 11:11:54 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/fix-script-to-create-ci-s-on-assets/m-p/1688076#M345002</guid>
      <dc:creator>NFGDom</dc:creator>
      <dc:date>2020-09-24T11:11:54Z</dc:date>
    </item>
    <item>
      <title>Re: Fix Script to Create CI's on Assets</title>
      <link>https://www.servicenow.com/community/developer-forum/fix-script-to-create-ci-s-on-assets/m-p/1688077#M345003</link>
      <description>&lt;P&gt;Interesting, could you try the below script? It's an extract of the inner workings of the AssetandCI script include, just removed some of the boilerplate code and stripped it down to its core.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If it errors, likely the issue is with one of the further referenced SIs (AssetCMDBUtil or AssetAndSynchronizer)&lt;/P&gt;
&lt;PRE class="language-javascript"&gt;&lt;CODE&gt;var queryStr = 'ciISEMPTY^model_category!=fed01818db93ff00c0ede66505961962^ORmodel_category=NULL^model_category!=996b568ec3102000b959fd251eba8f19^model_category!=218323293743100044e0bfc8bcbe5d61^model_category!=35bf2d4137101000deeabfc8bcbe5dbd^model_category!=c9d5bc50c3031000b959fd251eba8fbc^model_category!=83c92540732623008b516cb63cf6a7b1^model_category!=58fdb454c3031000b959fd251eba8ff0';
var assets_no_ci = new GlideRecord('alm_asset');
//assets_no_ci.addEncodedQuery(queryStr); //Commented to test with a single record
assets_no_ci.addQuery('serial_number', '1234567890'); //test this script with a single record
assets_no_ci.orderBy('model_category');
assets_no_ci.query();

while (assets_no_ci.next()) {
 createAsset(assets_no_ci);
  gs.log("Adding CI to Asset: " + assets_no_ci.display_name);
}


function createAsset(asset){

  if (ciClass != '') {
    var ciSysId;
    var ci = new GlideRecord(ciClass);
    ci.initialize();
    ci.asset = asset.sys_id;
    ci.name = asset.model.name;
    // Populate manufacturer
    ci.manufacturer = asset.model.manufacturer;
   
    var sync = new AssetAndCISynchronizer();
    sync.syncRecordsWithoutUpdate(asset, ci, 'cmdb_ci', false);
   
    var assetCMDBUtil = new AssetCMDBUtil();
    if (asset.install_status != 2
      &amp;amp;&amp;amp; !gs.nil(asset.serial_number)
      &amp;amp;&amp;amp; assetCMDBUtil.isSerialNumberMandatory(asset.model_category)) {
      ci.name = asset.serial_number + ' - ' + asset.model.name;
      ciSysId = assetCMDBUtil.createCIUsingIRE(ci);
    } else {
      ciSysId = ci.insert();
    }
    asset.ci = ciSysId;
  }
}&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 24 Sep 2020 13:36:49 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/fix-script-to-create-ci-s-on-assets/m-p/1688077#M345003</guid>
      <dc:creator>Kieran Anson</dc:creator>
      <dc:date>2020-09-24T13:36:49Z</dc:date>
    </item>
    <item>
      <title>Re: Fix Script to Create CI's on Assets</title>
      <link>https://www.servicenow.com/community/developer-forum/fix-script-to-create-ci-s-on-assets/m-p/1688078#M345004</link>
      <description>&lt;P&gt;When I ran the snippet, I got the below. I double checked the assets in my query and all have a class of hardware.&lt;/P&gt;
&lt;PRE class="language-javascript"&gt;&lt;CODE&gt;Evaluator: org.mozilla.javascript.EcmaError: "ciClass" is not defined.
   Caused by error in script at line 33

     30: 
     31: function createAsset(asset){
     32: 
==&amp;gt;  33:   if (ciClass != '') {
     34:     var ciSysId;
     35:     var ci = new GlideRecord(ciClass);
     36:     ci.initialize();

Evaluator: org.mozilla.javascript.EcmaError: "ciClass" is not defined.
   Caused by error in script at line 26

     23: assets_no_ci.query();
     24: 
     25: while (assets_no_ci.next()) {
==&amp;gt;  26:  createAsset(assets_no_ci);
     27:   gs.log("Adding CI to Asset: " + assets_no_ci.display_name);
     28: }
     29: 

[0:00:00.061] Total Time&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;Dom&lt;/P&gt;</description>
      <pubDate>Thu, 24 Sep 2020 13:53:13 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/fix-script-to-create-ci-s-on-assets/m-p/1688078#M345004</guid>
      <dc:creator>NFGDom</dc:creator>
      <dc:date>2020-09-24T13:53:13Z</dc:date>
    </item>
    <item>
      <title>Re: Fix Script to Create CI's on Assets</title>
      <link>https://www.servicenow.com/community/developer-forum/fix-script-to-create-ci-s-on-assets/m-p/1688079#M345005</link>
      <description>&lt;P&gt;Dang, helps if I copy and paste properly!&lt;/P&gt;
&lt;PRE class="language-javascript"&gt;&lt;CODE&gt;var queryStr = 'ciISEMPTY^model_category!=fed01818db93ff00c0ede66505961962^ORmodel_category=NULL^model_category!=996b568ec3102000b959fd251eba8f19^model_category!=218323293743100044e0bfc8bcbe5d61^model_category!=35bf2d4137101000deeabfc8bcbe5dbd^model_category!=c9d5bc50c3031000b959fd251eba8fbc^model_category!=83c92540732623008b516cb63cf6a7b1^model_category!=58fdb454c3031000b959fd251eba8ff0';
var assets_no_ci = new GlideRecord('alm_asset');
//assets_no_ci.addEncodedQuery(queryStr); //Commented to test with a single record
assets_no_ci.addQuery('serial_number', '1234567890'); //test this script with a single record
assets_no_ci.orderBy('model_category');
assets_no_ci.query();

while (assets_no_ci.next()) {
 createAsset(assets_no_ci);
  gs.log("Adding CI to Asset: " + assets_no_ci.display_name);
}

var ciClass = '';
		if ('leave_to_category' == asset.model.asset_tracking_strategy
			.toString())
		ciClass = asset.model_category.cmdb_ci_class.toString();

function createAsset(asset){

  if (ciClass != '') {
    var ciSysId;
    var ci = new GlideRecord(ciClass);
    ci.initialize();
    ci.asset = asset.sys_id;
    ci.name = asset.model.name;
    // Populate manufacturer
    ci.manufacturer = asset.model.manufacturer;
   
    var sync = new AssetAndCISynchronizer();
    sync.syncRecordsWithoutUpdate(asset, ci, 'cmdb_ci', false);
   
    var assetCMDBUtil = new AssetCMDBUtil();
    if (asset.install_status != 2
      &amp;amp;&amp;amp; !gs.nil(asset.serial_number)
      &amp;amp;&amp;amp; assetCMDBUtil.isSerialNumberMandatory(asset.model_category)) {
      ci.name = asset.serial_number + ' - ' + asset.model.name;
      ciSysId = assetCMDBUtil.createCIUsingIRE(ci);
    } else {
      ciSysId = ci.insert();
    }
    asset.ci = ciSysId;
  }
}&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 24 Sep 2020 14:09:57 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/fix-script-to-create-ci-s-on-assets/m-p/1688079#M345005</guid>
      <dc:creator>Kieran Anson</dc:creator>
      <dc:date>2020-09-24T14:09:57Z</dc:date>
    </item>
    <item>
      <title>Re: Fix Script to Create CI's on Assets</title>
      <link>https://www.servicenow.com/community/developer-forum/fix-script-to-create-ci-s-on-assets/m-p/1688080#M345006</link>
      <description>&lt;P&gt;lol no worries! Here is what I get - no success yet:&lt;/P&gt;
&lt;PRE class="language-javascript"&gt;&lt;CODE&gt;Evaluator: org.mozilla.javascript.EcmaError: "AssetCMDBUtil" is not defined.
   Caused by error in script at line 49

     46:     var sync = new AssetAndCISynchronizer();
     47:     sync.syncRecordsWithoutUpdate(asset, ci, 'cmdb_ci', false);
     48:    
==&amp;gt;  49:     var assetCMDBUtil = new AssetCMDBUtil();
     50:     if (asset.install_status != 2
     51:       &amp;amp;&amp;amp; !gs.nil(asset.serial_number)
     52:       &amp;amp;&amp;amp; assetCMDBUtil.isSerialNumberMandatory(asset.model_category)) {

Evaluator: org.mozilla.javascript.EcmaError: "AssetCMDBUtil" is not defined.
   Caused by error in script at line 26

     23: assets_no_ci.query();
     24: 
     25: while (assets_no_ci.next()) {
==&amp;gt;  26:  createAsset(assets_no_ci);
     27:   gs.log("Adding CI to Asset: " + assets_no_ci.display_name);
     28: }
     29: 

[0:00:00.078] Total Time&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 24 Sep 2020 14:14:27 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/fix-script-to-create-ci-s-on-assets/m-p/1688080#M345006</guid>
      <dc:creator>NFGDom</dc:creator>
      <dc:date>2020-09-24T14:14:27Z</dc:date>
    </item>
    <item>
      <title>Re: Fix Script to Create CI's on Assets</title>
      <link>https://www.servicenow.com/community/developer-forum/fix-script-to-create-ci-s-on-assets/m-p/1688081#M345007</link>
      <description>&lt;P&gt;Ah we're getting there,&lt;/P&gt;
&lt;P&gt;can you check your script include table and look for a record called&amp;nbsp;AssetCMDBUtil. Looks like it currently doesn't exist as servicenow isn't able to find it, possible someone accidentally renamed it?&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="find_real_file.png"&gt;&lt;img src="https://www.servicenow.com/community/image/serverpage/image-id/200731iAEDC9A1A34A05234/image-size/large?v=v2&amp;amp;px=999" role="button" title="find_real_file.png" alt="find_real_file.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you can't find it, create a new script include and call it &lt;EM&gt;AssetCMDBUtil&lt;/EM&gt; and ensure you're in the global scope. copy the following script into the file and save it&lt;/P&gt;
&lt;PRE class="language-javascript"&gt;&lt;CODE&gt;var AssetCMDBUtil = Class.create();
AssetCMDBUtil.prototype = {
	initialize: function() {
		this.jsonApi = new global.JSON();
		this.cmdbApi = SNC.IdentificationEngineScriptableApi;
	},

	isSerialNumberMandatory: function(modelCategory) {
		if (gs.getProperty(AssetCMDBUtil.ASSET_IRE_PROPERTY) === 'true') {
			var modelCategoryGr = new GlideRecord('cmdb_model_category');
			if (modelCategoryGr.get(modelCategory)) {
				var ciClass = modelCategoryGr.getValue('cmdb_ci_class');
				if (!gs.nil(ciClass)) {
					var res = SNC.CmdbMetadataScriptableApi.hasSerialNumberRuleWithNoRelationship(ciClass);
					return res;
				}
			}
		}
		return false;
	},

	getMatchRuleInfo: function(attempts) {
		var matchRuleInfo = {};
		var attempt;
		if (gs.nil(attempts)) {
			return matchRuleInfo;
		}
		for (var element in attempts) {
			attempt = attempts[element];
			if (attempt.attemptResult === 'MATCHED') {
				matchRuleInfo.identifier = !gs.nil(attempt.identifierName) ? attempt.identifierName : '';
				matchRuleInfo.table = !gs.nil(attempt.searchOnTable) ? attempt.searchOnTable : '';
				matchRuleInfo.attributes = !gs.nil(attempt.attributes) ? attempt.attributes.join(', ') : '';
				break;
			}
		}
		return matchRuleInfo;
	},

	parseIDResult: function(result) {
		var idResultObj = {
			success: false,
			insert: false,
			sysId: null,
			className: null,
			attempts: [],
			matchedRule: {},
			errors: [],
		};

		if (JSUtil.nil(result) || JSUtil.nil(result.items) || !result.items.length || result.items.length &amp;gt; 1) {
			return idResultObj;
		}

		// There should be only one item returned
		var item = result.items[0];

		if (item.errors) {
			idResultObj.errors = item.errors;
			return idResultObj;
		}

		idResultObj.success = true;
		idResultObj.insert = item.operation === 'INSERT';
		idResultObj.matchedRule = this.getMatchRuleInfo(item.identificationAttempts);
		idResultObj.sysId = !gs.nil(item.sysId) ? item.sysId : null;
		idResultObj.className = item.className;
		idResultObj.attempts = item.identificationAttempts;

		return idResultObj;
	},

	checkInsertOrUpdate: function(input) {
		var result = this.jsonApi.decode(this.cmdbApi.identifyCI(input));
		return this.parseIDResult(result);
	},

	insertOrUpdate: function(source, input) {
		var result = this.jsonApi.decode(this.cmdbApi.createOrUpdateCI(source, input));
		return this.parseIDResult(result);
	},

	createCIUsingIRE: function(ci) {
		var ciJSON = {};
		var columnName;
		var fields = ci.getFields();
		for (var num = 0; num &amp;lt; fields.size(); num++) {
			columnName = fields.get(num).getName();
			if (columnName.indexOf('sys_') !== 0 &amp;amp;&amp;amp; ci[columnName].changes()) {
				ciJSON[columnName] = ci.getValue(columnName);
			}
		}

		var payload = {
			items: [{
				className: ci.getTableName(),
				values: ciJSON,
			}],
		};
		var input = this.jsonApi.encode(payload);

		var ciSysId;
		var checkResult = this.checkInsertOrUpdate(input);
		if (!checkResult.success) {
			throw new Error(gs.getMessage('CMDB Identification Error occured during creation of CI'));
		}
		if (checkResult.success &amp;amp;&amp;amp; !checkResult.insert &amp;amp;&amp;amp; checkResult.sysId) {
			var link = '&amp;lt;a href=/cmdb_ci.do?sys_id=' + checkResult.sysId + '&amp;gt;Configuration Item&amp;lt;/a&amp;gt;';
			var msg = gs.getMessage('Duplicate Entry - Matching {0} found', link);
			throw new Error(msg);
		} else {
			var insertOrUpdateResult = this.insertOrUpdate(AssetCMDBUtil.DISCOVERY_SOURCE, input);
			if (!insertOrUpdateResult.success) {
				throw new Error(gs.getMessage('CMDB Identification Error occured during creation of CI'));
			}
			if (insertOrUpdateResult.success &amp;amp;&amp;amp; JSUtil.notNil(insertOrUpdateResult.sysId)) {
				ciSysId = insertOrUpdateResult.sysId;
			}
		}
		return ciSysId;
	},

	type: 'AssetCMDBUtil',
};
AssetCMDBUtil.ASSET_IRE_PROPERTY = 'glide.asset.create_ci_with_ire';
AssetCMDBUtil.DISCOVERY_SOURCE = 'SNAssetManagement';&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 24 Sep 2020 14:23:36 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/fix-script-to-create-ci-s-on-assets/m-p/1688081#M345007</guid>
      <dc:creator>Kieran Anson</dc:creator>
      <dc:date>2020-09-24T14:23:36Z</dc:date>
    </item>
    <item>
      <title>Re: Fix Script to Create CI's on Assets</title>
      <link>https://www.servicenow.com/community/developer-forum/fix-script-to-create-ci-s-on-assets/m-p/1688082#M345008</link>
      <description>&lt;P&gt;Hello User,&lt;/P&gt;
&lt;P&gt;Why are taking so much effort,Service already gave you OOTB business rule to create CI after updating Asset Record.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="find_real_file.png"&gt;&lt;img src="https://www.servicenow.com/community/image/serverpage/image-id/200732iA1782339A3AFAC56/image-size/large?v=v2&amp;amp;px=999" role="button" title="find_real_file.png" alt="find_real_file.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Why you are not using this from the above.&lt;/P&gt;
&lt;P&gt;I hope you got my point.&lt;/P&gt;
&lt;P&gt;Please Mark it helpful/correct if my answer helps in any way to resolve your query.&lt;BR /&gt;Reach out to me if any more help required.&lt;/P&gt;
&lt;P&gt;Regards&lt;/P&gt;
&lt;P&gt;Yash.K.Agrawal&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 24 Sep 2020 14:28:29 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/fix-script-to-create-ci-s-on-assets/m-p/1688082#M345008</guid>
      <dc:creator>Yash Agrawal1</dc:creator>
      <dc:date>2020-09-24T14:28:29Z</dc:date>
    </item>
    <item>
      <title>Re: Fix Script to Create CI's on Assets</title>
      <link>https://www.servicenow.com/community/developer-forum/fix-script-to-create-ci-s-on-assets/m-p/1688083#M345009</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;I would strongly suggest you use the above business rule code.and create a new one with modifications in it as per your requirement.&lt;/P&gt;
&lt;P&gt;This business rule is written on the ASSET table for creating and updating CI record.&lt;/P&gt;
&lt;P&gt;This may help you a lot.&lt;/P&gt;
&lt;P&gt;Please keep posted if more help required.&lt;/P&gt;
&lt;P&gt;Regards,&lt;/P&gt;
&lt;P&gt;Yash Agrawal&lt;/P&gt;</description>
      <pubDate>Thu, 24 Sep 2020 14:30:32 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/fix-script-to-create-ci-s-on-assets/m-p/1688083#M345009</guid>
      <dc:creator>Yash Agrawal1</dc:creator>
      <dc:date>2020-09-24T14:30:32Z</dc:date>
    </item>
    <item>
      <title>Re: Fix Script to Create CI's on Assets</title>
      <link>https://www.servicenow.com/community/developer-forum/fix-script-to-create-ci-s-on-assets/m-p/1688084#M345010</link>
      <description>&lt;P&gt;Hi Yash,&lt;/P&gt;
&lt;P&gt;Unfortunately do to an enhancement we made months ago it broke&amp;nbsp;creation of CI's of new assets&amp;nbsp;without us knowing until recently. Now we're trying to do a one time clean-up for those assets that should have&amp;nbsp; CI.&lt;/P&gt;
&lt;P&gt;We could update the business rule to create the CI on update, however that means manually going though all the assets and clicking update. Now we're at the point of just creating a script to handle it.&lt;/P&gt;
&lt;P&gt;Thank you for your input!&lt;/P&gt;
&lt;P&gt;Dom&lt;/P&gt;</description>
      <pubDate>Thu, 24 Sep 2020 14:52:58 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/fix-script-to-create-ci-s-on-assets/m-p/1688084#M345010</guid>
      <dc:creator>NFGDom</dc:creator>
      <dc:date>2020-09-24T14:52:58Z</dc:date>
    </item>
    <item>
      <title>Re: Fix Script to Create CI's on Assets</title>
      <link>https://www.servicenow.com/community/developer-forum/fix-script-to-create-ci-s-on-assets/m-p/1688085#M345011</link>
      <description>&lt;P&gt;It didn't seem to exist in our instance and I wasn't able to find anything similar.&lt;/P&gt;
&lt;P&gt;After creating it, this is what I got:&lt;/P&gt;
&lt;PRE class="language-javascript"&gt;&lt;CODE&gt;Evaluator: org.mozilla.javascript.EvaluatorException: GlideRecord.insert() - invalid table name: null (&amp;lt;refname&amp;gt;; line 39)
   Line(39)
	} else {
evaluateString(JavaScript evaluation error on:
var queryStr = 'ciISEMPTY^model_category!=fed01818db93ff00c0ede66505961962^ORmodel_category=NULL^model_category!=996b568ec3102000b959fd251eba8f19^model_category!=218323293743100044e0bfc8bcbe5d61^model_category!=35bf2d4137101000deeabfc8bcbe5dbd^model_category!=c9d5bc50c3031000b959fd251eba8fbc^model_category!=83c92540732623008b516cb63cf6a7b1^model_category!=58fdb454c3031000b959fd251eba8ff0';
var assets_no_ci = new GlideRecord('alm_asset');
//assets_no_ci.addEncodedQuery(queryStr); //Commented to test with a single record
assets_no_ci.addQuery('serial_number', '041804401557'); //test this script with a single record
assets_no_ci.orderBy('model_category');
assets_no_ci.query();

while (assets_no_ci.next()) {
 createAsset(assets_no_ci);
  gs.log("Adding CI to Asset: " + assets_no_ci.display_name);
}

var ciClass = '';
		if ('leave_to_category' == asset.model.asset_tracking_strategy
			.toString())
		ciClass = asset.model_category.cmdb_ci_class.toString();

function createAsset(asset){

  if (ciClass != '') {
    var ciSysId;
    var ci = new GlideRecord(ciClass);
    ci.initialize();
    ci.asset = asset.sys_id;
    ci.name = asset.model.name;
    // Populate manufacturer
    ci.manufacturer = asset.model.manufacturer;
   
    var sync = new AssetAndCISynchronizer();
    sync.syncRecordsWithoutUpdate(asset, ci, 'cmdb_ci', false);
   
    var assetCMDBUtil = new AssetCMDBUtil();
    if (asset.install_status != 2
      &amp;amp;&amp;amp; !gs.nil(asset.serial_number)
      &amp;amp;&amp;amp; assetCMDBUtil.isSerialNumberMandatory(asset.model_category)) {
      ci.name = asset.serial_number + ' - ' + asset.model.name;
      ciSysId = assetCMDBUtil.createCIUsingIRE(ci);
    } else {
      ciSysId = ci.insert();
    }
    asset.ci = ciSysId;
  }
}
)
[0:00:00.055] Total Time&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 24 Sep 2020 14:59:09 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/fix-script-to-create-ci-s-on-assets/m-p/1688085#M345011</guid>
      <dc:creator>NFGDom</dc:creator>
      <dc:date>2020-09-24T14:59:09Z</dc:date>
    </item>
    <item>
      <title>Re: Fix Script to Create CI's on Assets</title>
      <link>https://www.servicenow.com/community/developer-forum/fix-script-to-create-ci-s-on-assets/m-p/1688086#M345012</link>
      <description>&lt;P&gt;Hello MFG,&lt;/P&gt;
&lt;P&gt;So, if you want help, please provide the details description of the issue.&lt;/P&gt;
&lt;P&gt;So that I can also help you, to achieve it.&lt;/P&gt;
&lt;P&gt;Regards,&lt;/P&gt;
&lt;P&gt;Yash Agrawal&lt;/P&gt;</description>
      <pubDate>Thu, 24 Sep 2020 15:03:03 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/fix-script-to-create-ci-s-on-assets/m-p/1688086#M345012</guid>
      <dc:creator>Yash Agrawal1</dc:creator>
      <dc:date>2020-09-24T15:03:03Z</dc:date>
    </item>
    <item>
      <title>Re: Fix Script to Create CI's on Assets</title>
      <link>https://www.servicenow.com/community/developer-forum/fix-script-to-create-ci-s-on-assets/m-p/1688087#M345013</link>
      <description>&lt;P&gt;Hi Yash,&lt;/P&gt;
&lt;P&gt;In short we're looking to create CI's and associate them to asset records that do not have CI's for specific model categories. The details on this query are above in my code snippet.&lt;/P&gt;
&lt;P&gt;Thank you!&lt;/P&gt;
&lt;P&gt;Dom&lt;/P&gt;</description>
      <pubDate>Thu, 24 Sep 2020 15:06:34 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/fix-script-to-create-ci-s-on-assets/m-p/1688087#M345013</guid>
      <dc:creator>NFGDom</dc:creator>
      <dc:date>2020-09-24T15:06:34Z</dc:date>
    </item>
    <item>
      <title>Re: Fix Script to Create CI's on Assets</title>
      <link>https://www.servicenow.com/community/developer-forum/fix-script-to-create-ci-s-on-assets/m-p/1688088#M345014</link>
      <description>&lt;P&gt;Hello ,&lt;/P&gt;
&lt;P&gt;did you check how much parameter CreateCi() function wants.&lt;/P&gt;
&lt;P&gt;second point why you gave ^ORmodel_category=Null&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="token keyword"&gt;var&lt;/SPAN&gt; queryStr &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; &lt;SPAN class="token string"&gt;'ciISEMPTY^model_category!=fed01818db93ff00c0ede66505961962^ORmodel_category=NULL&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="color: #000000;"&gt;It should be&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="color: #000000;"&gt;^model_category==NULL&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One more error if you want to pass asset sys_id in createCi() function then pass it like&lt;/P&gt;
&lt;P&gt;CreateCi(assets_no_ci.sys_id)&lt;/P&gt;
&lt;P&gt;So that it will pass sys_id of asset record to function.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="color: #000000;"&gt;please try to check the mentioned point.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="color: #000000;"&gt;Your query will. get reaolved.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="color: #000000;"&gt;Regards&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="color: #000000;"&gt;Yash Agrawal&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 24 Sep 2020 15:37:48 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/fix-script-to-create-ci-s-on-assets/m-p/1688088#M345014</guid>
      <dc:creator>Yash Agrawal1</dc:creator>
      <dc:date>2020-09-24T15:37:48Z</dc:date>
    </item>
    <item>
      <title>Re: Fix Script to Create CI's on Assets</title>
      <link>https://www.servicenow.com/community/developer-forum/fix-script-to-create-ci-s-on-assets/m-p/1688089#M345015</link>
      <description>&lt;P&gt;Added a few logging statements to get a bit more detail other than ci.insert failure.&lt;/P&gt;
&lt;PRE class="language-javascript"&gt;&lt;CODE&gt;var queryStr = 'ciISEMPTY^model_category!=fed01818db93ff00c0ede66505961962^ORmodel_category=NULL^model_category!=996b568ec3102000b959fd251eba8f19^model_category!=218323293743100044e0bfc8bcbe5d61^model_category!=35bf2d4137101000deeabfc8bcbe5dbd^model_category!=c9d5bc50c3031000b959fd251eba8fbc^model_category!=83c92540732623008b516cb63cf6a7b1^model_category!=58fdb454c3031000b959fd251eba8ff0';
var assets_no_ci = new GlideRecord('alm_asset');
//assets_no_ci.addEncodedQuery(queryStr); //Commented to test with a single record
assets_no_ci.addQuery('serial_number', '1234567890'); //test this script with a single record
assets_no_ci.orderBy('model_category');
assets_no_ci.query();

while (assets_no_ci.next()) {
 createAsset(assets_no_ci);
  gs.log("Adding CI to Asset: " + assets_no_ci.display_name);
}

function createAsset(asset){
  
  
  var ciClass = '';
		if ('leave_to_category' == asset.model.asset_tracking_strategy
			.toString())
		ciClass = asset.model_category.cmdb_ci_class.toString();

  gs.print("Class being used as reference table value is: " + ciClass");

  if (ciClass != '') {
    var ciSysId;
    var ci = new GlideRecord(ciClass);
    ci.initialize();
    ci.asset = asset.sys_id;
    ci.name = asset.model.name;
    // Populate manufacturer
    ci.manufacturer = asset.model.manufacturer;
   
    var sync = new AssetAndCISynchronizer();
    sync.syncRecordsWithoutUpdate(asset, ci, 'cmdb_ci', false);
   
    var assetCMDBUtil = new AssetCMDBUtil();
    if (asset.install_status != 2
      &amp;amp;&amp;amp; !gs.nil(asset.serial_number)
      &amp;amp;&amp;amp; assetCMDBUtil.isSerialNumberMandatory(asset.model_category)) {
      ci.name = asset.serial_number + ' - ' + asset.model.name;
      ciSysId = assetCMDBUtil.createCIUsingIRE(ci);
    } else {
      ciSysId = ci.insert();
    }
    asset.ci = ciSysId;
  }
}&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 24 Sep 2020 16:11:27 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/fix-script-to-create-ci-s-on-assets/m-p/1688089#M345015</guid>
      <dc:creator>Kieran Anson</dc:creator>
      <dc:date>2020-09-24T16:11:27Z</dc:date>
    </item>
    <item>
      <title>Re: Fix Script to Create CI's on Assets</title>
      <link>https://www.servicenow.com/community/developer-forum/fix-script-to-create-ci-s-on-assets/m-p/1688090#M345016</link>
      <description>&lt;P&gt;The log statement you added returned undefined. I'll keep on poking around at it to see where I get.&lt;/P&gt;
&lt;P&gt;Yash below had suggested adding sys_id when calling the function. This seemed to have fixed the error however the asset is not updated with a CI still.&lt;/P&gt;
&lt;PRE class="language-javascript"&gt;&lt;CODE&gt;*** Script: Class being used as reference table value is: 
*** Script: Adding CI to Asset: Microsoft Corporation Surface Laptop 3
[0:00:00.072] Total Time&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 24 Sep 2020 16:47:45 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/fix-script-to-create-ci-s-on-assets/m-p/1688090#M345016</guid>
      <dc:creator>NFGDom</dc:creator>
      <dc:date>2020-09-24T16:47:45Z</dc:date>
    </item>
    <item>
      <title>Re: Fix Script to Create CI's on Assets</title>
      <link>https://www.servicenow.com/community/developer-forum/fix-script-to-create-ci-s-on-assets/m-p/1688091#M345017</link>
      <description>&lt;P&gt;so that's the issue, it doesn't know on what cmdb_ci table to create the CI's. This value should be stored in the model category and is what the following does. it dot walks to the value&lt;/P&gt;
&lt;PRE class="language-javascript"&gt;&lt;CODE&gt;ciClass = asset.model_category.cmdb_ci_class.toString();&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="find_real_file.png"&gt;&lt;img src="https://www.servicenow.com/community/image/serverpage/image-id/200733i37AA41BC448020EF/image-size/large?v=v2&amp;amp;px=999" role="button" title="find_real_file.png" alt="find_real_file.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 24 Sep 2020 16:53:00 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/fix-script-to-create-ci-s-on-assets/m-p/1688091#M345017</guid>
      <dc:creator>Kieran Anson</dc:creator>
      <dc:date>2020-09-24T16:53:00Z</dc:date>
    </item>
    <item>
      <title>Re: Fix Script to Create CI's on Assets</title>
      <link>https://www.servicenow.com/community/developer-forum/fix-script-to-create-ci-s-on-assets/m-p/1688092#M345018</link>
      <description>&lt;P&gt;I did not check the CreateCI() function yet.&lt;/P&gt;
&lt;P&gt;I had copied my query from from the table by clicking on the filter, I can change that to null.&lt;/P&gt;
&lt;P&gt;Adding the sys_id seems to have elevated some errors I was having but the CI is still not creating. I'll keep on poking at it.&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;
&lt;P&gt;Dom&lt;/P&gt;</description>
      <pubDate>Thu, 24 Sep 2020 16:54:43 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/fix-script-to-create-ci-s-on-assets/m-p/1688092#M345018</guid>
      <dc:creator>NFGDom</dc:creator>
      <dc:date>2020-09-24T16:54:43Z</dc:date>
    </item>
    <item>
      <title>Re: Fix Script to Create CI's on Assets</title>
      <link>https://www.servicenow.com/community/developer-forum/fix-script-to-create-ci-s-on-assets/m-p/1688093#M345019</link>
      <description>&lt;P&gt;Out of curiosity would having the model_category field as a list type and not reference type cause it not to find the cmdb_ci_class? This I believe was our original issue as to why we have to clean these assets up. We had&amp;nbsp;a change a while back (not by me!) that made the model category field a list type from the reference type and it broke a lot of things.&lt;/P&gt;
&lt;P&gt;I think making that change in our dev instance will probably get this working. I'll test this out and report back. Thank you again for all the help - leaning stuff every day still!&lt;/P&gt;
&lt;P&gt;Dom&lt;/P&gt;</description>
      <pubDate>Thu, 24 Sep 2020 17:09:52 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/fix-script-to-create-ci-s-on-assets/m-p/1688093#M345019</guid>
      <dc:creator>NFGDom</dc:creator>
      <dc:date>2020-09-24T17:09:52Z</dc:date>
    </item>
  </channel>
</rss>

