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

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:

 

 

Because the business rule wont refresh the list view, right? Thats why I dont use a business rule

What I have looks pretty similar to what you listed above. I thought I had what I needed. Am I correct in saying that the business rule you posted would not refresh the list view? Thats why I am not using a business rule for this, but trying instead to use the glide ajax.

 

Client script:


var saveandclose = true;

var computer_name = newValue;
alert('BigFfix Comp name new: ' + computer_name);

var ga = new GlideAjax('BigFix_GetComputerData');
ga.addParam('sysparm_name','RetrieveData'); // Always try to use asynchronous (getXML) calls rather than synchronous (getXMLWait)
ga.addParam('sysparm_computer_name', computer_name);
ga.getXML(parseResponse);

function getResponse(response) {

var server_opstatus = response.responseXML.documentElement.getAttribute("answer");
alert('Big Fix: Server opstatus: ' + server_opstatus);
//g_form.setValue('u_server_status', server_opstatus);

var saveandclose = true;
}

callback(saveandclose);
}

 

Script include:

 

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


RetrieveData: function(){

gs.log('Big Fix: Script include: start ');
var computer_name = this.getParameter('sysparm_computer_name');
var gra = new GlideRecord('cmdb_ci');
gra.addQuery('name', computer_name);
gra.query();

while (gra.next()){

var server_opstatus = gra.getValue('operational_status');
gs.log('Big Fix: Script include: ' + server_opstatus);
return server_opstatus;

}

}, //end func

type: 'BigFix_GetComputerData'
});

 

When I modify the cell in the cell edit, I can see the client script runs as I see an alert, but in developer tools I can see there is an issue: "Uncaught ReferenceError: parseResponse is not defined". The cell edit doesnt even go through, so when I click the green confirm button it doesnt save the new cell value

 

Any idea where I am going wrong here? Also - how can I create logs inside the script include? I cannot see any of my gs.log statements for the script include

Your callback is not correct:

ga.getXML(parseResponse);

function getResponse(response) {

Should be the same name:

ga.getXML(parseResponse);

function parseResponse(response) {