How to Remove the values in listcollector variable
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-21-2024 07:11 AM
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:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-21-2024 12:25 PM
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'
});