Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Show error message when adding duplicate CI using add button in affected CI

Saranya Babu1
Tera Expert

Hello All,

@Ankur Bawiskar 

Affected CI related list in the Outage table. When user adding CI which is already part of any outage of that change request number. I want to display a message to stating "This CI is already added in another outage. Please remove the duplication".

 

I have updated the script include which is using the Add button but its not displaying in the form. This scripting is working and I am getting the logs.

 

 

Please suggest on this

 

On click on button ADD button list getting refreshing so how we can add a message

 

 

 addSelected: function() {
        gs.info("Check Duplicate ::inside this function");
        var id = this.getParameter("sysparm_id");
        var addToTable = this.getParameter("sysparm_add_to_table");
        var listTableName = this.getParameter("sysparm_tableName");
        var selCIsList = this.getParameter("sysparm_selCIs");
        var selCIs = selCIsList.split(",");

        var outageGr = new GlideRecordSecure(this.OUTAGE_TABLE);
        outageGr.get(id);
        var changeNumber = outageGr.getValue("task_number"); // Assuming "change_request" is the field with the change number.
        gs.info("Check Duplicate ::changeNumber" + changeNumber);
        for (var i = 0; i < selCIs.length; i++) {
            if (selCIs[i]) {
                // Check if the CI is already associated with another Outage with the same change number
                var existingCI = new GlideRecordSecure(addToTable);
                existingCI.addQuery("JOINcmdb_ci_outage.sys_id=cmdb_outage_ci_mtom.outage");
                existingCI.addQuery("ci_item", selCIs[i]);
                existingCI.query();

                var isDuplicate = false;

                // Loop through existing CI associations to check for same change number
                while (existingCI.next()) {
                    var relatedOutageGr = new GlideRecordSecure(this.OUTAGE_TABLE);
                    relatedOutageGr.get(existingCI.outage);
                    if (relatedOutageGr.getValue("task_number") === changeNumber) {
                        isDuplicate = true;
                        break;
                    }
                }

                if (isDuplicate) {
                    gs.info("Check Duplicate ::This is a duplicate CI for the same change number");
                    gs.addInfoMessage("This is a duplicate CI for the same change number.");
                }

                // If not a duplicate, associate the CI to the outage
                var affectedCI = new GlideRecordSecure(addToTable);
                affectedCI.initialize();
                affectedCI.outage = id;
                affectedCI.ci_item = selCIs[i];
                affectedCI.insert();
            }
        }
        return gs.getMessage("Added selected configuration items successfully to the outage.");
    

 

 

 

 

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

	gajax.getXMLAnswer(openList);
}

function openList(url){
	var cmdbciModal = new GlideModal('outage_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() + ".cmdb_outage_ci_mtom.outage";

	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();
}

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

 

0 REPLIES 0