Access record producer multi-row variable set values from business rule in a scoped app

cloudyrobert
Kilo Guru

I'm hoping that I'm doing something stupid here... I'm trying to take values from a multi-row variable set on a record producer and create them as records in a table related to the one for which the primary record from the record producer is created. E.g. Record producer creates main record. Each row in the multi-row variable set is represented as a row in the supporting table, referencing the main record. This is all within a scoped app.

I've got it working with no issue from a background script once the record has been submitted. However, the same code (modified for context) doesn't work in a business rule running after insert on the table represented below as "x_payment."

/* used in background script only */
var current = new GlideRecord('x_payment');
current.get('__sys_id__');
/* */

var grRow = new GlideRecord('x_funding_sources');

var mrvs;
mrvs = current.variables.funding_sources;
var mrvsArray = JSON.parse(mrvs);

for (i = 0; i < mrvsArray.length; i++) {
    grRow.initialize();
    grRow.payment = current.sys_id;

    grRow.cost_center = mrvsArray[i].cost_center;
    /* etc. */
    grRow.insert();
}
1 ACCEPTED SOLUTION

mattgerry_TP13
Kilo Expert

Hey Robert-

I couldn't get this working with an After business rule either so I ran the business rule Async instead and that seemed to work well for me. Hope this helps!

View solution in original post

5 REPLIES 5

mattgerry_TP13
Kilo Expert

Hey Robert-

I couldn't get this working with an After business rule either so I ran the business rule Async instead and that seemed to work well for me. Hope this helps!

Interesting--yeah, that seems to do it. Thanks, Matt!

Kim Sullivan
Tera Guru

Having similar trouble, would you mind sharing your BR script?  Thanks!

This is very efficient than Glide 'sc_multi_row_question_answer' table .

Here  is my working script.

(function executeRule(current, previous /*null when async*/ ) {

var mrvs = current.variables.vendor_information;
var totalRows = mrvs.getRowCount();
var total = 0;
for (var i = 0; i < totalRows; i++) {
total = total + mrvs.getRow(i).getCell('vendor_price').getCellDisplayValue() * mrvs.getRow(i).getCell('quantity').getCellDisplayValue();
}
var req = new GlideRecord('sc_request');
req.get(current.request);
req.price = total.toFixed(2);
current.price = total;
})(current, previous);