Is it possible to use g_form in oncell edit client scripts

MaharshiC
Tera Contributor

Hi,

 

Is it possible to use g_form for oncell edit client scripts? If it is not possible how to check for values in other fields for the request using oncelledit?

 

 

10 REPLIES 10

Juhi Poddar
Kilo Patron

Hello @MaharshiC 

  • No, g_form is not available in onCellEdit client scripts because these scripts run in the list view, not on a form.
  • To check values in other fields when an onCellEdit script runs, you can use GlideAjax to fetch the necessary field values from the server.

Example:

If you need to check the value of a field (e.g., approval) when another field (e.g., request_state) is edited, you can do the following:

Client Script configuration:

JuhiPoddar_0-1741772326661.png

onCellEdit Client Script:

 

function onCellEdit(sysIDs, table, oldValues, newValue, callback) {
    var saveAndClose = true;

    var sys_id = g_list.getChecked(); // Gets the sys_id of the edited row
    if (!sys_id) {
        return;
    }

    var ga = new GlideAjax('GetFieldValue');
    ga.addParam('sysparm_name', 'getField');
    ga.addParam('sysparm_sys_id', sys_id);
    ga.addParam('sysparm_field_name', 'approval'); // Replace with the field you want to check

    ga.getXMLAnswer(function(response) {
        if (response === 'approved') { // Modify condition as needed
            g_list.refresh();
            alert('Cannot edit this field as the request is already approved.');
        }
    });

    callback(saveAndClose);
}​

 

Script Include:

 

getField: function () {
        var sys_id = this.getParameter('sysparm_sys_id');
        var fieldName = this.getParameter('sysparm_field_name');
        
        var gr = new GlideRecord('sc_request'); // Change to your table name
        if (gr.get(sys_id)) {
            return gr.getValue(fieldName);
        }
        return '';
    }

 

Note: Script include should be client callable.

Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily and supports the community!"

 

Thank You
Juhi Poddar

sunil maddheshi
Tera Guru

@MaharshiC 

g_form is only available in the form view, so it cannot be used in onCellEdit client scripts because these scripts run in the list view, not in a form.Since g_form is not available, you need to use GlideAjax or getReference() to retrieve values from other fields.

Below is one sample example:

Client script:

 

(function executeRule(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue == oldValue) {
        return;
    }

    // Get the sys_id of the current row being edited
    var sys_id = g_list.getChecked(); // Returns an array of selected records
    if (sys_id.length == 0) return;

    // Use GlideAjax to fetch additional field values from the server
    var ga = new GlideAjax('GetRequestDetails');
    ga.addParam('sysparm_name', 'getFieldValues');
    ga.addParam('sysparm_sys_id', sys_id[0]); // Use the first selected record
    ga.getXMLAnswer(function(response) {
        var result = JSON.parse(response);
        if (result.some_field == 'some_value') {
            alert('Field value matches!');
        }
    });

})(g_list, previousValue, newValue, isLoading, isTemplate);

 

Script inlcude:

 

var GetRequestDetails = Class.create();
GetRequestDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getFieldValues: function() {
        var sys_id = this.getParameter('sysparm_sys_id');
        var gr = new GlideRecord('sc_request'); // Change to your table
        if (gr.get(sys_id)) {
            var response = {
                some_field: gr.getValue('some_field')
            };
            return JSON.stringify(response);
        }
        return '{}';
    }
});

 

Please mark correct/helpful if this helps you!

Ankur Bawiskar
Tera Patron
Tera Patron

@MaharshiC 

g_form won't work in onCell Edit client script.

You can use before update business rule with condition as that field changes and then stop the update if required based on validation

If you want to check if the value entered for that cell on which onCell edit runs is valid or not then something like this will help

function onCellEdit(sysIDs, table, oldValues, newValue, callback) {

  var saveAndClose = true;
  if(newValue == '6' || newValue == '7' || newValue == '9'){
  alert('Not allowed');
  saveAndClose = false;
  } else {
  saveAndClose = true;
  }
callback(saveAndClose);

}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@MaharshiC 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@MaharshiC 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader