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.

How to write onCellEdit for validating multiple records through glideAjax

John Doe
Giga Expert

Hi,

I am trying to create on onCellEdit Client script on one of my table's listview to validate start date should be less than end, I am successful for writing when single record changes, but now I am working towards updating multiple records at a time, I want someone to check my code and guide me, Thank you

 

Script Include Code :

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





    getStatus: function() {

        var sysIds = this.getParameter('sysparm_sysID');
        var endDate = this.getParameter('sysparm_EndDate');
      
        var length = sysIds.length();
        for (i = 0; i < length; i++) {

            if (test(sysIds[i]) > endDate) {
                
				return 'false';

            }
			return 'true';



        }
       

    },

    test: function(id) {
        var gr = new GlideRecord('u_admstry10005_table_1');
        gr.addQuery('sys_id', id);
        gr.query();
        if (gr.next()) {
            return gr.u_date;

        }
    },
    type: 'startDateEndDateMulti'
});

 

onCellEditClient Script

function onCellEdit(sysIDs, table, oldValues, newValue, callback) {
  var saveAndClose = true;
	
   var ga = new GlideAjax('startDateEndDateMulti');
    ga.addParam('sysparm_name', 'getStatus');
    ga.addParam('sysparm_sysID', sysIDs);
	ga.addParam('sysparm_EndDate', newValue);
    ga.getXML(getStartDate);

    function getStartDate(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
     alert(answer);
    }
 
 callback(saveAndClose); 
}

 

when I am performing this it is alerting as null only , please help

1 ACCEPTED SOLUTION

Community Alums
Not applicable

I think you miss one part here, check these SI & CSJS :

<instance>/sys_script_client.do?sys_id=1788ca29c31354509e777d127840dd6a&sysparm_record_target=sys_script_client&sysparm_record_row=3&sysparm_record_rows=22&sysparm_record_list=type%3DonCellEdit%5EORDERBYorder

 

<instance>/sys_script_include.do?sys_id=8ad1ff0dc3df5c109e777d127840ddef

 

You can see that OOB it's using the param sysIDs which is array type (you miss this point) and then , in the loop iteration in the SCJS they do this (in order to pass it to the SI as you select multiple records, right) :

function onCellEdit(sysIDs, table, oldValues, newValue, callback) {
var facet_field = newValue;
for (var i=0; i<sysIDs.length; i++) {
var facet_id = sysIDs[i];

//Call script includes to check if valid Facet Field name
var ga = new GlideAjax('ValidateFacets');
ga.addParam('sysparm_name', 'validate_cell_edit');
ga.addParam('sysparm_facet_field', facet_field);
ga.addParam('sysparm_facet_id', facet_id);
ga.getXMLAnswer(updateField);

}

function updateField (answer) {
if (answer === "invalid") callback(false);
else callback(true);
}

}

View solution in original post

2 REPLIES 2

Community Alums
Not applicable

I think you miss one part here, check these SI & CSJS :

<instance>/sys_script_client.do?sys_id=1788ca29c31354509e777d127840dd6a&sysparm_record_target=sys_script_client&sysparm_record_row=3&sysparm_record_rows=22&sysparm_record_list=type%3DonCellEdit%5EORDERBYorder

 

<instance>/sys_script_include.do?sys_id=8ad1ff0dc3df5c109e777d127840ddef

 

You can see that OOB it's using the param sysIDs which is array type (you miss this point) and then , in the loop iteration in the SCJS they do this (in order to pass it to the SI as you select multiple records, right) :

function onCellEdit(sysIDs, table, oldValues, newValue, callback) {
var facet_field = newValue;
for (var i=0; i<sysIDs.length; i++) {
var facet_id = sysIDs[i];

//Call script includes to check if valid Facet Field name
var ga = new GlideAjax('ValidateFacets');
ga.addParam('sysparm_name', 'validate_cell_edit');
ga.addParam('sysparm_facet_field', facet_field);
ga.addParam('sysparm_facet_id', facet_id);
ga.getXMLAnswer(updateField);

}

function updateField (answer) {
if (answer === "invalid") callback(false);
else callback(true);
}

}

why did he run a loop on sysid?