How to Remove the values in listcollector variable

Suvedha Vignesh
Tera Contributor

Hello Everyone,

 

I have a scenario, in which there is a field 'service offering ' .If service offering is selected CI related to Service offering field in Field A. My requirement is if user deletes the CI in field B, it check whether that CI has more than one relationship. If not error message should throw.

I have tried in array method and also the below method

I am able to fetch the details but,

Client script:

 

function onChange(control, oldValue, newValue, isLoading) {
    var currentArray = g_form.getValue("so_ri_cis_current").toString().split(','); //hidden field
    var proposedArray = newValue.toString().split(',');
    var preU = currentArray.length;
    var newU = proposedArray.length;

   var difference = currentArray.filter(checkRemoved);
   function checkRemoved(x) {
        return !proposedArray.includes(x);
    }
    if (g_form.getValue("so_ri_service_offering") != "") {
        if (preU > newU || preU == newU) {
            //alert(preU +":"+newU);
            var ga = new GlideAjax('CBACMDBCatalogUtilsAjax');
            ga.addParam('sysparm_name', 'getRelationInfo');
            ga.addParam("sysparm_element", difference);
            ga.getXMLAnswer(returnCodes)
    
    function returnCodes(response) {
        var answer = response;
        if (answer == 'true' )       
            g_form.showErrorBox('so_ri_cis', 'cannot delete because it has only one relationship');
            g_form.setVisible('configuration_item', true);
            g_form.setMandatory('configuration_item', true);
            // tlist = answer.split(",");
        }
    }
}
Script include:
 
 var childconfig = this.getParameter("sysparm_element");
    gs.log('child' + childconfig);
    var rel = new GlideAggregate('cmdb_rel_ci');
    rel.addEncodedQuery('parent.sys_class_name=cmdb_ci_service_by_tags^ORparent.sys_class_name=cmdb_ci_query_based_service^child.sys_idIN'+childconfig);
    rel.addAggregate("COUNT");
    rel.groupBy("child.sys_id");
    rel.query();
    while(rel.next()) {
        var count = rel.getAggregate("COUNT");
        gs.log("countnumsuv"+count);
       
        if (count == 1) {

            return true;
    }
    else{
          return false;
    }
    }
},
 
Now this is working only for sys_id. For eg in query we have given as 'sys_id is one of' , if we remove 2 value, then count is not showing correctly. is there a way we can take the latest removed one or any other way to get the count of  ci.
Thanks in advance,
Suvedha.
1 REPLY 1

VarunS
Kilo Sage

@Suvedha Vignesh 

Update the client script to handle multiple removed values properly and call the server-side script include for each removed CI.

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading) {
        return;
    }
    
    var currentArray = g_form.getValue("so_ri_cis_current").toString().split(','); // hidden field
    var proposedArray = newValue.toString().split(',');

    var removedCIs = currentArray.filter(function(ci) {
        return !proposedArray.includes(ci);
    });

    if (g_form.getValue("so_ri_service_offering") !== "" && removedCIs.length > 0) {
        removedCIs.forEach(function(ci) {
            var ga = new GlideAjax('CBACMDBCatalogUtilsAjax');
            ga.addParam('sysparm_name', 'getRelationInfo');
            ga.addParam("sysparm_element", ci);
            ga.getXMLAnswer(returnCodes);
        });
    }

    function returnCodes(response) {
        var answer = response;
        if (answer == 'true') {
            g_form.showErrorBox('so_ri_cis', 'Cannot delete because it has only one relationship');
            g_form.setValue('so_ri_cis_current', currentArray.join(',')); // Revert the value to prevent deletion
            g_form.setValue('so_ri_cis', currentArray.join(',')); // Revert the value to prevent deletion
        }
    }
}

 

Update the script include to handle a single CI and return whether it has more than one relationship.

 

var CBACMDBCatalogUtilsAjax = Class.create();
CBACMDBCatalogUtilsAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getRelationInfo: function() {
        var childConfig = this.getParameter("sysparm_element");
        var rel = new GlideAggregate('cmdb_rel_ci');
        rel.addEncodedQuery('parent.sys_class_name=cmdb_ci_service_by_tags^ORparent.sys_class_name=cmdb_ci_query_based_service^child.sys_id=' + childConfig);
        rel.addAggregate("COUNT");
        rel.groupBy("child.sys_id");
        rel.query();

        while (rel.next()) {
            var count = rel.getAggregate("COUNT");
            if (count == 1) {
                return 'true';
            }
        }
        return 'false';
    },
    
    type: 'CBACMDBCatalogUtilsAjax'
});