GR Query for Operational Status

Rowella Montell
Tera Contributor

Hi All,

Good Day!

I was asked to the below requirement and as my task doesn't mainly include scripting I'm trying to process what is being asked here:

  • Create a new function called ChangeManagementUtil called "isContainsRetiredCIs" which accepts the change request as the parameter:
    • This will be query the task_ci table and returns false if the CI's operational status is Retired, otherwise return true.

I have done the below script but it doesn't seem to be working and I'm not sure if I'm on the right path to achieve the above requirement.

isContainsRetiredCIs: function(change_request) {
	var taskGR = new GlideRecord('task_ci');
		taskGR.addQuery('operational_status');
		taskGR.query();
		while (taskGR.next()) {
			if(taskGR.operational_status != 'Retired'){
                           taskGR.isValidRecord();
};
},

 

Also can you please advise as to how I can test it using the background script app? Since currently what I do is I save this script include update and go to the change_request table > Add an Affected CIs to see if it's working. The goal here is to make sure the Retires CIs and affected CIs can't be selected in a change request. Appreciate your asistance on this!

1 ACCEPTED SOLUTION

Hi,

Value of Retired is 6. So, in your code you can query whether Operational status is not 6.

isContainsRetiredCIs: function(change_request) {
	var taskGR = new GlideRecord('task_ci');
	taskGR.addQuery('task',change_request); // Use this line if change_request has sys_id. If it has Change Number then comment this line and uncomment next line
	//taskGR.addQuery('task.number',change_request); 
	taskGR.addEncodedQuery('ci_item.operational_status!=6'); // Check if operational status is not retired
	taskGR.query();
	return taskGR.hasNext(); // Return true if record returned. Otherwise return false
};
},
Thank you,
Palani

View solution in original post

4 REPLIES 4

palanikumar
Mega Sage

Hi,

You code should be like this:

Note: Read the comment in line 3 and update code accordingly

isContainsRetiredCIs: function(change_request) {
	var taskGR = new GlideRecord('task_ci');
	taskGR.addQuery('task',change_request); // Use this line if change_request has sys_id. If it has Change Number then comment this line and uncomment next line
	//taskGR.addQuery('task.number',change_request); 
	taskGR.addQuery('ci_item.operational_status','1'); // Check if operational status is Operational 
	taskGR.query();
	return taskGR.hasNext(); // Return true if record returned. Otherwise return false
};
},
Thank you,
Palani

Hi @palanikumar ,

Thank you for your help, I just have a question as per my requirement it has to return true if the operational status is operational, non-operational, ready etc. (all status except retired) and should return false if the operational status is Retired. How should I achieve this?

This would do that...

function change_request) {
	return new global.GlideQuery('task_ci')
	.getBy({
		'task': change_request,
		'ci_item.operational_status': 6
	}).isEmpty();
},

This also returns true if no task_ci record is found for that change_request, if that is not the desired behaviour it will need a tweak.

Hi,

Value of Retired is 6. So, in your code you can query whether Operational status is not 6.

isContainsRetiredCIs: function(change_request) {
	var taskGR = new GlideRecord('task_ci');
	taskGR.addQuery('task',change_request); // Use this line if change_request has sys_id. If it has Change Number then comment this line and uncomment next line
	//taskGR.addQuery('task.number',change_request); 
	taskGR.addEncodedQuery('ci_item.operational_status!=6'); // Check if operational status is not retired
	taskGR.query();
	return taskGR.hasNext(); // Return true if record returned. Otherwise return false
};
},
Thank you,
Palani