Script include should show reference variable value from multirow variable set(in short desc SCTASK

Fotina Galieb
Tera Contributor

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!

 

 

var Device_Update_Task = Class.create();
Device_Update_Task.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
 
    returnShortDescription: function() {
 
        var deviceId = this.getParameter('sysparm_device_ids');
       

        // Create an array of the device IDs
        var deviceIdsArray = [deviceId];

        
        var sysparm_device_ids = deviceIdsArray.filter(function(id) {
            return id; /
        }).join(',');

        // Now use sysparm_device_ids in your GlideRecord query
        var queryDevice = new GlideRecord('list_of_devices');
        queryDevice.addQuery('sys_idIN', sysparm_device_ids);
        queryDevice.query();

        // Count the number of records
        var recordCount = queryDevice.getRowCount();

        // Handle multiple records
        if (recordCount > 1) {
            return 'multiple items';
        }
    },
});
1 ACCEPTED SOLUTION

Astik Thombare
Tera Sage

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

 

 

 

Now, in the case of the Script Include, I'm checking the device sys_ids in the cmdb_ci_comm table. In your case, it could be any table as per your needs. Please see the Script Include below: 
 
 

 

 

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.

 

AstikThombare_0-1729267502696.png

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

 

 

 

 

 

View solution in original post

1 REPLY 1

Astik Thombare
Tera Sage

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

 

 

 

Now, in the case of the Script Include, I'm checking the device sys_ids in the cmdb_ci_comm table. In your case, it could be any table as per your needs. Please see the Script Include below: 
 
 

 

 

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.

 

AstikThombare_0-1729267502696.png

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