How to add a new item to an existing request
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-29-2015 01:13 PM
On some of our catalog items there is a variable that indicates how many of the items the end user is requesting. For example, if they are ordering a mouse and they set this number variable to a 3, we want three total request items (each with their own workflow) for a mouse under this same request. After searching through the developer community I've come along this post and script (How to add a new item to an existing request with catalog variables). I have this script in an After Insert Business Rule and when I submit a request, I do see that all of my gs.log statements are getting hit, however, I am not seeing the additional RITM's being created. Any help would be greatly appreciated. Do I have this script in the correct location? Is there something wrong with the below script?
function onAfter(current, previous) {
//This function will be automatically called when this rule is processed.
gs.log("Got into the Business rule");
var reqHelper = new GlideappCalculationHelper();
reqHelper.addItemToExistingRequest(current.request, '2861aa256f9b310061b29b9eae3ee45d', 1);
reqHelper.rebalanceRequest(current.request);
gs.log("After rebalanceRequest");
// update/add values to variables on item
var grReqItem = new GlideRecord('sc_req_item');
grReqItem.addQuery('request', current.request);
grReqItem.addQuery('cat_item','2861aa256f9b310061b29b9eae3ee45d');
grReqItem.query();
if(grReqItem.next()) {
gs.log("In the IF statement");
grReqItem.variables.requested_by = current.opened_by;
grReqItem.variables.requested_for = current.opened_by;
grReqItem.variables.phone_number = 'test phone';
grReqItem.variables.type = 'standard';
grReqItem.variables.comments = 'test comments';
grReqItem.variables.send_to = current.opened_by;
grReqItem.parent = current.sys_id;
gs.log("Just about to update.");
grReqItem.update();
}
}
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-29-2015 01:58 PM
This script looks like it's going to the sc_req_item table and looking for existing items. On the line right before your log statement where it says if(grReqItem.next()), That should be if(!grReqItem.next()) and that is to say that if you do not find any items related to the query, then execute everything below. After your log statement "In the IF statement", it should be grReqItem.initialize(); which will instantiate a new record to be created. Then all of the lines that you currently have will set the values accordingly. Just before your last log "just about to update", you need to add grReqItem.insert(); which will actually insert the new record and you can do var record = grReqItem.insert(); gs.log("Just about to update " + record); and that will log the sys_id of the new record that got created. You do not need that last line grReqItem.update(); That should be a good start.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-29-2015 03:23 PM
Out of curiosity, why are you not using the "Add to cart" button instead? That's basically what it does without any customization.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-20-2022 04:34 AM
If you're looking for a way to achieve adding a new catalog item to an existing request with the CartJS API have a look at this Article: