Question about Client Script oncelledit & script include glideajax call

anfield
Tera Guru

I am trying to create a client script oncelledit that calls a script include so that the rows in the table will be updated dynamically. What I am wondering is how do I retrieve the field value in the list view being edited to pass to the script include?

 

What I am trying to do is when -   editing a certain field in my custom table in the list, for example - computer name, and I want to pass that to the script include so that it can use that value to do a lookup to the cmdb to pull back support group and operational status to populate to my custom table. How do I do that? Researching this I am seeing that I cannot use g_form on the list view - so how do I pass that field to the script include so that it can do a lookup?

Can I accomplish what I need with this approach? Do a lookup and present the new values on screen in the list view?

 

1 ACCEPTED SOLUTION

Willem
Giga Sage
Giga Sage

Hi Anfield,

I recently answered a similar question. Feel free to copy and make adjustments.

https://community.servicenow.com/community?id=community_question&sys_id=42ba1100db561810b1b102d5ca96...

Client:

function onCellEdit(sysIDs, table, oldValues, newValue, callback) {
    var saveAndClose = true;
    //Type appropriate comment here, and begin script below

    var gaRegion = new GlideAjax("cellEditHandler");
    gaRegion.addParam('sysparm_name', 'getLocation');
    gaRegion.addParam('sysparm_locIDs', sysIDs.join(","));
    gaRegion.addParam('sysparm_newValue', newValue);
    gaRegion.getXML(alertRegion);

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

    callback(saveAndClose);

}

 

Server Script Include:

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

    getLocation: function () {
        var locIDs = this.getParameter("sysparm_locIDs");
        locIDs = locIDs.split(",");
        var newValue = this.getParameter("sysparm_newValue") + '';
        var strOutput = 'empty';
        var grLocation = new GlideRecord('cmn_location');
        if (grLocation.get(newValue)) {
            var grIncident = new GlideRecord('incident');
            grIncident.addQuery("sys_id", "IN", LocIDs);
            grIncident.query();
            while (grIncident.next()) {
                grIncident.u_region = grLocation.u_region;
                grIncident.update();
            }
            strOutput = grLocation.getDisplayValue();
        }
        return "Region updated to: " + strOutput;

    },

    type: 'cellEditHandler'
});

 

This also works if multiple are edited in the list at once. You can modify the second query part to fit your custom table.

 

In addition of the working script I must ask you. Why not do it in a Business rule? Before update of the record, set the value of the Region to the region you want. If it is a field on the location you could even dotwalk.

When to run:

 

Actions:

 

 

View solution in original post

12 REPLIES 12

Changed that and the error is gone. Also I can see the values being set in my alerts. But it doesnt actually set the value

In the client script logging I can see that the value 1 is returned to the client script (first line below), so it looks like it is doing its job mostly, but the value is not being set. I'm not sure though if setvalue can be used in an oncelledit client script?

 

alert('Big Fix: Server opstatus: ' + server_opstatus);
g_form.setValue('u_server_status', server_opstatus);

No, it is not available since you are not on a form (hence g_form😊).

Can you mark answers as helpful and the one that helped you fix the call as correct? Rewards me a bit for the time and effort👌

Definitely will. And thanks for the help.