GlideAJAX not working for affected CIs list

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-28-2024 06:39 AM
Hello
I have the following need to have a UI Action on the related list "Affected CIs" to create one change request per selected CI.
I tried to do so with a mix between a UI Script and a GlideAjax, but GlideAjax gives me a Null answer.
Can you help me out please ?
Script Include :
var CIRetriever = Class.create();
CIRetriever.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getCiItems: function() {
var sysIds = this.getParameter('sysparm_sys_ids'); // Expecting a comma-separated list of sys_ids
var answer = [];
// Debugging: log the sysIds received
gs.info('Received sysparm_sys_ids: ' + sysIds);
if (!sysIds) {
gs.info('No sys_ids provided');
return JSON.stringify(answer); // Return empty list
}
// Split sysIds into an array
var sysIdList = sysIds.split(',');
// Debugging: log the sysIdList array
gs.info('sysIdList array: ' + sysIdList.join(','));
// Ensure sysIdList is not empty
if (sysIdList.length === 0) {
gs.info('sysIdList is empty after split');
return JSON.stringify(answer);
}
var taskCiSysId = new GlideRecord('task_ci');
taskCiSysId.addQuery('sys_id', 'IN', sysIdList); // Use 'IN' operator for list of sys_ids
taskCiSysId.query();
// Check if the query returns any results
if (!taskCiSysId.hasNext()) {
gs.info('No records found for the provided sys_ids');
}
while (taskCiSysId.next()) {
gs.info('Found CI Item: ' + taskCiSysId.ci_item.sys_id); // Debugging
answer.push(taskCiSysId.ci_item); // Collect results
}
gs.info('Retrieved CI Items: ' + JSON.stringify(answer)); // Debugging
return JSON.stringify(answer); // Return as JSON string
},
type: 'CIRetriever'
});
UI Script :
function processInputs() {
// Validate the change type selection
var changeType = gel("change_selection").value;
if (changeType == '') {
alert('Change Selection cannot be left blank.');
return; // Exit if no selection
}
// Collect the selected "Affected CI" record sys_ids from the related list
var selectedTaskCIs = g_list.getChecked(); // These are the sys_ids from the task_ci table
var selectedTaskCiSplit = selectedTaskCIs.split(',');
// Log the selected task_ci sys_ids for debugging
//console.log('Selected Task CI sys_ids:', selectedTaskCIs);
// Ensure you have valid selections
if (!selectedTaskCIs || selectedTaskCIs.length === 0) {
alert('No CIs selected.');
return; // Exit if no CIs are selected
}
// Assuming selectedTaskCiSplit is an array of sys_ids
//var sysIdList = selectedTaskCiSplit.join(','); // Convert array to comma-separated list
// Assuming selectedTaskCiSplit is an array of sys_ids
var sysIdList = selectedTaskCiSplit.join(','); // Convert array to comma-separated list
var ciToProcess = new GlideAjax('CIRetriever');
alert('List of CIs to Proceed : ' + sysIdList);
ciToProcess.addParam('sysparm_sys_ids', sysIdList);
ciToProcess.getXMLAnswer(function(response) {
var resultList = JSON.parse(response); // Parse the JSON response
console.log('AJAX call completed'); // Log after AJAX call
console.log('AJAX Response:', response); // Log the response to check what is received
alert('Response from AJAX : ' , response);
alert('Retrieved CI Items: ', resultList); // Debugging
if (resultList && resultList.length > 0) {
// Process each CI
for (var i = 0; i < resultList.length; i++) {
createChangeRequest(resultList[i], changeType);
alert('Processed the following CI: ' + resultList[i]);
}
} else {
alert('No CIs retrieved or failed to retrieve CIs.');
}
});
// Fetch the actual CI sys_ids using GlideAjax
//for (var i = 0; i < selectedTaskCIs.length; i++) {
//var taskCiSysId = selectedTaskCIs[i]; // This is the sys_id from the task_ci table
//g_form.addInfoMessage('ID processed : ' + selectedTaskCIs[i]);
/*var retrieveCi = new GlideRecord('task_ci');
retrieveCi.addEncodedQuery('sys_idIN' + selectedTaskCIs);
retrieveCi.query();
while (retrieveCi.next()) {
var ciSysId = retreiveCi.ci_item;
createChangeRequest(ciSysId, changeType);
g_form.addInfoMessage('Processed the following CI : ' + ciSysId);
}*/
// }
/*// Use GlideAjax to fetch the actual CI sys_id (ci_item)
var ajax = new GlideAjax("get_ci_sys_id");
ajax.addParam("sysparm_name", "get_ci_sys_id");
ajax.addParam("sysparm_task_ci_id", taskCiSysId);
ajax.addParam("sysparm_type", changeType);
ajax.getXMLAnswer(function(response) {
var ciSysId = response;
if (ciSysId && ciSysId.length === 32) {
createChangeRequest(ciSysId, changeType);
} else {
//alert('Failed to retrieve CI sys_id for Task CI: ' + taskCiSysId);
}
});*/
GlideDialogWindow.get().destroy(); // Close the dialog window after processing
//g_form.addInfoMessage('Splitted value : ' + selectedTaskCiSplit);
}
// Function to create a change request for a given CI sys_id
function createChangeRequest(ciSysId, changeType) {
var ajax = new GlideAjax("create_new_change_request");
ajax.addParam("sysparm_name", "change_request");
ajax.addParam("sysparm_ci_id", ciSysId);
ajax.addParam("sysparm_type", changeType);
ajax.getXMLAnswer(function(response) {
var answer = response;
});
}
function processCancel() {
GlideDialogWindow.get().destroy(); // Close the dialog window
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-29-2024 05:37 AM
Since the response from the server is comma-separated list of sys_ids, ciItems is a string, so your for loop is invalid. Ideally you would only want one trip to the server and back, so if this Script Include function getCiItems is not used elsewhere, add to it the change GlideRecord from your second Script Include within a for loop instead of joining the answer array and returning it. You can then return the list of CIs and/or the new change sys_ids or numbers for your alert.
If you must keep the Script Includes separate, still pass in ciItems as a string, then split it in the Script Include function and run the change GlideRecord in a for loop so there's only one additional trip to the server and back.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-29-2024 01:47 AM
I tried this, but returns Retrieved CI Items : []
var sysIdList = selectedTaskCiSplit.join(','); // Convert array to comma-separated list
var ciToProcess = new GlideAjax('CIRetriever');
ciToProcess.addParam('sysparm_name', 'getCiItems');
ciToProcess.addParam('sys_ids', sysIdList);
alert('CI List : ' + sysIdList);
ciToProcess.getXMLAnswer(function(response) {
var ciItems = response;
alert('Retrieved CI Items : ' + ciItems);
});