updates with createOrUpdateCI versus glide record

tenagra
Kilo Expert

I'm working on a Script Include that will update cmdb ci records with specific information based.  This will be done via a schedule job and some discovery business rules.  

My original thinking was to use something similar to the code below so I could handle everything in the Script Includes.  However, when testing the code below, if I call getRecord and then getNewRecord, getNewRecord doesn't actually return a value.  The opposite is true of I run it in reverse.  I assume this is because of the While (next()) loop that is being run (it not, please correct me).  A getValue() wouldn't return a value.

While I could likely work around the issue, a thought struck me about using the createOrUpdateCI() API.  This would allow me to manage a JSON object, rather than a glide record object, bypassing the while(next()) issue  However, I know it would engage the identification engine, which I don't see as a major concern, but I could be way off on that assumption.

So, I wanted to pose the question to the community about any pro/cons of using a GlideRecord versus using a createOrUpdateCI() and/or if there are some best practices around it.  All input is welcome.

var CIGRTest = Class.create();
CIGRTest.prototype = {
    initialize: function(sysid) 
	{
		this.gr = new GlideRecord('cmdb_ci');
		this.gr.addQuery('sys_id', sysid);
		this.gr.query();
		gs.print('Record Count = ' + this.gr.getRowCount());
    },
	getRecord: function()
	{
		gs.print('--- Staring getRecord()------');
		while (this.gr.next())
			{var name = this.gr.getValue('name');gs.print('CI Name = ' + name);	}
	},
	getNewRecord: function()
	{
		gs.print('--- Staring getNewRecord()------');
		while (this.gr.next())
			{var name = this.gr.getValue('name'); gs.print('CI Name = ' + name);}
	},
    type: 'CIGRTest'
};

 

 

Thanks,

Nathan

7 REPLIES 7

SanjivMeher
Kilo Patron
Kilo Patron

What is the difference between getRecord and getNewRecord? Why not initialize the object in the function itself as below?

 

var CIGRTest = Class.create();
CIGRTest.prototype = {
    initialize: function() 
	{
    },
	getRecord: function(sysid)
	{
		this.gr = new GlideRecord('cmdb_ci');
		this.gr.addQuery('sys_id', sysid);
		this.gr.query();
		gs.print('Record Count = ' + this.gr.getRowCount());
		gs.print('--- Staring getRecord()------');
		while (this.gr.next())
			{var name = this.gr.getValue('name');gs.print('CI Name = ' + name);	}
	},
	getNewRecord: function(sysid)
	{
		this.gr = new GlideRecord('cmdb_ci');
		this.gr.addQuery('sys_id', sysid);
		this.gr.query();
		gs.print('--- Staring getNewRecord()------');
		while (this.gr.next())
			{var name = this.gr.getValue('name'); gs.print('CI Name = ' + name);}
	},
    type: 'CIGRTest'
};

Please mark this response as correct or helpful if it assisted you with your question.

For this specific test case, there isn't any difference, I was just trying to see if the record would be available in multiple methods.  When I actually start creating the Script Includes, I estimate approximately 9-12 methods, all referencing the same GR record for various things.  (i.e. setting location, build new relationships, setting some custom fields, setting support group, etc.).  Ideally, I only query the glide record once and then one update after everything is done.  So something like this:

 

var info = new CIGRTest(sysid);
info.updateLocation(location);
info.setSupportGroup(supportGroup);
info.setCustomVal1(val1);
info.setCustomVal2(val2);
info.updateCI();

 

 

However, as stated in my original post, I am wondering why use glide records at all.  Is there a benefit to using glide records over using the cmdb API createOrUpdateCI(), which would update the same CMDB CI Record?  Is one better than the other, and if so, why?  Obviously, for things outside the CMDB, glide requires are required, but are they the preferred method for updating cmdb records?

Sincerely,

Nathan

I didnt find much documentation for createOrUpdateCI(). But what I understood is, I can pass a JSON payload to it and update parameters.

find_real_file.png

 

But I would use GlideRecord, since it is simple and I can query the desired records and update it as needed.

Also in the below script I would put the gr.next in the initialize in the initialize function. Because I only need to query the record once. and then get/set values using different functions.

var CIGRTest = Class.create();
CIGRTest.prototype = {
    initialize: function(sysid) 
	{
		this.gr = new GlideRecord('cmdb_ci');
		this.gr.addQuery('sys_id', sysid);
		this.gr.query();
                this.gr.next();
		gs.print('Record Count = ' + this.gr.getRowCount());
    },
	getRecord: function()
	{
		gs.print('--- Staring getRecord()------');
		var name = this.gr.getValue('name');gs.print('CI Name = ' + name);	
	},
	getNewRecord: function()
	{
		gs.print('--- Staring getNewRecord()------');
		var name = this.gr.getValue('name'); gs.print('CI Name = ' + name);
	},
    type: 'CIGRTest'
};

Please mark this response as correct or helpful if it assisted you with your question.

Doing some research over the weekend, I found that createOrUpdateCI() shoudn't be used in this manner.  What it really comes down to is that you have to specify a discovery_source when using the method.  Rather defeats the purpose of it.

So, glide records it is.

Nathan