- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-15-2024 09:02 AM
Hello Experts!
We have several business entities (PO Business Units) under one company. When items are ordered from the Service Catalog, each request must have items from one business entity only. For example: Item 1 has a business entity A in a variable. The next item, Item 2, must have the business entity A in the variable as well, and so on for all items in the one request.
The problem I am having is the catalog client script is putting Item 2 with business entity B into the cart that already has Item 1 with business entity A in the cart.
Here is my code:
function onSubmit() {
function validatePOBU() {
var newPOBU = g_form.getValue('po_business_unit');
var userID = g_user.userID;
var crt = 'default';
// Get the user's cart
var sc = new GlideRecord('sc_cart');
sc.addQuery('user', userID);
sc.addQuery('name', crt);
sc.query();
if (sc.next()) {
var item = new GlideRecord('sc_cart_item');
item.addQuery('cart', sc.sys_id);
item.query();
// Check options for each cart item
while (item.next()) {
var options = new GlideRecord('sc_item_option');
options.addQuery('cart_item.sys_id', item.sys_id);
options.query();
// Compare PO Business Units
while (options.next()) {
var cartPOBU = options.getValue('item_option_new');
alert('newPOBU '+ newPOBU + ' cartPOBU ' + cartPOBU);
if (newPOBU == cartPOBU) {
return true;
} else {
g_form.addErrorMessage('PO Business Units cannot be different in the same request! Last item not added to cart!');
return false;
}
}
}
}
return true; // If no issues found
}
// Example usage:
var isValid = validatePOBU();
if (isValid) {
g_form.addInfoMessage('All items have consistent PO Business Units.');
} else {
g_form.addErrorMessage('There are inconsistencies in PO Business Units.');
}
}
I realize that I should use GlideAjax for this, but I wanted to first get it working.
Thank you in advance,
Laurie
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-25-2024 01:50 PM
The solution is you must use GlideAjax. We just upgraded to Vancouver.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-18-2024 01:01 PM
I should add it is failing on the first GlideRecord:
if(sc.next()){
Additionally, I noticed the code works if I use the Try It button from the catalog item.
Any ideas?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-25-2024 01:50 PM
The solution is you must use GlideAjax. We just upgraded to Vancouver.