Unable to update current Request with after insert BR

Vijay Kumar4
Mega Guru
(function executeRule(current, previous /*null when async*/) {
    var cardOrderSysId = '000e49541b0e0d10683742e7bd4bcbed';

    if (current.cat_item.toString() !== cardOrderSysId) {
        return;
    }

    var tinNumber = current.variables.u_tin;
    var memberNumber = current.variables.u_member_number;

    if (!tinNumber && !memberNumber) {
        return;
    }

    var matchingRITMs = [];
    var reqToRitmMap = {}; 

    var gr = new GlideRecord('sc_req_item');
    gr.addQuery('cat_item', cardOrderSysId);
    if (tinNumber) {
        gr.addQuery('u_tin', tinNumber);
    }
    if (memberNumber) {
        gr.addQuery('u_member_number', memberNumber);
    }
    gr.query();

    while (gr.next()) {
        var reqSysId = gr.request.toString();
        var ritmNum = gr.number.toString();

        matchingRITMs.push({
            reqSysId: reqSysId,
            ritmNum: ritmNum
        });

        if (!reqToRitmMap[reqSysId]) {
            reqToRitmMap[reqSysId] = [];
        }

        reqToRitmMap[reqSysId].push(ritmNum);
    }

    for (var reqId in reqToRitmMap) {
        var currentRitmNums = reqToRitmMap[reqId];
        var otherRitmNums = [];

        matchingRITMs.forEach(function(entry) {
            if (entry.reqSysId !== reqId) {
                otherRitmNums.push(entry.ritmNum);
            }
        });

        if (otherRitmNums.length > 0) {
            var reqRec = new GlideRecord('sc_request');
            if (reqRec.get(reqId)) {
                reqRec.u_isduplicate = true;
                reqRec.u_reference_ticket_number = otherRitmNums.join(', ');
                reqRec.update();
            }
        }
    }

})(current, previous);

BR: After insert

VijayKumar4_0-1744041154034.pngVijayKumar4_1-1744041176590.png

 

I’ve implemented a Business Rule to identify duplicate requests based on either the TIN or Member Number provided in the request. For example, we have a catalog item called Card Order, which includes two variables: TIN and Member Number. Users can enter either of these, and the value is then mapped to the corresponding field in the RITM.

Here's the intended logic:

If a user submits a request with 123 as the Member Number, it creates REQ001 → RITM001.

If another request is submitted with the same Member Number, it creates REQ002 → RITM002.

In this case, within REQ002, I expect:

isDuplicate to be set to true

u_reference_ticket_number to be updated with RITM001

Likewise, REQ001 should have its u_reference_ticket_number updated to include RITM002.

However, this is not working as expected. The values are not updating correctly. When I submit a third request (REQ003 → RITM003) with the same Member Number, the current result is:

REQ001 → u_reference_ticket_number = RITM002

REQ002 → u_reference_ticket_number = RITM001

REQ003 → u_reference_ticket_number = empty

But the expected behavior is:

REQ001 → u_reference_ticket_number = RITM002, RITM003

REQ002 → u_reference_ticket_number = RITM001, RITM003

REQ003 → u_reference_ticket_number = RITM001, RITM002

In short, the new request is not updating the reference field in previously created requests, and its own reference field is not being populated with existing matches either.

Could you please help me identify what's missing or going wrong in this logic?

 

5 REPLIES 5

Sure! Here's a polished rephrased version:


Hi @Robert H, thanks for your suggestion. I’m currently using this approach, and it typically results in a maximum of 4–5 duplicate requests with the same TIN/Member Number. However, each record contains some additional differing data. Is there a way to achieve my requirement by updating the existing Business Rule?