Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Duplicate RITMs are created while submitting catalog item using cart- API

abhishku1
Tera Contributor

I am submitting a catalog item using cart API. but, it's generating multilple duplicate records everytime it execute which i am unable to identify the root case of it.

BR : Async

Table : Astone(u_stone)

Condition: Status changesto 'completed'

insert: true

Script:

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

    try {
        var cart = new sn_sc.CartJS();
        var userid = current.u_user.toString();
        var app_role = 'sys_id of role';
        var item = {
            'sysparm_id': 'sys_id of item', //  //Providing sys_id of the service catalog
            'sysparm_quantity': '1',
            'variables': {
                'requested_for': userid,
                'requested_by': userid,
                'brief_software_description': 'Granting access after learning completion ' + current.u_title,
                'update_access': 'u_modify_access'
                'which_software_requesting': 'sys_id of app',
                'please_select_the_appropriate_role': app_role
            }
        };

        cart.orderNow(item);
        gs.info("Test" + JSON.stringify(cart.orderNow(item)));

    } catch (ex) {
        gs.error('Error triggering Service Catalog item: ' + ex.getMessage());
    }
})(current, previous);
 
also, 'Requested for' on RITM  form is showing different than present 'requested for' on variables of the RITM.
any help on this is really appreciated.
Thanks

 

3 REPLIES 3

Brian Lancaster
Kilo Patron

Not sure if this is the issue with multiple but have you tried running the BR as a before or after rather then async. I don't see anything in the code that would order multiple.

Edit: Also I see in docs that async business rules do not have access to changes to for conditions.

Brian Lancaster
Kilo Patron

For the requested for on the RITM I think you need to add the following after

var cart = new sn_sc.CartJS();

cart.setRequestedFor(current.u_user.toString());

abhishku1
Tera Contributor

@Brian Lancaster , thank you for replying. issue is fixed using below script:

 

        var cartid = "cart_" + gs.generateGUID(); // along with gs.generateGUID() we should append "cart_". This can be used as unique id for the Cart.
        var cart = new sn_sc.CartJS(cartid);
            "sysparm_cart_name": cartid // Use "sysparm_cart_name" to set the Cart ID for this cart.
        cart.orderNow(request); // Use "OrderNow" to submit the request.
Thanks,
Abhi