On Cell edit query

Community Alums
Not applicable

Hi All,

 

From incident list, if the category changes to software and subcategory changes to email then record should not submit.

I have written the On cell edit client script:

JPRamyaPriya_0-1690137449392.png

 

Script include:

JPRamyaPriya_1-1690137509813.png

 

How to pass newValue of subcategory to script include?

Any help is appreciate!!

Thanks in advance!!

 

 

2 ACCEPTED SOLUTIONS

Riya Verma
Kilo Sage
Kilo Sage

Hi @Community Alums ,

 

Hope you are doing great.

 

Its not a good practice to use on cell edit client script , for your requirement , you can go ahead with before BR:

  1. the "When to run" section, select "Before" to ensure the rule runs before the record is submitted.
  2. For the "Advanced" condition, use the following script:

 

 

 

(function executeRule(current, previous /*, g*/) {
    if (current.category.changes() || current.subcategory.changes()) {
        if (current.category == 'software' && current.subcategory == 'email') {
            current.setAbortAction(true);
            gs.addErrorMessage("Record submission is not allowed when the category is 'software' and the subcategory is 'email'.");
        }
    }
})(current, previous);
​

 

 

Also when you are using getXML call while calling script include via GlideAjax, its a asynchronous call so it will be going into queue. it will not going to wait to get the response from server and decide the abort action. So this is reason why its failing in script. Try using before BR for your requirement to abort submit.

 

 

 

 

 
 
Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.
Regards,
Riya Verma

View solution in original post

Hi @Community Alums ,

 

You have a solution now using onCellEdit from Khasim Pathan. I also tried, my version follows

 

 

function onCellEdit(sysIDs, table, oldValues, newValue, callback) {
	var saveAndClose = true;
	//Type appropriate comment here, and begin script below
	var sysIDstr = sysIDs;
//	alert('sysIDstr = ' + sysIDstr + ', newValue = ' + newValue);
	if (newValue == 'email') {
		var incGA = new GlideAjax('incidentTable');
		incGA.addParam('sysparm_name', 'tablequery');
		/* These don't work for some reason
		incGA.addParam('sysparm_syssid', sysIDstr);
		incGA.addParam('sysparm_newvalues', newValue);
		*/
		incGA.addParam('sysparm_newValue', newValue);
		incGA.addParam('sysparm_sysId', sysIDstr);
		incGA.getXML(myid);
	}

	function myid(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
		var res = JSON.parse(answer);
        if (res.status === 'false') {
			alert(res.message);
			callback(false);
		}
		else
			callback(true);		
	}
}

 

Script include:

 

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

	tablequery: function() {
		obj = {};
		obj.status = 'true';
        obj.message = '';
        var newValue = this.getParameter('sysparm_newValue');
        var sysId = this.getParameter('sysparm_sysId');
//		gs.info("incidentTable: newValue = " + newValue + ", sysId = " + sysId);

		/* doesn't work
        var incID = this.getParameter('sysparam_syssid');
		var incSC = this.getParameter('sysparam_newvalues');
		gs.info('incidentTable: Checking for sys_id =  ' + incID + ', incSC = ' + incSC + '.');
		*/

		var incCategory = '';
        var empname = new GlideRecord('incident');
        empname.addQuery('sys_id', sysId);
        empname.query();
//		gs.info('incidentTable: Found ' + empname.getRowCount() + ' records.');
        if (empname.next()) {
//			gs.info('incidentTable: category: ' + empname.category);
            incCategory = empname.category;
        }
        if ((incCategory == 'software') && (newValue = 'email')) {
//			gs.info('incidentTable: Got invalid combination.');
			obj.status = 'false';
            obj.message = gs.getMessage('Invalid combination of Category: software and Subcategory: email');
            return JSON.stringify(obj);
		}
        return JSON.stringify(obj);
    },
	
    type: 'incidentTable'
});

 

 

I changed some name values, as starting with your versions didn't work. Also see examples of how to debug the client script and script include (Debug lines have been commented out). And from what I can tell, this only works in List view of incident. As I can still select that combination on the incident form. A business rule solution would prevent both. But you can satisfy your lead with the above or what Khasim posted.

View solution in original post

10 REPLIES 10

Tony Chatfield1
Kilo Patron

Hi, what are the results of your debugging\testing? what exactly is\is not working?
Evaluating screenshots is not ideal, perhaps you could share your code as plain text.

Community Alums
Not applicable

Thanks Tony!!

what are the results of your debugging\testing? -- In the alert we are getting old value of category

what exactly is\is not working? - if we change category to software in the list and sub category changes to email then record should not submit to server. Script should check is it category is software or not, if yes, then the record should abort.

 

On cell edit Client script:

Field name: Sub category

Script:

function onCellEdit(sysIDs, table, oldValues, newValue, callback) {
var saveAndClose = true;
//Type appropriate comment here, and begin script below
var ga = new GlideAjax('incidenttTable');
ga.addParam('sysparm_name', 'tablequery');
ga.addParam('sysparm_syssid', sysIDs);
ga.addParam('sysparm_newvalues', newValue);
ga.getXML(myid);

function myid(response) {

var answer = response.responseXML.documentElement.getAttribute("answer");
alert('ramya' + answer);
if (answer == 't1')
if (newValue == 'email')
alert('anns' + answer);
saveAndClose = false;

}
callback(saveAndClose);
}

 

Script Include:

var incidenttTable = Class.create();
incidenttTable.prototype = Object.extendsObject(AbstractAjaxProcessor, {
 
    tablequery: function() {
        var sysid = this.getParameter('sysparm_syssid');
        //var managername = '';
        var empname = new GlideRecord('incident');
        empname.get(sysid);
        empname.query();
        if (empname.next()) {
gs.addInfoMessage('category' + empname.category);
            var state = empname.category;
        }
        var catgory;
        if (state == 'software')
gs.addInfoMessage('category inside' + empname.category);
            catgory = 't1';
        return catgory;
    }
});
 
Thanks in advance!!

Community Alums
Not applicable

Hi @Community Alums ,

You can acheive this without coding.

1. Create Before Insert/Update BR.(It'll work for list editing too)

2.  Filter conditions : category is software

                                    Subcategory is Email

3. Under actions Tab: select Add message & Abort action.

 

Note: Script includes + OncellEdit scripts are not efficeint together , You can user before BR.

Community Alums
Not applicable

Hi Khasim,

Thanks!!

My lead want me to do with On cell Edit.

 

Any idea?