Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

UI action to update records in the List view

kuttti
Kilo Guru

Hello Community,

I am having a hard time in figuring out the solution for below scenario. Any help is greatly appreciated.

Requirement - UI action to update bulk records in list view.

(Existing functionality - Usually when an update is made to a record either in the list view or form view a BR is called from where flow designed is executed from which a status message is sent to BR and displayed to the user ex:"Update success" or "Update  failed").

Trying to achieve above thing when an update is made through list view in Bulk.

UI action created

function bulkUpdate() {
    
    var selectedRecords = g_list.getChecked().toString(); 
    var dialogClass = GlideModal ? GlideModal : GlideDialogWindow;
    var dialog = new dialogClass("IncidentBulkUpdate");
    dialog.setTitle(getMessage("Incident Bulk Edit"));
    dialog.setPreference("recordList", selectedRecords); //Pass the selected records into the dialog
    dialog.setPreference("queryList",g_list.getQuery());
    dialog.setPreference("totalRows",g_list.totalRows);
    dialog.setPreference("tableName",g_list.getTableName());
   dialog.setPreference("sysparm_nostack", true);
    //dialog.setWidth(500);
    dialog.render();

UI Page: Client Script:

//var sys_id = $j('#record_sys_id').val();
//alert(sys_id);
var modal = GlideModal.get();
var recordList = modal.getPreference('recordList');
//var recordQuery = modal.getPreference('queryList');
var totalRowCount = modal.getPreference('totalRows');
var tableName = modal.getPreference('tableName');
var records = "";
var selectedRecord = 0;
var maxCount = 100;
gel('relate_info_object').value = '';
$j('#grc_count_warn').show();
jQuery('#number_of_updates').show();
if (recordList != "" && recordList != undefined) {
    records = recordList.split(",");
    selectedRecord = records.length + '';
    rCount = selectedRecord;
}
enableOKButton();
getMessageUpdate(rCount, maxCount);

var table = gel("jvar_table_name");
table.value = tableName;

function recordSelectionChange() {
    if (jQuery('#all_filter').val() == 'condition') { //if is not not executed always it goes to else if
        rCount = totalRowCount;
        getMessageUpdate(rCount, maxCount);
    } else if (jQuery('#all_filter').val() == "record_list") {
        rCount = selectedRecord;
        getMessageUpdate(rCount, maxCount);
    }
    enableOKButton();
}
function enableOKButton() {
    if (rCount != 0 && rCount <= maxCount && gel('relate_info_object').value != "") {
        $j("#ok_button").removeAttr("disabled");
    } else {
        jQuery('#ok_button').attr('disabled', true);
    }
}

function getMessageUpdate(rCount, maxCount) {
    if (rCount > maxCount) {
        jQuery('#grc_count_warn').html(new GwtMessage().getMessage('You have selected {0} records. The maximum number of records you can select is {1}.', rCount, maxCount));
    } else if (rCount == 0) {
        jQuery('#grc_count_warn').html(new GwtMessage().getMessage('Select records to update.'));
    } else {
        rCount = rCount == '1' ? '1 record' : (rCount + ' records');
        jQuery('#grc_count_warn').html(new GwtMessage().getMessage('This action will update {0}.', rCount));
    }
    $j('#grc_count_warn').show();
    jQuery('#number_of_updates').show();
}
function performAssosication() {
    var ajax = new GlideAjax('global.GetRecordsSelected');
   // ajax.addParam('sysparm_tableName', tableName);
    ajax.addParam('sysparm_infoObject', gel('relate_info_object').value);
  //  ajax.addParam('sysparm_operation', actionOK());
    ajax.addParam('sysparm_recordSelection', jQuery('#all_filter').val());
    if (jQuery('#all_filter').val() == 'condition') {
        ajax.addParam('sysparm_recordQuery', recordQuery);
    } else if (jQuery('#all_filter').val() == 'record_list') {
        ajax.addParam('sysparm_recordList', records);
    }
    ajax.addParam('sysparm_name', 'associateInfoObjectandRecords');
    ajax.getXMLAnswer(function(answerStr) {});
}

I am updating the records in Script Include: global.GetRecordsSelected

SI Logic:

var GetRecordsSelected = Class.create();
GetRecordsSelected.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    
    associateInfoObjectandRecords: function() {
     //   var tableName = this.getParameter('sysparm_tableName');
        var infoObject = this.getParameter('sysparm_infoObject');
     
        var selectionFilter = this.getParameter('sysparm_recordSelection');
        var recordList = "";
        var rec = "";
        if (selectionFilter == 'condition') {
            var queryStr = this.getParameter('sysparm_recordQuery');
            rec = new GlideRecord(tableName);
            rec.addEncodedQuery(queryStr);
            rec.query();
        } else if (selectionFilter == 'record_list') {
            recordList = this.getParameter('sysparm_recordList');
        }
       
        if (recordList != "") { //else if (recordList != "") 
            var list = recordList.split(",");
             var gr = new GlideRecord('incident');
        gr.addQuery('sys_id', 'IN', list);
        gr.query();
        while (gr.next()) {
            //gr.state = '3'; //On Hold
            gr.setValue("assignment_group", infoObject);
            //gr.assignment_group = '019ad92ec7230010393d265c95c260dd'; //Analytics Settings Managers
            gr.update(); - HERE AFTER UPDATE IS MADE I WANT TO KNOW IF THE UPDATE IS SUCCESSFUL AND IF IT IS SUCCESS i WANT TO IMPLEMENT ONE MORE LOGIC HERE

ALSO NOT SURE HOW TO PASS A  VALUE FROM SI TO UI PAGE 
        }
        }
    },

    type: 'GetRecordsSelected'
});

not sure if this would explain my requirement. Please shoot me with questions. I want this to get fixed.

Thanks!

1 REPLY 1

Allen Andreas
Tera Patron

Hello,

Please use the appropriate forum feature: "Insert/edit code sample" button when pasting code so that it remains organized and easier to read. Currently, it can be difficult to read as it's a big wall of text.

The button looks like this: {;}

Please mark reply as Helpful, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!