Workflow not triggered

jakejeon
Tera Contributor

I ran the script below to create multiple sc_req_item records under a single sc_request. However, I noticed an odd behavior: only one of the sc_req_item records triggers its associated workflow, while the others do not.

Interestingly, if I run the same script again, the workflows are triggered for all the sc_req_item records. This inconsistent behavior is confusing.

 

Has anyone encountered this issue before or have any idea what might be causing it?

 
function CatalogItemSubmitter() {
    this.prepareItem = function (cart, item, request) {
        cart.setVariable(item, "caller", request.caller);
        cart.setVariable(item, "primary_device", request.primaryDevice);
        cart.setVariable(item, "devices", request.secondaryDevices);
        cart.setVariable(item, "business_justification", request.bizJustification1);
        cart.setVariable(item, "business_justification_field", request.bizJustification2);
        cart.setVariable(item, "overwrite_ack", true);
        cart.setVariable(item, "acknowledgement", true);
        cart.setVariable(item, "approver", gs.getUser().getID());
        cart.setVariable(item, "ticket_source", "BULK");
        cart.setVariable(item, "ticket_memo", current.variables.ticket_memo);
        cart.setVariable(item, "ticket_reference", current.sys_id);
    };

    this.submit = function (requests) {
        var catalogItemSysId = "1231231231232131415315315115";
        var cartId = GlideGuid.generate(null);
        var cart = new Cart(cartId);

        var item = cart.addItem(catalogItemSysId);

        for (var i = 0; i < requests.length; i++) {
            var request = requests[i];
            if (i > 0) {
                item = cart.addItem(catalogItemSysId);
            }
            this.prepareItem(cart, item, request);
        }

        var rc = cart.placeOrder();
        return rc.number;
    };
}

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@jakejeon 

seems similar issue was seen earlier and I shared solution for that which worked

placeOrder() method in Cart api causing issue in while loop. 

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@jakejeon 

you are trying to reuse the same Cart Item object.

Move cart.addItem() in loop and not outside -> prepare a new cart for each item

function CatalogItemSubmitter() {
    this.prepareItem = function(cart, item, request) {
        cart.setVariable(item, "caller", request.caller);
        cart.setVariable(item, "primary_device", request.primaryDevice);
        cart.setVariable(item, "devices", request.secondaryDevices);
        cart.setVariable(item, "business_justification", request.bizJustification1);
        cart.setVariable(item, "business_justification_field", request.bizJustification2);
        cart.setVariable(item, "overwrite_ack", true);
        cart.setVariable(item, "acknowledgement", true);
        cart.setVariable(item, "approver", gs.getUser().getID());
        cart.setVariable(item, "ticket_source", "BULK");
        cart.setVariable(item, "ticket_memo", current.variables.ticket_memo);
        cart.setVariable(item, "ticket_reference", current.sys_id);
    };

    this.submit = function(requests) {
        var catalogItemSysId = "1231231231232131415315315115";
        var cartId = GlideGuid.generate(null);
        var cart = new Cart(cartId);

        for (var i = 0; i < requests.length; i++) {
            var item = cart.addItem(catalogItemSysId);
            this.prepareItem(cart, item, requests[i]);
        }

        var rc = cart.placeOrder();
        return rc.number;
    };
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

@jakejeon 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Ankur Bawiskar
Tera Patron
Tera Patron

@jakejeon 

seems similar issue was seen earlier and I shared solution for that which worked

placeOrder() method in Cart api causing issue in while loop. 

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Thanks it works for me as well.