The CreatorCon Call for Content is officially open! Get started here.

current.variable.changes() always retrurning true on sc_req_item table

kiran_gottipati
Tera Contributor

Hi Everyone,

 

I have a requiremet where I need to run some code when catalog item variables updated from the RITM UI form : 

 

current.variable.changes() always retrurning true on sc_req_item table Business Rule.(tried both Before and After Update BRs)

 

Could anyone please check and help me on this..

 

Thank's in advance

 

Regards,

Kiran

4 REPLIES 4

Brad Bowman
Kilo Patron
Kilo Patron

Hi Kiran,

Using the GlideRecordToObject function will work to compare current to previous like this:

(function executeRule(current, previous /*null when async*/) {
	var curvar = JSON.stringify(new GlideRecordToObject().toObject(current.variable_pool));
	var prevar = JSON.stringify(new GlideRecordToObject().toObject(previous.variable_pool))
	if (curvar != prevar) {
		gs.addInfoMessage('Variable(s) changed');
	} else {
		gs.addInfoMessage('Variable(s) not changed');
	}
})(current, previous);

Hi @Brad Bowman  ,

 

Thank you for the reply, but the code not working.. getting
"GlideRecordToObject" is not defined.

I used the same sippet you shared above..

And Also I noticied "current,variables.changes()" workin fine for normal vairables but only if I have MRVS (Multi ROw Variable Set) in the cat item > then it always returning true..

 

Regards,

Kiran

Sorry, that must be a custom SI we added.  Here it is, but I noticed that when I only update an MRVS row/variable, this is not detecting the change as MRVS are not included in variable_pool.

var GlideRecordToObject = Class.create();
GlideRecordToObject.prototype = {
	initialize: function() {

	},
	/**
 	* @summary toObject function returns a gliderecord as an object. This
	* is used to prevent saving the place in memory. 
	* https://community.servicenow.com/community/develop/blog/2015/09/24/community-code-snippets--gliderecord-to-object-array-conversion
 	* @Param {object} recordToPackage - The gliderecord object
 	* @return {object} JSON object that can be used. 
 	*/
	toObject : function(recordToPackage){
		var packageToSend = {}; 
		for (var property in recordToPackage) {
			try {
				if(property == 'work_notes' || property == 'comments'){
					packageToSend[property] = recordToPackage[property].getJournalEntry(1);
				} else {
					packageToSend[property] = recordToPackage[property].getDisplayValue();
				}
			}
			catch(err){}
		}
		return packageToSend;
	},
	
	type: 'GlideRecordToObject'
};

I can't think of a way to use instead of or with this to detect when an MRVS changes.

 

Hi @Brad Bowman  , 

 

Thank you for your input but I rather found a way myself to validate if any varable value are changed by comparing my previous and current values from Before BR with this below approach..

Please refer this OOB docs referring this script - https://docs.servicenow.com/en-US/bundle/tokyo-application-development/page/script/server-scripting/...

 

var current_variables = current.variables.getElements(true);
var previous_variables = previous.variables.getElements(true);

if(current_variables != previous_variables){
current.comments('variables changed');
}
else{
current.comments('variables not changed');
}

 

Once Again thank you for your input.

 

Regards,

Kiran