Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Update records using MRVS and Record Producer( Scoped Application)

supriya pratapa
Tera Guru
I am trying to update order line items based on the selected order. And my record producer is on order (sn_ind_tmt_orm_order) table.
(function executeProducer() {
    var orderSysId = producer.choose_your_order;
  var orderGR = new GlideRecord('sn_ind_tmt_orm_order');
var mrvsData = producer.variables.add_product_offerings_to_order;
    var lineItems;
    try {
        lineItems = JSON.parse(mrvsData);
    } catch (e) {
        gs.addErrorMessage('Invalid MRVS data format.');
        gs.error('MRVS Parse Error: ' + e.message);
        return;
    }
    var updatedCount = 0;
    for (var i = 0; i < lineItems.length; i++) {
        var item = lineItems[i];

        if (!item.order_line_item) {
            gs.addErrorMessage('Missing Order Line Item ID for row ' + (i + 1));
            continue;
        }

        var lineGR = new GlideRecord('sn_ind_tmt_orm_order_line_item');
        if (lineGR.get(item.order_line_item)) {
            lineGR.product_offering = item.product_offering || lineGR.product_offering;
            lineGR.quantity = item.quantity || lineGR.quantity;
            lineGR.update();
            updatedCount++;
        } else {
            gs.addErrorMessage('Order Line Item not found for ID: ' + item.order_line_item);
        }
    }

    if (updatedCount > 0) {
        gs.addInfoMessage(updatedCount + ' order line item(s) updated successfully.');
    } else {
        gs.addErrorMessage('No line items were updated. Please check the input data.');
    }

    current.setAbortAction(true); // Prevent creating a new record
    action.setRedirectURL(orderGR); // Redirect to the updated order

})();
 
 
4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@supriya pratapa 

script looks fine to me.

Did you check by adding logs where it's failing?

 

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

Hi @Ankur Bawiskar 

I have added logs and I see below in logs.

1) Raw MRVS Data: [ {
"order_line_item" : "60b78034337832d0d53768a13d5c7b25",
"brand" : "Chocobay",
"sub_brand" : "tempt",
"flavour" : "Caramel",
"size" : "10 gm",
"product_offering" : "74a33df633283290d53768a13d5c7b70",
"quantity" : "8"
} ]

2)MRVS data is empty or missing.

current.setAbortAction(true); // Prevent creating a new record

(function executeProducer() {
    var orderSysId = producer.choose_your_order;
    gs.info('Selected Order Sys ID: ' + orderSysId);

    var orderGR = new GlideRecord('sn_ind_tmt_orm_order');
    var orderFound = orderGR.get(orderSysId);
    if (!orderFound) {
        gs.addErrorMessage('Order not found for sys_id: ' + orderSysId);
        gs.error('Order not found for sys_id: ' + orderSysId);
        return;
    }
    var mrvsData = producer.add_product_offerings_to_order;
    gs.info('Raw MRVS Data: ' + mrvsData);

    if (!mrvsData || mrvsData.trim() === '') {
        gs.addErrorMessage('No order line items received from the form.');
        gs.error('MRVS data is empty or missing.');
        return;
    }

    var lineItems;
    try {
        lineItems = JSON.parse(mrvsData);
        gs.info('Parsed MRVS line items count: ' + lineItems.length);
    } catch (e) {
        gs.addErrorMessage('Invalid MRVS data format.');
        gs.error('MRVS Parse Error: ' + e.message);
        return;
    }

    var updatedCount = 0;
    for (var i = 0; i < lineItems.length; i++) {
        var item = lineItems[i];
        gs.info('Processing line item #' + (i + 1) + ': ' + JSON.stringify(item));

        if (!item.order_line_item) {
            gs.addErrorMessage('Missing Order Line Item ID for row ' + (i + 1));
            gs.error('Missing order_line_item in row ' + (i + 1));
            continue;
        }

        var lineGR = new GlideRecord('sn_ind_tmt_orm_order_line_item');
        if (lineGR.get(item.order_line_item)) {
            lineGR.product_offering = item.product_offering || lineGR.product_offering;
            lineGR.quantity = item.quantity || lineGR.quantity;
            lineGR.update();
            updatedCount++;
            gs.info('Updated line item ID: ' + item.order_line_item);
        } else {
            gs.addErrorMessage('Order Line Item not found for ID: ' + item.order_line_item);
            gs.error('Order Line Item not found for ID: ' + item.order_line_item);
        }
    }

    if (updatedCount > 0) {
        gs.addInfoMessage(updatedCount + ' order line item(s) updated successfully.');
        gs.info(updatedCount + ' line items updated.');
    } else {
        gs.addErrorMessage('No line items were updated. Please check the input data.');
        gs.error('No line items were updated.');
    }

    // Redirect to the updated order in portal
    var redirectURL = new GlideURL('csp');
    redirectURL.addParam('id', 'customer_order_details');
    redirectURL.addParam('table', 'sn_ind_tmt_orm_order');
    redirectURL.addParam('sys_id', orderGR.getUniqueValue());
    redirectURL.addParam('view', 'portal');
    gs.info('Redirecting to URL: ' + redirectURL.toString());
    action.setRedirectURL(redirectURL);

})();

@supriya pratapa 

so JSON you are getting?

is it not able to parse?

is it not able to get the length and iterate the for loop?

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

@supriya pratapa 

any update to this?

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