Cart Duplicate Validation in ServiceNow Based on Requested For and Application Name

Sangeeta_Nayak
Tera Contributor

I have implemented a functionality in a ServiceNow catalog item where, when the user clicks the "Add to Cart" button, the system checks if an item with the same combination of requested_for and application_name already exists in the cart. If a duplicate is found, an error message appears: "You cannot add the same request to the cart without any changes." The functionality works as expected, ensuring duplicates are not added to the cart. However, if either requested_for is different while the application_name remains the same, or if application_name is different while the requested_for remains the same, or both fields differ, the item should be allowed to be added to the cart.

Additionally, the client requires a hidden key field to store the concatenation of requested_for and application_name. This key value is passed to the onSubmit client script to validate duplicates. The system should also be capable of handling unique combinations of requested_for, application_name, and possibly environment in the future.

Currently, this validation is working perfectly but triggers for both "Add to Cart" and "Order Now" buttons. I need help modifying the functionality so that it only triggers when the user clicks the "Add to Cart" button. The "Order Now" button should bypass the duplicate check and always proceed with the order.

 

Can someone guide me on how to ensure this functionality is restricted to only the "Add to Cart" button, while the "Order Now" button bypasses the duplicate check?

 

I have written the following Script Include for this functionality:

var CartDuplicateChecker = Class.create();
CartDuplicateChecker.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    validateCart: function() {
        // Get the key passed from the client script
        var key = this.getParameter('sysparm_key');
        gs.log("Key: " + key);

        var cartItem = new GlideRecord('sc_cart_item');
        cartItem.query();

        var isDuplicate = false;
        while (cartItem.next()) {
            var cartItemSysId = cartItem.sys_id;
            gs.log("Checking cart item with sys_id: " + cartItemSysId);
            var itemOption = new GlideRecord('sc_item_option');
            itemOption.addQuery('cart_item', cartItemSysId);
            itemOption.addQuery('value', key); // Check for the key match
            itemOption.query();

            if (itemOption.next()) {
                gs.log("Duplicate found for key: " + key);
                isDuplicate = true;
                break;
            }
        }

        gs.log("Returning result: " + isDuplicate);
        return isDuplicate.toString(); // Return true/false as a string
    }
});


And the onSubmit client script:

 

 

function onSubmit() {
    if (g_scratchpad._ajaxChecked) {
        g_scratchpad._ajaxChecked = null;
        return true;
    }
    var actionName = g_form.getActionName();
    alert(actionName);
    g_form.clearMessages();

    var requestedFor = g_form.getValue('requested_for');
    var applicationName = g_form.getValue('application_name');

    // Concatenate the values to create the key
    var key = requestedFor + '_' + applicationName;
    g_form.setValue('key', key);

    g_scratchpad._action = g_form.getActionName();
    g_scratchpad._ajaxChecked = false;

    var ga = new GlideAjax('CartDuplicateChecker');
    ga.addParam('sysparm_name', 'validateCart');
    ga.addParam('sysparm_key', key);

    ga.getXMLAnswer(function(response) {
        if (response) {
            var answer = response;
            if (answer === 'true') {
                g_form.addErrorMessage('You cannot add the same request to the cart without any changes.');
                g_scratchpad._ajaxChecked = false; // Reset the flag
            } else {
                g_scratchpad._ajaxChecked = true;
                g_form.submit(g_scratchpad._action);
            }
        } else {
            g_form.addErrorMessage("No response from server");
            g_scratchpad._ajaxChecked = false; // Reset the flag
        }
    });

    return false;
}

 

0 REPLIES 0