List action UI action validation before executing on selected list

nikhil2810
Tera Contributor

I'm trying to create a UI action which works on list action and logic I'm trying to build is on click of UI action 'Mark for quarantine' on asset from list should mark the status to 'quarantine'(custom status in our environment). In order to do that I need to validate if selected assets are in state 'in use' and assigned to is populated.

I've managed to get the functionality of updating the asset status working but state & assign to validation is not working. Any leads on how can I enforce the validation and if user selects records that they shouldn't from list I need to display an error message for those records and process correct record.

3 REPLIES 3

harshav
Tera Guru

In the server script of the action write the code as below

var state = current.state;
var assigned_to = current.assigned_to;
if(state == "in use" && assigned_to){
   current.state = "quarantine";
   current.update();
}

Ankur Bawiskar
Tera Patron
Tera Patron

@nikhil2810 

unless you share your current script we cannot help

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

Hi Ankur,
Below is script , I got it working but it is inefficient cause of all the validation done on client side, need to understand if can be enhanced.

 

client callable = true,

On Click = markQuar()

Condition =current.install_status == '1' && current.assigned_to != ''

 

 

function markQuar() {
    var toQuar = [];
    var invalidRecs = [];
    var numSel = 0;
    var tblName = g_list.getTableName();
    var selSysIds = g_list.getChecked();
    var sysIDs = selSysIds.split(',');
    var lth = sysIDs.length;
    for (var i = 0; i < lth; i++) {
        //alert(sysIDs[i]);
        var a = new GlideRecord('alm_hardware');
        a.addQuery('sys_id', sysIDs[i]);
        a.query();
        if (a.next()) {

            if ((a.install_status == '1') && (a.assigned_to != '')) {
                toQuar.push(a.sys_id);
                //alert('Yes ' + a.display_name);
            } else {
                invalidRecs.push(a.display_name);
            }
        }
    }
    var message = '';
    if (invalidRecs.length > '0') {
        message = 'please confirm quarantine to be marked for ' + toQuar.length + ' assets.\n ' + 'Cannot quarantine ' + invalidRecs.length + ' asset, please check if status in use & assigned to :'; //+ invalidRecs.toString();
        for (var j = 0; j < invalidRecs.length; j++) {
            message += '\n' + invalidRecs[j].toString();
        }
    } else {
        message = 'please confirm quarantine to be marked for ' + toQuar.length;
    }
    var usrResponse = confirm(message);
    if (usrResponse == true) {
        var ga = new GlideAjax('TriggQuar');
        ga.addParam('sysparm_name', 'createQuarTask');
        ga.addParam('sysparm_entry_ids', toQuar.toString());
        ga.getXML(getDupTasks);
        // }

        function getDupTasks(response) {
            var answer = response.responseXML.documentElement.getAttribute("answer");
            if (answer) {
                alert('Qurantinemarke for ' + toQuar.length + 'Assets');
            }
        }

    } else {
        return false;
    }
}