How to hide submit button when items are present in the cart in employee service center portal form?

shrinivasprasa1
Tera Contributor

Hi Community,

 

We have a requirement, that submit button should disappear when items are not present in the cart, if there are no items in the cart then only it should visible.

 

appreciated for the quick response.

 

shrinivasprasa1_0-1724329548927.png

 

Thanks,

Shrinivasprasad

4 REPLIES 4

Sandeep Rajput
Tera Patron
Tera Patron

@shrinivasprasa1 You need a combination of a GlideAjax call + DOM manipulation to achieve this. Using GlideAjax, and a server side script include you can check if there any records for the user in sc_cart table. If the count is grater than 0 then you can use the DOM manipulation via your client script to show the submit button or hide it.

 

For DOM manipulation, you can take the reference of this community post https://www.servicenow.com/community/developer-forum/hide-disable-quot-add-to-cart-quot-on-portal-ba...

 

Hope this helps.

shrinivasprasa1
Tera Contributor

Hi @Sandeep Rajput ,

 

Could you please help with the code please.

 

Thanks,

Shrinivasprasad

@shrinivasprasa1 Here is the sample code to check

Client script.

function onLoad() {
    // Call a Script Include using GlideAjax to check if there are items in the cart
    var ga = new GlideAjax('CartCheck');
    ga.addParam('sysparm_name', 'checkCart');
    ga.getXMLAnswer(function(response) {
        var itemsInCart = parseInt(response);

        // If there are no items in the cart, hide the submit button
        if (itemsInCart === 0) {
            // Hide the submit button by setting its display style to 'none'
            document.getElementById('oi_order_now_button').style.display = 'none';
            document.getElementById('oi_request_button').style.display = 'none';
        }
    });
}
var CartCheck = Class.create();
CartCheck.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    checkCart: function() {
        var cart = new GlideRecord('sc_cart');
        cart.addQuery('user', gs.getUserID());
        cart.addActiveQuery();
        cart.query();

        if (cart.next()) {
            var cartItem = new GlideRecord('sc_cart_item');
            cartItem.addQuery('cart', cart.sys_id);
            cartItem.addActiveQuery();
            cartItem.query();

            return cartItem.getRowCount();
        }
        return 0;
    }

});

 

Hope this helps.

 

Hi @Sandeep Rajput ,

 

Thanks for the reply, i have written onload client script as below but it is not working, i have added the items in the cart stiil the submit button is showing us in the form.

 

shrinivasprasa1_0-1724418724948.png

shrinivasprasa1_1-1724418867647.png

Thanks,

Shrinivasprasad