Scripting Help- Querying the Database and returning a True/False Answer for onCellEdit Client Script

Su522
Kilo Sage

Would someone please help me with scripting for onCellEdit Client Script?

I have an onChange Client Script that works, but I need to replicate the same functionality with an onCellEdit Client Script.

The onChange Client Script uses a GlideAjax call to a Script Include.

The Script Include queries the database and returns a True/False Answer

The Client Script uses that Answer to determine if a pop up should display with 2 options: 'Yes' (proceed) or 'Cancel' 

 

Needed Outcome:

It needs to query the database to retrieve Order Management 'Order Line Items' that are associated with a 'Product Order'. The value it needs to return to the Client Script is a true/false field 'u_order_line_updated'

 

Order Line Item table: sn_ind_tmt_orm_order_line_item

Product Order table: sn_ind_tmt_orm_product_order 

 

Matching field is the 'Order' field on the 'Product Order' table: order_line_item.order

Matching field is the 'Order' field on the 'Order Line Item'table: order

 

Use Case:

When state changes to "X" on the Product Order table, check all associated Order Line Items, if any of them have the field 'u_order_line_updated' set to true, popup a confirmation window. 

 

The onChange Client Script, GlideModel for the pop up, and the associated Script Include is working.

I need help scripting to replicate this functionality for an onCellEdit Client Script with this logic:

Query the Order Line Item table

If Order # matches the Order # in the Product table

Check if the Order Line Item field 'u_order_line_updated' is true

Do this for all associated Order Line Items

 

should return true or false

(check all associated Order Line Items, if any of them have the field 'u_order_line_updated' set to true)

 

Here is the code that is working for the onChange Client Script and Script Include-

onChange Client Script:

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

    var gFormAlreadySubmitted = false;

// Callback to the Script Include
    var ga = new GlideAjax('OrderLineItems');
    ga.addParam('sysparm_name', 'getOrderLineUpdated');
    ga.addParam('sysparm_order', g_form.getValue('order_line_item.order'));
    ga.getXML(callbackdata);

    function callbackdata(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");

        // Check if the new state is "Sent to billing" and if Order Line Items have been updated
        if ((newValue === '4') && (answer == 'true')) {

            // Opens Pop Up Window

 

Script Include (client-callable):

var OrderLineItems = Class.create();
OrderLineItems.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    getOrderLineUpdated: function() {
        var grI = new GlideRecord("sn_ind_tmt_orm_order_line_item");
//        gs.info("Order: " + this.getParameter("sysparm_order"));
        grI.addQuery("order", this.getParameter("sysparm_order"));
        grI.query();
        while (grI.next()) {
            if (grI.getValue("u_order_line_updated") == "1") // If it is true
                return true;
        }
        return false;
    },
    type: 'OrderLineItems'
});

 

 

Help is GREATLY appreciated!!!

Thank you

5 REPLIES 5

Jon23
Mega Sage

Hi @Su522 ,

 

Here is an example that should work with a few tweaks to match your fields/values:

 

onCellEdit Client Script

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

    var ga = new GlideAjax('OrderLineItems');
    ga.addParam('sysparm_name', 'hasAnyOrderLineUpdated');
    ga.addParam('sysparm_order', sysIDs);
    ga.getXMLWait()
    var response = ga.getAnswer();

    if (response == 'true') {
        var messageResult = confirm('Pop Up Message'); // <<  This will be your pop-up message
    }

    if(messageResult == true) {
        saveAndClose = true;
    } else {
        saveAndClose = false;
    }

    callback(saveAndClose);
}

 

Update your script include with additional functions:

    hasAnyOrderLineUpdated: function() {
        var grProductOrder = new GlideRecord('sn_ind_tmt_orm_product_order');
        if (grProductOrder.get(this.getParameter('sysparm_order'))) {
			return this._orderLineItemCheck(grProductOrder.order_line_item.order.getValue());
		}
    },

    _orderLineItemCheck: function(orderLineItemSysId) {
        var grI = new GlideRecord('sn_ind_tmt_orm_order_line_item');
        grI.addQuery('order', orderLineItemSysId);
        grI.addQuery('u_order_line_updated', true); // <<<< Change this to your specific value
        grI.setLimit(1);
        grI.query();
        if (grI.hasNext()) {
            return true;
        } else {
            return false;
        }
    },

 

Thank you Jon. I very much appreciate your help!!

I'm trying to get the Script Include working but it is not. 'u_order_line_updated' is the correct value

Can you please take a look. Here is what I have now:

 

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

    hasAnyOrderLineUpdated: function() {
        var grProductOrder = new GlideRecord('sn_ind_tmt_orm_product_order');
        if (grProductOrder.get(this.getParameter('sysparm_order'))) {
            return this._orderLineItemCheck(grProductOrder.order_line_item.order.getValue());
        }
    },

    _orderLineItemCheck: function(orderLineItemSysId) {
        var grI = new GlideRecord('sn_ind_tmt_orm_order_line_item');
        grI.addQuery('order', orderLineItemSysId);
        grI.addQuery('u_order_line_updated', true); // <<<< Change this to your specific value
        grI.setLimit(1);
        grI.query();
        if (grI.hasNext()) {
            return true;
        } else {
            return false;
        }
    },


//     getOrderLineUpdated: function() {
//         var grI = new GlideRecord("sn_ind_tmt_orm_order_line_item");
// //        gs.info("Order: " + this.getParameter("sysparm_order"));
//         grI.addQuery("order", this.getParameter("sysparm_order"));
//         grI.query();
//         while (grI.next()) {
//             if (grI.getValue("u_order_line_updated") == "1") // If it is true
//                 return true;
//         }
//         return false;
//     },
    type: 'OrderLineItems2'
});

What field type is 'u_order_line_updated'?  In my example it is a boolean but your field could be a choice as your original shows you evaluating to '1'

//             if (grI.getValue("u_order_line_updated") == "1") // If it is true

 

Hi Jon,

It is a true/false boolean