How to Check Variable on Catalog Item in Cart with Variable on Item Ready to Add to Cart

Laurie Marlowe1
Kilo Sage

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

1 ACCEPTED SOLUTION

The solution is you must use GlideAjax.  We just upgraded to Vancouver.

View solution in original post

2 REPLIES 2

Laurie Marlowe1
Kilo Sage

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?

The solution is you must use GlideAjax.  We just upgraded to Vancouver.