How to programmatically disable/enable Order Now and Add to Cart buttons in the ServicePortal?

colleyloyej
Tera Contributor

I made a function that works well for toggling the Add To Cart and Order Now buttons during my onSubmit():

function toggleSubmitButtons(state) {
    var index;
    var cartBtns = document.getElementsByName('add_to_cart');
    for (index = 0; index < cartBtns.length; index++) {
        cartBtns[index].style.display = state;
    }

    var saveBtns = document.getElementsByName('save_item');
    for (index = 0; index < saveBtns .length; index++) {
        saveBtns[index].style.display = state;
    }

    this.document.getElementsById('submit-btn').style.display = state;
}

  State == 'none' or '' for disable/enable.

 

This works but seems to give me the warning: This catalog client script is not VA supported due to the presence of the following variables in the script: ... ... .. 

 

It does not like the DOM manipulation.

 

I found many links explaining that they can be disabled.. such as this: https://www.servicenow.com/community/itsm-forum/how-to-hide-quot-order-now-quot-button-for-a-one-cat...

 

How can I access those values via g_form or some other mechanism that won't give me the VA support warning? I want to programmatically enable/disable 'Add to Cart' and 'No Order Now'.

3 REPLIES 3

VikMach
Mega Sage

Hi @colleyloyej,

You need to use g_form.getFormElement().
Example script that you created. I just randomly made adjustments.
Test and correct it to make it work.

function toggleSubmitButtons(state) {
    var cartBtns = g_form.getFormElement().querySelectorAll('[name="add_to_cart"]');
    cartBtns.forEach(function(btn) {
        btn.style.display = state;
    });

    var saveBtns = g_form.getFormElement().querySelectorAll('[name="save_item"]');
    saveBtns.forEach(function(btn) {
        btn.style.display = state;
    });

    var submitBtn = g_form.getFormElement().querySelector('#submit-btn');
    if (submitBtn) {
        submitBtn.style.display = state;
    }
}


Let me know if it worked.

Regards,
Vikas K

colleyloyej
Tera Contributor

Hi Vikas: Thanks I'll give that try tomorrow

colleyloyej
Tera Contributor

Hi Vikas: I tried that but still get the warning: This catalog client script is not VA supported due to the presence of the following variables in the script: g_form.getFormElement.QuerySelector.

 

Also it seems to give a console error: Cannot read properties of undefined (reading 'querySelector')

 

Does Virtual Agent support really mean much? I don't even think we use it much if at all.