The CreatorCon Call for Content is officially open! Get started here.

Business rule is running twice with async and update?

Santhosh15
Tera Guru

Hello all,

 

When I am using current.update() in my code, the business rule is running twice for assigning ticket to some other team.

Please help on this issue.

 

Code Below:

=================================================

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

// Add your code here
var httpStatus = current.u_http_status_code;

if (httpStatus == 201) {
gs.log("inside 201 block" + httpStatus);
var seconds = 90;
var second = parseInt(seconds, 10) * 1000;
var start = parseInt(new Date().getTime()) + second;
while (start > parseInt(new Date().getTime())) {
// do nothing
}
gs.log("outside the waitblock while loop response w");
if (current.sys_updated_by != "svc.awx") {
current.assignment_group = "72fb418d13c863004d8e5ce12244b008"; //Unix
current.update();
}

} else {
current.assignment_group = "e6fb018d13c863004d8e5ce12244b0dc"; //ServiceNow Team
current.update();
}

})(current, previous);

16 REPLIES 16

You can try before business rule and remove the current.update() else your last resort would be to add as per support kb article.

current.setWorkflow(false);
current.update();
current.setWorkflow(true);

 

Ankur Bawiskar
Tera Patron
Tera Patron

@Santhosh15 

if you are setting field values on same table on which BR is running then you should use Before and not After BR

update the BR as Before and remove current.update();

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

Pavankumar_1
Mega Patron

Hi @Santhosh15 ,

you are using async because you are  making a REST call from business rule. If yes can you below script 

If not for integration go with before business rule and remove current.update();

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

    // Add your code here
    var httpStatus = current.u_http_status_code;

    if (httpStatus == 201) {
        gs.log("inside 201 block" + httpStatus);
        var seconds = 90;
        var second = parseInt(seconds, 10) * 1000;
        var start = parseInt(new Date().getTime()) + second;
        while (start > parseInt(new Date().getTime())) {
            // do nothing
        }
        gs.log("outside the waitblock while loop response w");
        if (current.sys_updated_by != "svc.awx") {
            current.assignment_group = "72fb418d13c863004d8e5ce12244b008"; //Unix
        }

    } else {
        current.assignment_group = "e6fb018d13c863004d8e5ce12244b0dc"; //ServiceNow Team
    }
    current.setWorkflow(false);
    current.update();
    current.setWorkflow(true);

})(current, previous);

 

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

Hello @Pavankumar_1 ,

 

When I applied this code, I am not able to capture assignment group data on activities section.  

Maybe when we applying this code, Audit history will not capture in ServiceNow?

Please reply to this message.

 

Thank you

@Santhosh15 

if you use setWorkflow(false) then it won't show in activities section

Did you check my response and make the BR as Before instead of After?

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