- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-18-2024 07:07 AM
Hello Guys,
I have an item and multirow variable set in it with reference to table which has list of devices.
My goal is to print nam of device in short description of the SCTASK in case 1 device is chosen.
In case more than 1 chosen, i need to have just text "several devices ordered"
I tried to do it via Script Include, but all the time I have result as undefined in my short description.
Could you please help me to understand where is my mistake and how to fix it?
Thank you!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-18-2024 09:06 AM
Hi @Fotina Galieb,
I have tried your scenario in my PDI and was able to get a proper response from the server side.
Let’s start with the client script. For example, I constructed an onLoad client script to pass some static sys_ids of devices as an array of objects, similar to how we would retrieve values from multi-row variable sets (MRVs). In your case, it would be a Catalog Client Script. Please see the client script below, and I was alerting the response just to check the output.
function onLoad() {
// Define static device IDs
var deviceIdsArray = [
'81f5d102531d121010af51a0a0490ee5',
'61065d8e53d9121010af51a0a0490edc'
];
// Convert the array to a comma-separated string
var deviceIdsString = deviceIdsArray.join(',');
// Create a GlideAjax object to call the Script Include
var ga = new GlideAjax('Device_Update_Task');
// Add parameters to the GlideAjax request
ga.addParam('sysparm_name', 'returnShortDescription'); // Server-side function
ga.addParam('sysparm_device_ids', deviceIdsString); // Pass the comma-separated string
// Make the call to the server-side Script Include
ga.getXML(updateShortDescription);
// Function to handle the server response
function updateShortDescription(response) {
// Get the 'answer' from the server response
var answer = response.responseXML.documentElement.getAttribute("answer");
// Display the result in an alert (for debugging purposes)
alert(answer);
}
}
var Device_Update_Task = Class.create();
Device_Update_Task.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
returnShortDescription: function() {
// Get the sysparm_device_ids parameter (comma-separated string of sys_ids)
var deviceIds = this.getParameter('sysparm_device_ids');
gs.info('Received device IDs: ' + deviceIds); // Log the received device IDs
if (!deviceIds) {
return 'No device selected';
}
// Split the device IDs into an array and trim spaces
var deviceIdsArray = deviceIds.split(',').map(function(id) {
return id.trim(); // Trim any spaces
});
gs.info('Device IDs Array: ' + JSON.stringify(deviceIdsArray)); // Log the device IDs array
// Query the device table based on the provided sys_ids
var queryDevice = new GlideRecord('cmdb_ci_comm'); //Change table Name as per your need
queryDevice.addQuery('sys_id', 'IN', deviceIdsArray);
queryDevice.query();
// Log the number of records found
var recordCount = queryDevice.getRowCount();
gs.info('Number of devices found: ' + recordCount);
if (recordCount > 1) {
return 'Several devices ordered';
} else if (recordCount === 1 && queryDevice.next()) {
// Only one device selected, return the device name
gs.info('Found device name: ' + queryDevice.name); // Log the device name found
return queryDevice.name; // Assuming 'name' is the field holding the device name
}
return 'No devices found';
},
type: 'Device_Update_Task'
});
Now, for the final outcome: I'm able to see the alert stating "Several devices ordered" since I'm passing two devices. In your case, you will be updating the catalog task description with this output. Please see the alert below that I received. I hope this will solve your issue.
If my reply helped with your issue please mark helpful 👍and correct ✔️if your issue is resolved.
By doing so you help other community members find resolved questions which may relate to an issue they're having
Thanks,
Astik
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-18-2024 09:06 AM
Hi @Fotina Galieb,
I have tried your scenario in my PDI and was able to get a proper response from the server side.
Let’s start with the client script. For example, I constructed an onLoad client script to pass some static sys_ids of devices as an array of objects, similar to how we would retrieve values from multi-row variable sets (MRVs). In your case, it would be a Catalog Client Script. Please see the client script below, and I was alerting the response just to check the output.
function onLoad() {
// Define static device IDs
var deviceIdsArray = [
'81f5d102531d121010af51a0a0490ee5',
'61065d8e53d9121010af51a0a0490edc'
];
// Convert the array to a comma-separated string
var deviceIdsString = deviceIdsArray.join(',');
// Create a GlideAjax object to call the Script Include
var ga = new GlideAjax('Device_Update_Task');
// Add parameters to the GlideAjax request
ga.addParam('sysparm_name', 'returnShortDescription'); // Server-side function
ga.addParam('sysparm_device_ids', deviceIdsString); // Pass the comma-separated string
// Make the call to the server-side Script Include
ga.getXML(updateShortDescription);
// Function to handle the server response
function updateShortDescription(response) {
// Get the 'answer' from the server response
var answer = response.responseXML.documentElement.getAttribute("answer");
// Display the result in an alert (for debugging purposes)
alert(answer);
}
}
var Device_Update_Task = Class.create();
Device_Update_Task.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
returnShortDescription: function() {
// Get the sysparm_device_ids parameter (comma-separated string of sys_ids)
var deviceIds = this.getParameter('sysparm_device_ids');
gs.info('Received device IDs: ' + deviceIds); // Log the received device IDs
if (!deviceIds) {
return 'No device selected';
}
// Split the device IDs into an array and trim spaces
var deviceIdsArray = deviceIds.split(',').map(function(id) {
return id.trim(); // Trim any spaces
});
gs.info('Device IDs Array: ' + JSON.stringify(deviceIdsArray)); // Log the device IDs array
// Query the device table based on the provided sys_ids
var queryDevice = new GlideRecord('cmdb_ci_comm'); //Change table Name as per your need
queryDevice.addQuery('sys_id', 'IN', deviceIdsArray);
queryDevice.query();
// Log the number of records found
var recordCount = queryDevice.getRowCount();
gs.info('Number of devices found: ' + recordCount);
if (recordCount > 1) {
return 'Several devices ordered';
} else if (recordCount === 1 && queryDevice.next()) {
// Only one device selected, return the device name
gs.info('Found device name: ' + queryDevice.name); // Log the device name found
return queryDevice.name; // Assuming 'name' is the field holding the device name
}
return 'No devices found';
},
type: 'Device_Update_Task'
});
Now, for the final outcome: I'm able to see the alert stating "Several devices ordered" since I'm passing two devices. In your case, you will be updating the catalog task description with this output. Please see the alert below that I received. I hope this will solve your issue.
If my reply helped with your issue please mark helpful 👍and correct ✔️if your issue is resolved.
By doing so you help other community members find resolved questions which may relate to an issue they're having
Thanks,
Astik