Duplicate request creating with same Number

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 existingReqItem = new GlideRecord('sc_req_item');
    existingReqItem.addQuery('cat_item', cardOrderSysId);
    existingReqItem.addQuery('sys_id', '!=', current.sys_id);
    if (tinNumber) {
        existingReqItem.addQuery('u_tin', tinNumber);
    }
    if (memberNumber) {
        existingReqItem.addQuery('u_member_number', memberNumber);
    }
    existingReqItem.query();

    if (existingReqItem.next()) {
        var req = current.request.getRefRecord();
            req.u_isduplicate = true;
            req.u_reference_ticket_number = existingReqItem.number;

            req.update();
        
    }
})(current, previous);



The above is my Business Rule, which runs after insert. The correct RITM is being updated, but I’m facing an issue. When I submit the card with an existing TIN or Member Number, two requests are being created with the same request number—one contains the requested item, while the other does not.

Why is this happening? Why is a duplicate request being created with the same number? How can I prevent this? This issue only occurs when a duplicate TIN or Member Number is found; other cases work fine. 

VijayKumar4_0-1743069893748.png

 



Please note that, I have created a workfow to assign some default values when this request submitted.

1 ACCEPTED SOLUTION

Shivalika
Mega Sage

Hello @Vijay Kumar4 

 

Remove req.update() 

 

Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this questions from unsolved to solved bucket. 

 

Regards,

 

Shivalika 

 

My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194

 

My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeEISQCY

View solution in original post

3 REPLIES 3

Medi C
Giga Sage

Hello @Vijay Kumar4,

 

Did you check if the variables u_member_number and u_tin if empty/null? Please do gs.info to log the values of those variables and share the results if they are empty?

gs.info(current.variables.u_tin);
gs.info(current.variables.u_member_number);

 


If you found this helpful, please hit the thumbs-up button and mark as correct. That helps others find their solutions.

Shivalika
Mega Sage

Hello @Vijay Kumar4 

 

Remove req.update() 

 

Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this questions from unsolved to solved bucket. 

 

Regards,

 

Shivalika 

 

My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194

 

My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeEISQCY

Simon Christens
Kilo Sage

As far as I remember the RITM is inserted into the DB _before_ the REQ.
When you then update the (at this point - non existant) request then the req.update() inserts an empty request record.
Try changing the Business rule to run async to give SN a chance to create the Req before you update it.
Also include some validation

    if (existingReqItem.next()) {
		var req = new GlideRecord('sc_request');

        if(req.get(current.getValue('request') && req.isValidRecord())){
            req.u_isduplicate = true;
            req.u_reference_ticket_number = existingReqItem.number;

            req.update();
		}
    }

 

You can also try changing the execution order to 1.000.000 on the BR as it wait a bit longer (after BR) before it runs