Return error statement from client script

SHALIKAS
Tera Guru

I have a list collector variable called asset which refers to the hardware table. My requirment is that whenever any value selected which is of class hardware or server and has install status retired, then a message should be displayed in the form saying Value selected is not correct. How to do this

1 ACCEPTED SOLUTION

GopikaP
Mega Sage

Hi @SHALIKAS , You can create a onChange client script on asset field - 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    //Type appropriate comment here, and begin script below
    var assets = g_form.getValue('u_asset');
    var ga = new GlideAjax('ScriptIncludeName');
    ga.addParam('sysparm_name', 'functionName');
    ga.addParam('sysparm_assetIds', assets);
    ga.getXML(getInfo);

    function getInfo(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        if (answer != 'false')
            g_form.addErrorMessage(answer);
    }
}

and then create a client callable script include - 

var ScriptIncludeName = Class.create();
ScriptIncludeName.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    functionName: function() {
        var information;
        var asset = [];
        var assets = this.getParameter('sysparm_assetIds');
        var agg = new GlideRecord('cmdb_ci_hardware');
        agg.addQuery('sys_id', 'IN', assets);
        agg.addEncodedQuery('sys_class_name=cmdb_ci_server^ORsys_class_name=cmdb_ci_hardware^install_status=7');
        agg.query();
        while (agg.next()) {
            asset.push(agg.getValue('name'));
        }
        if (asset) {
            information = 'Following assets are having install status retired.Please correct your selections-\n ' + asset;
            return information;
        } else
            return false;

    },
    type: 'ScriptIncludeName'
});

 

View solution in original post

6 REPLIES 6

GopikaP
Mega Sage

Hi @SHALIKAS , You can create a onChange client script on asset field - 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    //Type appropriate comment here, and begin script below
    var assets = g_form.getValue('u_asset');
    var ga = new GlideAjax('ScriptIncludeName');
    ga.addParam('sysparm_name', 'functionName');
    ga.addParam('sysparm_assetIds', assets);
    ga.getXML(getInfo);

    function getInfo(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        if (answer != 'false')
            g_form.addErrorMessage(answer);
    }
}

and then create a client callable script include - 

var ScriptIncludeName = Class.create();
ScriptIncludeName.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    functionName: function() {
        var information;
        var asset = [];
        var assets = this.getParameter('sysparm_assetIds');
        var agg = new GlideRecord('cmdb_ci_hardware');
        agg.addQuery('sys_id', 'IN', assets);
        agg.addEncodedQuery('sys_class_name=cmdb_ci_server^ORsys_class_name=cmdb_ci_hardware^install_status=7');
        agg.query();
        while (agg.next()) {
            asset.push(agg.getValue('name'));
        }
        if (asset) {
            information = 'Following assets are having install status retired.Please correct your selections-\n ' + asset;
            return information;
        } else
            return false;

    },
    type: 'ScriptIncludeName'
});

 

Murtaza Saify
Tera Contributor

Here’s how you can implement this:


Steps to Implement the Validation

1. Identify the List Collector Variable

  • Ensure you have a list collector variable (e.g., asset) that refers to the alm_hardware table.

2. Create a Client Script

  • Use a Client Script to validate the selected assets when the form is submitted or when the selection changes.

3. Write the Client Script

  • The script will:

    1. Get the selected values from the list collector.

    2. Query the alm_hardware table to check the class and install status of each selected asset.

    3. Display a message if any selected asset is invalid.


Example Client Script

Here’s an example of a Client Script to achieve this:

javascript
Copy
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    // Get the list collector variable
    var assetListCollector = g_form.getControl('asset'); // Replace 'asset' with the actual variable name

    // Get the selected assets
    var selectedAssets = g_form.getListCollectorValues('asset'); // Replace 'asset' with the actual variable name

    // Flag to track if any invalid asset is selected
    var invalidAssetFound = false;

    // Loop through the selected assets
    selectedAssets.forEach(function(assetSysID) {
        // Query the hardware table
        var hardwareGR = new GlideRecord('alm_hardware');
        if (hardwareGR.get(assetSysID)) {
            // Check if the asset is of class 'hardware' or 'server' and has install status 'retired'
            if ((hardwareGR.getValue('sys_class_name') == 'hardware' || hardwareGR.getValue('sys_class_name') == 'server') &&
                hardwareGR.getValue('install_status') == 'retired') {
                invalidAssetFound = true;
            }
        }
    });

    // Display a message if an invalid asset is found
    if (invalidAssetFound) {
        g_form.addErrorMessage('Value selected is not correct. Please select assets that are not retired.');
    } else {
        g_form.clearMessages();
    }
}