- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-03-2015 07:37 PM
Hi Experts,
I want to know if it is possible to query variable values associated with an item in Shopping Cart. My Catalog item as a few Variables, and once I have added an item to the cart, I would like to warn the user if they try to select the same variable values, or conflicting ones(vs the values for item already in cart). I tried to use Client Script I query sc_cart_item using glideRecord with:
var cart_item = new GlideRecord('sc_cart_item');
cart_item.query();
if(cart_item.next()) {
alert('Cart Item variable1 : ' + cart_item.cat_item + ' and Cart Item variable2= ' + cart_item.cat_item);
}
I tried cart_item.cat_item.getValue('variable1') and that breaks the script. and cart_item.variable1 but that gives me undefined.
For cart_item.cat_item I get an object, I see the sys_id(in pop-up), I can query that sys_id using SOAP but I don't see how to get to the variables and values attached to the cat_item sitting in cart.
Is this possible? What am I missing?
Thanks
Solved! Go to Solution.
- Labels:
-
User Interface (UI)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-03-2015 08:47 PM
Hi Michael,
Please refer to this thread for the solution of what you're looking for:
Accessing variables from a given user's cart (before REQ or RITM is created)?
I hope this is helpful and please let me know if you have any follow up questions.
Thanks,
Berny
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-03-2015 08:47 PM
Hi Michael,
Please refer to this thread for the solution of what you're looking for:
Accessing variables from a given user's cart (before REQ or RITM is created)?
I hope this is helpful and please let me know if you have any follow up questions.
Thanks,
Berny
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2015 07:19 AM
Thanks a bunch Berny for the pointer.
I adapted the solution a little bit, I was able to include the script below within an onChange() function for Client Script that is already performing some other validation, and it works. It warns me as soon as I select the duplicate role in drop-down(pointing to custom role table) if I try to select a role that is already sitting in cart, form variable is u_selected_role, for catalog item 'My Shopping Cart':
var items = new GlideRecord('sc_cart_item');
items.addQuery('cart.user', g_user.userID);
items.addQuery('cat_item.name', 'My Shopping Cart');
items.query();
var answer = [];
var ids = [];
while (items.next())
{
ids.push(items.sys_id.toString());
}
for (i=0;i < ids.length; i++)
{
var options = new GlideRecord('sc_item_option');
options.addQuery('cart_item', ids[i]);
options.addQuery('item_option_new.name', 'u_app_role');
options.query();
if (options.next())
answer.push(options.value.toString());
if (g_form.getValue('u_selected_role') == options.value.toString()) {alert ('The role is already in cart : ' + options.value.toString());}
}
it may be a better idea to execute some the code on server side for performance, so splitting it back into server+client sides is probably the way to go.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2015 11:07 AM
Sounds great!
You can accomplish decoupling that logic using GlideAjax. http://wiki.servicenow.com/index.php?title=GlideAjax#gsc.tab=0
That will give you the benefit that you can retrieve only the data that you need instead of retrieve all the Glide record data.
Another option is to do an asynchronous GlideRecord, it's not as great in performance as the GlideAjax since you still retrieve all the Glide record into the client but the benefit is that you do it in an asynchronous way which makes really dynamic and with an minimum impact to the browser and so then the end users execution. It's also a little bit simpler to write than a GlideAjax
Your code with an asynchronous GlideRecord is something like the following:
var answer = [];
var ids = [];
var items = new GlideRecord('sc_cart_item');
items.addQuery('cart.user', g_user.userID);
items.addQuery('cat_item.name', 'My Shopping Cart');
items.query(processItems);
function processItems(items){
while (items.next()){
ids.push(items.sys_id.toString());
}
for (i=0;i < ids.length; i++){
var options = new GlideRecord('sc_item_option');
options.addQuery('cart_item', ids[i]);
options.addQuery('item_option_new.name', 'u_app_role');
options.query(processOptions);
function processOptions(options){
if (options.next())
answer.push(options.value.toString());
if (g_form.getValue('u_selected_role') == options.value.toString()) {
alert ('The role is already in cart : ' + options.value.toString());
}
}
}
}
Still, due to the amount of server calls that you have in your code, an async GlideAjax call to a script include is perhaps the best solution for you.
I hope you find this helpful or at least insightful
Have a good one!
Thanks,
Berny