Duplicate RITMs are created while creating the Request using script
Hello Everyone,
I would like to share my experience on the below issue so that it can be helpful to others if someone face similar issue.
Recently while working on a requirement to create the REQ and RITMs via script we ran into an issue where duplicate RITMs were getting created. Now as per the requirement multiple REQ should be created as soon as the user record is created in ServiceNow. So, suppose 200 user records created in 5 seconds then the script will run to create 200 REQ for this 200 users.
We tried to use the Cart() to create REQ as shown below:
_createNewJoinerRequest: function(usrID) {
var newJoinerCatId = gs.getProperty("new.joiner.catalog.item");
var cart = new Cart();
var item = cart.addItem(newJoinerCatId);
cart.setVariable(item, 'requested_for', usrID);
var rc = cart.placeOrder();
var request = rc.getDisplayValue();
return request;
},
But when we tested this, we observed that Duplicate REQs and RITMs were created for same user. For example in ideal scenario if say 3 users are created Arnaud, Sujay and Nick then we should have 3 REQs with 1 RITM each as shown below:
REQ0040061 --> RITM0040062 for user Arnaud
REQ0040062 --> RITM0040063 for user Sujay
REQ0040063 --> RITM0040064 for user Nick
But the REQs and RITMs were created as shown below:
REQ0040061 (Requested for = Arnaud) and has 2 RITMs
--> RITM0040062 (Requested for = Arnaud) & RITM0040065 (Requested for = Sujay)
REQ0040062 (Requested for = Arnaud) and has 2 RITMs
--> RITM0040063 (Requested for = Arnaud) & RITM0040066 (Requested for = Sujay)
After checking on this we observed that there is some issue with the timing. The Cart was getting created as expected but the cart was taking 3-4 seconds to get submitted and hence by the time the Cart is submitted there are multiple Items that are added in the Cart. So, the Cart was getting submitted with more than 1 item in it.
This was happening for most (not all) of the REQ or User. The Cart was getting created for each user record that is created in ServiceNow but by the time the order is placed or Cart is submitted other items were also added to the same cart and hence this issue was observed. We tried different ways to fix this issue like using new sn_sc.CartJS() or new sn_sc.CartJS(gs.generateGUID()) so that there is a unique id generated for each cart. But this does not resolved the issue.
We checked with ServiceNow on this and were able to fix this. There was slight change that was required in the script which we were not able to figure out as that was not clearly mentioned anywhere in documentation. Below is the script that fixed this issue:
_createNewJoinerRequest: function(usrID) {
var newJoinerCatId = gs.getProperty("new.joiner.catalog.item");
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);
var request = {
"sysparm_id": newJoinerCatId,
"sysparm_quantity": "1",
"variables": {
"requested_for": usrID
},
"sysparm_cart_name": cartid // Use "sysparm_cart_name" to set the Cart ID for this cart. This was something that was not clear to me.
};
var cartRequest = cart.orderNow(request);
var requestNumber = "";
if (cartRequest.request_number)
requestNumber = cartRequest.request_number;
return requestNumber;
},
If you find this as helpful or helped you to learn something new, please do mark it helpful.
Thanks.
https://www.servicenow.com/community/itsm-articles/duplicate-ritms-are-created-while-creating-the-request-using/ta-p/2306267