- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 07-18-2018 09:53 AM
Problem:
The ServiceNow order guide includes a field called "Requested For" that is collected at the end of the order. Out-of-the-box, this field is defaulted to the current user (the user who is submitting the order).
Some clients want to collect the user name in a variable as part of a catalog item, which executes prior to the checkout. They then want this user to be set as the Requested For on the cart.
The requested_for field is part of the sc_cart record, which supports the Cart used within the order guide. I have seen several posts online that attempt a solution. However, these solution are either clunky or just don't work with the latest version of ServiceNow (Kingston).
The solution below is simple, lightweight, and should be pretty stable across ServiceNow versions as long as the basic Order Guide/Cart/Variable relationships remain intact.
Solution:
This solution waits for the user to select the User in an item variable, and then runs a business rule whenever that variable's value changes.
This can be accomplished by creating a business rule on the `sc_item_option` table. This table contains the variable question and answers that are in the catalog item.
Once you have decided which variable you want to pull the new Requested For value from, you will need to set a condition so this business rule only runs for the variable that we are pulling the User from
Here's an example:
Business Rule Condition:
current.item_option_new == 'abcdef5134cc63500323dc0c4cbatuvxyz' //only run this rule for the specific variable that collects the User.
Business Rule Script:
(function executeRule(current, previous /*null when async*/) {
//find the cart that contains this item, and update its requested_for value
var cartGR = getCartFromItem(current);
if( cartGR.getRowCount() ){
cartGR.requested_for = current.value;
cartGR.update();
}
function getCartFromItem(optionGR) {
var itemGR = new GlideRecord("sc_cart_item");
itemGR.get(optionGR.cart_item);
var cartGR = new GlideRecord("sc_cart");
cartGR.addQuery("sys_id", itemGR.cart);
cartGR.addQuery("current_guide_active", "abcdefge045e45008d6d57488930a4fa"); //my order guide
cartGR.query();
cartGR.next();
return cartGR;
}
})(current, previous);
Note: Unless you want this rule to affect any order guide using this variable, it is prudent to ensure that the variable triggering this rule is also part of an `sc_cart` record that is being used on the appropriate order guide (see the current_guide_active as part of the query in the rule script above.)
- 3,459 Views
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
I'm having trouble getting this to work...admittedly I am a newbie ... I have an order guide. I put the sys id of that order guide in the code where it looks like it should go. I got the sys id of my reference variable that is the user and put that in the condition. That reference variable is part of a variable set... I don't know if that makes a difference or not. But it is definitely the sys_id for the variable and not the set. I have the business rule running on the sc_item_option` table. I'm not sure what to put in the when to run field... before? after? insert? query?...I've tried it a bunch of different combinations...so maybe I'm doing something else wrong.