Accessing variables from a given user's cart (before REQ or RITM is created)?

andrew_lawlor
Giga Expert

Is there any way to access a given request item's variables in the Service Catalog via script, before that user checks out (that is, before the REQ and RITMs are created)? I want to perform some validation, but I need to grab a value from one of the mandatory reference field variables   that belongs to a Catalog Item.

I've looked through all of the OOB Script Includes involving Service Catalog cart manipulation, as well as the sc_cart_item table and various other tables. No leads yet. Is anyone willing to shed some light on this issue?

I know the values are stored somewhere -- as they can be displayed from the 'Edit Cart' page. Are these values accessible via script, or is the code to access these values at this point not exposed?

Thanks!

1 ACCEPTED SOLUTION

russell_miller
Kilo Guru

Hi Andrew,



You should be able to get at them, the items in the cart are linked to the sc_cart records.



If you look at one of these (on sc_cart_item) they have the variables for the item listed and they reside on sc_item_option.



These sc_item_option records are linked back to the cart item via the "cart_item" field (display value is the sys_id of the cart item record)



You should be able to put a query together that will grab the cart, find the item, and get the variable value.



Hope that helps.



R


View solution in original post

7 REPLIES 7

russell_miller
Kilo Guru

Hi Andrew,



You should be able to get at them, the items in the cart are linked to the sc_cart records.



If you look at one of these (on sc_cart_item) they have the variables for the item listed and they reside on sc_item_option.



These sc_item_option records are linked back to the cart item via the "cart_item" field (display value is the sys_id of the cart item record)



You should be able to put a query together that will grab the cart, find the item, and get the variable value.



Hope that helps.



R


Thanks - your advice was helpful. After sitting down for a bit and thinking about the problem, I came up with a solution with which I'm satisfied. If you're curious, here it is:



Server side:


var CheckCart = Class.create();


CheckCart.prototype = Object.extendsObject(AbstractAjaxProcessor, {


  checkCart: function() {


// This could be made more modular with parameters.  


var items = new GlideRecord('sc_cart_item');


  items.addQuery('cart.user', gs.getUserID());


  items.addQuery('cat_item.name', 'Change Functional Role Ownership');


  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', 'SelectedFR');


  options.query();



  if (options.next())


  answer.push(options.value.toString());


  }



  return answer.toString();


  }



});



Client side:



function checkCart() {


  var fr = g_form.getValue('SelectedFR').toString(); // Reference field.


  var cart = new GlideAjax('CheckCart');


  cart.addParam('sysparm_name', 'checkCart');


  cart.getXMLWait();


  var resp = cart.getAnswer();


  alert(resp);


  if (resp.indexOf(fr) != -1) // If this value exists in their cart, abort.


  {


  alert('You may only submit one ownership change request per Functional Role, per order.');


  return false;


  }


  return true;


}



I am open to any critiques/comments.



Thank you!


It seems like you are trying to access a variable on the form. Is that right ? If so, why access sc_cart ? Can't you access the same in a onsubmit script and return false if the value is not satisfactory ? That would also stop the form submission .


Kalai,



I am accessing a value on the form. But I wanted to make sure a customer's cart does not contain an RITM for the same request with the same reference field value selected (Functional Role). That's why I make a call to the server to access the user's cart. Essentially, I only want to allow a given user to submit one RITM per REQ of the same Catalog Item where a specific value is chosen in a reference field.



Does that make sense?