Modifying the filter on the "Add" AffectedCI ui action

Jalen
Tera Contributor

Hello - I am attempting to filter the popup that appears on the "add" ui action on the AffectedCI Related List at the bottom of incidents. The filter I want is it to only show CIs that are active. No CIs that are in the "gone" state. 

 

I've been trying to modify the script include that this ui action is tied to - but it just breaks the ui action. Please advise on what needs to happen. 

 

UI Action: Add

function openCmdbCIList(){
	var gajax = new GlideAjax("AssociateCIToTask");
	gajax.addParam("sysparm_name","getURL");
	gajax.addParam("sysparm_id", g_form.getUniqueValue());
	gajax.addParam("sysparm_add_to", "task_ci");

	gajax.getXMLAnswer(openList);
}

function openList(url){
	var cmdbciModal = new GlideModal('task_add_affected_cis');
	cmdbciModal.setTitle(getMessage("Add Affected CIs"));
	cmdbciModal.setWidth(1200);
	cmdbciModal.setAutoFullHeight(true);
	cmdbciModal.on('beforeclose', function(){
		refreshAffectedCIs();
	});
	ScriptLoader.getScripts('/scripts/incident/glide_modal_accessibility.js', function() {
		cmdbciModal.template = glideModalTemplate;
		cmdbciModal.renderIframe(url, function(event) {
				glideModalKeyDownHandler(event, cmdbciModal.getID());
			});
	});
	var link = document.createElement('link');
	link.rel = 'stylesheet';
	link.href = 'styles/incident_glide_modal.css';
	document.head.appendChild(link);
}

function refreshAffectedCIs() {
	var listId = g_form.getTableName() + ".task_ci.task";

	var list = typeof GlideList2 !== "undefined" ? GlideList2.getByName(listId) : null;
	if (list == null)
		list = typeof GlideList !== "undefined" ? GlideList.get(listId) : null;

	if (list != null)
		list.refresh();
	var modal = window.GlideModal.prototype.get('task_add_affected_cis');
	if (modal && modal.message)
		g_form.addInfoMessage(modal.message);
}

function resizeIframe(){
    var x = g_glideBoxes.cm_add_affected_cis;
	x.autoDimension();
    x.autoPosition();
	x._createIframeShim();
}

 

Script Include: AssociateCIToTask

var AssociateCIToTask = Class.create();
AssociateCIToTask.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	SESSION_KEY: 'com.snc.change_request.AssociateCIToTask.ci_user_filter',

	ajaxFunction_getURL: function(){
		var chgReqId = this.getParameter("sysparm_id");
		var addToTable = this.getParameter("sysparm_add_to") + "";
		return this._getURL(chgReqId, addToTable);
	},

	_getURL: function(chgReqId, addToTable){
		if (!new GlideRecord('task_ci').canCreate())
			return;
		this.removeUserFilter();

		var latestClassAdded;
		var parentClass = "";
		var principalClassFilter = "";
		// latestClassAdded logic
		// 1. if there are ci's, get the latest class of the ci
		// 2. default the latest class to parent class
		if (addToTable === "task_ci") {
			principalClassFilter = this.getPrincipalClassFilter(chgReqId);
			if (!principalClassFilter)
				latestClassAdded = this.getLatestClass(chgReqId);
			parentClass = this.getParentClass(chgReqId);
			if (!latestClassAdded)
				latestClassAdded = parentClass;
		}
		else if (addToTable === "task_service_offering")
			latestClassAdded = "service_offering";
		else
			latestClassAdded = "cmdb_ci";

		var url = new GlideURL("task_add_affected_cis.do");
		url.set("sysparm_crSysId", chgReqId);
		url.set("sysparm_view", "associate_ci");
		url.set("sysparm_add_to", addToTable);
		url.set("sysparm_stack", "no");
		url.set("sysparm_table", latestClassAdded);
		url.set("sysparm_parent_class", parentClass);
		url.set("sysparm_fixed_query", principalClassFilter);
		url.set("sysparm_operational_status", 7);
		return new GlideTinyURL().createTinyURL(url);
	},

	getQueryFilter: function(chgReqId, addToTable) {
		var principalClassFilter = "";
		if (addToTable === "task_ci")
			principalClassFilter = this.getPrincipalClassFilter(chgReqId);
		return principalClassFilter;
	},

	/*
 	* If "Best Practice - Bulk CI Changes" plugin is active and task type is change_requests
 	*     parent class = ci_class populated on the change_request
 	*     if --None- is selected for ci_class, then cmdb_ci is returned
 	* else
 	*     parent class = cmdb_ci
 	**/
	getParentClass: function(chgReqId) {
		if (GlidePluginManager.isActive("com.snc.bestpractice.bulkchange") || GlidePluginManager.isActive("com.snc.change_management.mass_update_ci")) {
			var chgReqGR = new GlideRecordSecure("change_request");
			if (chgReqGR.get(chgReqId) && !chgReqGR.ci_class.nil()){
				if ((chgReqGR.isValidField("is_bulk") && chgReqGR.getValue("is_bulk") === "1") || !chgReqGR.isValidField("is_bulk")) {
					return chgReqGR.ci_class;
				}
			}
		}
		return "cmdb_ci";
	},

	getPrincipalClassFilter: function(chgReqId) {
		if (!chgReqId)
			return;
		var taskGr = new GlideRecord("task");
		taskGr.get(chgReqId);
		if (!taskGr.isValidRecord()) {
			gs.error("Invalid task record sys-id passed");
			return;
		}
		return new TaskUtils().getPCFilterEvaluated(taskGr.sys_class_name + '');
	},
	storeUserFilter: function(){
		var filter;
		filter = this.getParameter("sysparm_filter_query");
		gs.getSession().putClientData(this.SESSION_KEY, filter);
		return;
	},

	removeUserFilter: function(){
		gs.getSession().clearClientData(this.SESSION_KEY);
		return;
	},
	
	_isValidTask: function(id) {
		var gr = new GlideRecord('task');
		return gr.get(id) && gr.active;
	},

	addSelected: function() {
		var id = this.getParameter("sysparm_id");
		if(!this._isValidTask(id)){
			gs.error("Unable to Add CI item(s) for an inactive/invalid task {0}", id);
			return;
		}
		var addToTable = this.getParameter("sysparm_add_to_table") + "";
		var selCIsList = this.getParameter("sysparm_selCIs");

		if (!selCIsList)
			return;

		var affectedCI = new GlideRecord(addToTable);
		if (affectedCI.canCreate()) {
			var selCIs = selCIsList.split(",");

			var existingCI = new GlideRecord(addToTable);
			var col = this.getCIField(addToTable);
			existingCI.addQuery(col, "IN", selCIsList);
			existingCI.addQuery("task", id);
			existingCI.addActiveQuery(); // STRY0362599 Dixon Make sure all CIs are active
			existingCI.query();
			var addedCIs = [];
			while (existingCI.next())
				addedCIs.push(existingCI.getValue(col) + '');

			for (var i = 0; i < selCIs.length; i++) {
				if (addedCIs.indexOf(selCIs[i] + '') == -1) {
					affectedCI.initialize();
					affectedCI.task = id;
					affectedCI.setValue(col, selCIs[i]);
					affectedCI.insert();
				}
			}
		}
	},

	getLatestClass: function(chgReqId){
		var affectedCiGR = new GlideRecordSecure("task_ci");
		affectedCiGR.addQuery("task", chgReqId);
		affectedCiGR.orderByDesc("sys_updated_on");
		affectedCiGR.setLimit(1);
		affectedCiGR.query();
		if (affectedCiGR.next())
			return affectedCiGR.ci_item.sys_class_name;
		return "";
	},
	
	getCIField: function(table) {
		if ("task_ci" === table)
			return "ci_item";
		else if ("task_service_offering" === table)
			return "service_offering";
		return "cmdb_ci_service";
	},

	addAll: function() {
		var addToTable = this.getParameter("sysparm_add_to_table") + "";
		var id = this.getParameter("sysparm_id");
		if(!this._isValidTask(id)){
			gs.error("Unable to Add CI item(s) for an inactive/invalid task {0}", id);
			return;
		}
		var affectedCI = new GlideRecord(addToTable);
		if (affectedCI.canCreate()) {
			var fixedQuery = this.getParameter("sysparm_fixed_query");
			var query = this.getParameter("sysparm_query");
			var listTableName = this.getParameter("sysparm_tableName");

			var result = [];
			var existingCI = new GlideRecord(addToTable);
			existingCI.addQuery("task", id);
			existingCI.addActiveQuery(); // STRY0362599 Dixon Make sure all CIs are active
			existingCI.query();
			var col = this.getCIField(addToTable);
			while (existingCI.next())
				result.push(existingCI.getValue(col) + '');

			var affectedCiGR = new GlideRecordSecure(listTableName);
			affectedCiGR.addEncodedQuery(fixedQuery);
			affectedCiGR.addEncodedQuery(query);
			affectedCiGR.query();
			while (affectedCiGR.next()) {
				//Check if the CI is already added to the list
				if (!result.contains(affectedCiGR.sys_id + '')) {
					affectedCI.initialize();
					affectedCI.task = id;
					affectedCI.setValue(col, affectedCiGR.sys_id);
					affectedCI.insert();
				}
			}
		}
	},

	getTotalNumberOfItems: function() {
		var query = this.getParameter("sysparm_query");
		var listTableName = this.getParameter("sysparm_table");
		var affectedCiGR = new GlideAggregate(listTableName);

		if (!affectedCiGR.canRead())
			return 0;

		affectedCiGR.addEncodedQuery(query);
		affectedCiGR.addAggregate('COUNT');
		affectedCiGR.query();
		if (affectedCiGR.next())
		   return affectedCiGR.getAggregate('COUNT') - 0;
		return 0;
	},

	isPublic: function() {
		return false;
	},

	type: "AssociateCIToTask"
});
1 ACCEPTED SOLUTION

Harsh Vardhan
Giga Patron

@Jalen You can define the filter in existing one, no need to add anther set parameter .

 

eg: I don't want to see retired ci so filter would be appear like this. 

 

url.set("sysparm_fixed_query", principalClassFilter+"^install_status!=6");

 

 

Thanks,

Harsh

View solution in original post

4 REPLIES 4

Harsh Vardhan
Giga Patron

@Jalen You can define the filter in existing one, no need to add anther set parameter .

 

eg: I don't want to see retired ci so filter would be appear like this. 

 

url.set("sysparm_fixed_query", principalClassFilter+"^install_status!=6");

 

 

Thanks,

Harsh

Thank you @Harsh Vardhan  for the response. After making the change, when I click the button, it appears as if the modal begins to materialize then the page refreshes and takes me to the pa_dashboard on the main page of SN. 

@Jalen Have you made any other changes in the script ? 

Because I had added filter query in script include and it is working and redirecting me on form. 

 

Thanks

Harsh

Hello @Harsh Vardhan! I have tried exactly what you said, but I cloned both UI Action and Script Include to not mess up with oob, but still nothing. On my case I need to prevent retired CIs to be selected for change_request.