Business Rule inconsistently running

chrisp1
Mega Sage

Hi,

I've written a business rule to copy a date variable from one item in an order guide (PC logon account) to all other items ordered within the order guide, but it is only working inconsistently, sometimes updates none, sometimes a few, but never all. It's a standard variable on the item on which it is populated and then in a variable set on all the other items. Trying to avoid changing the whole OG to move the field to the initial screen.

So i'm assuming something wrong with my code, any suggestions gratefully received.

 

BR: After Update, Table sc_req_item, condition Item = PC logon account

 

(function executeRule(current, previous /*null when async*/ ) {
    var gr = new GlideRecord('sc_req_item');
    gr.addQuery('request', current.request);
    gr.addQuery('cat_item', '!=', 'd6753956dbd6881084cfa7c7489619b0'); //PC logon account
    gr.query();
    while (gr.next()) {
        gr.variables.start_date = current.variables.start_date;
        gr.update();
    }
})(current, previous);
 
TIA Chris
1 ACCEPTED SOLUTION

Mark Manders
Mega Patron

If that's the issue, maybe don't trigger the BR on the sc_req_item, but on the sc_task, when cat_item = PC Logon. Or why not create a flow for it. All flows run after, but you can easily put a wait condition in it. Have the system wait for x seconds after creation of the logon item and then run through all other related items.


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

View solution in original post

4 REPLIES 4

Mark Manders
Mega Patron

And if you try it like this:

(function executeRule(current, previous /*null when async*/) {
    var gr = new GlideRecord('sc_req_item');
    gr.addQuery('request', current.request);
    gr.addQuery('cat_item', '!=', 'd6753956dbd6881084cfa7c7489619b0'); // Exclude PC Logon account
    gr.query();
    while (gr.next()) {
        // Update the start_date variable for each matching request item
        updateStartDateVariable(gr, current.variables.start_date);
    }
})(current, previous);

function updateStartDateVariable(reqItemGr, startDate) {
    var vars = new GlideRecord('sc_item_option_mtom');
    vars.addQuery('request_item', reqItemGr.sys_id);
    vars.query();
    while (vars.next()) {
        // Check if this is the start_date variable
        if (vars.sc_item_option.name === 'start_date') {
            vars.sc_item_option.value = startDate;
            vars.sc_item_option.update();
        }
    }
}

Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

Thanks for the input Mark, unfortunately that doesn't seem to update the field at all ðŸ˜ž

chrisp1
Mega Sage

On further testing it seems that the script(s) only work against the RITMs which are created before the PC logon account RITM, any with a higher number have the field left blank. Is there any way to ensure an order guide item is created last, or to delay the BR until the order guide has finished?

Mark Manders
Mega Patron

If that's the issue, maybe don't trigger the BR on the sc_req_item, but on the sc_task, when cat_item = PC Logon. Or why not create a flow for it. All flows run after, but you can easily put a wait condition in it. Have the system wait for x seconds after creation of the logon item and then run through all other related items.


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark