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

Ankur Bawiskar
Tera Patron
Tera Patron

@SHALIKAS 

why not apply reference qualifier to allow selecting only Non Retired Install Status?

Then no scripting and validation required.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

But the requirment is to display message also

@SHALIKAS 

I don't think it's a best UI experience to let users select a retired Asset and then show the info message

Instead apply simple reference qualifier and show a field message or help text that only Non-retired assets can be selected

If you still require then you can write onChange catalog client script + GlideAjax and check if any 1 asset is in retired status then show that message

Something like this but please enhance

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    g_form.hideFieldMsg('asset');

    var ga = new GlideAjax('AssetValidation');
    ga.addParam('sysparm_name', 'validateAsset');
    ga.addParam('sysparm_sys_id', newValue);
    ga.getXMLAnswer(function(response) {
        if (response == 'invalid') {
            g_form.showFieldMsg('asset', 'Asset selected is not correct. Only non-retired assets can be selected', 'error');
        }
    });
}

Client callable Script Include

var AssetValidation = Class.create();
AssetValidation.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    validateAsset: function() {
        var assetSysId = this.getParameter('sysparm_sys_id').toString();
        var assetGR = new GlideRecord('cmdb_ci_hardware');
        assetGR.addQuery('sys_class_name', 'IN', 'cmdb_ci_hardware,cmdb_ci_server');
        assetGR.addQuery('sys_id', 'IN', assetSysId);
        assetGR.addQuery('install_status', '7');
        assetGR.setLimit(1);
        assetGR.query();
        if (assetGR.hasNext())
            return 'invalid';
        else
            return 'valid';
    }
});

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

@SHALIKAS 

Thank you for marking my response as helpful.

As per new community feature you can mark multiple responses as correct.

If my response helped please mark it correct as well so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader