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

I went back in and did a glide update inside the script include like yours, but I still need to refresh my screen in the list view to see the updated value.

Do you know any way around this? That was mainly the reason for using the glideajax. I was thinking that updates in the list view would appear on the record on screen.

 

Ok, we figured out the glideajax part. Can you create a new question to update the values to show in the list to the user? I think that way more people are inclined to help👍

Ok. Thanks