show and hide submit button using catalog ui policy script

Hafsa1
Mega Sage

I have written below code from community to hide submit button but it is giving issue when updating the case created. Is this code correct?

if(window == null){
        // for portal
        var z = this.document.getElementsByClassName("btn btn-primary btn-block ng-binding ng-scope");
        z[0].style.display = 'none';
    }else{
        // for native
        document.getElementById("submit_button").style.display  = 'none';
    }
 
and to unhide below code is used:
if(window == null){
        // for portal
        var z = this.document.getElementsByClassName("btn btn-primary btn-block ng-binding ng-scope");
        z[0].style.display = 'block';
    }else{
        // for native
        document.getElementById("submit_button").style.display  = 'block';
    }
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Hafsa1 

Code is fine. But to ensure DOM manipulation works ensure Isolate Script = False for your ui policy.

If this field is not on form then from list make it false

Didn't get what's the issue you are facing?

Are you sure your catalog UI policy is satisfying the condition?

why not use onChange catalog client script instead of Catalog UI policy?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

@Hafsa1 

Code is fine. But to ensure DOM manipulation works ensure Isolate Script = False for your ui policy.

If this field is not on form then from list make it false

Didn't get what's the issue you are facing?

Are you sure your catalog UI policy is satisfying the condition?

why not use onChange catalog client script instead of Catalog UI policy?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

MohanMallapu
Tera Contributor

Hi @Hafsa1 


Try this below code
if (typeof window !== 'undefined' && window.location) {
if (window.location.pathname.includes('/portal')) {
// For portal
var buttons = document.getElementsByClassName("btn btn-primary btn-block ng-binding ng-scope");
if (buttons.length > 0) {
buttons[0].style.display = 'none'; // Hides the first button with the specified class
}
} else {
// For native
var submitButton = document.getElementById("submit_button");
if (submitButton) {
submitButton.style.display = 'none'; // Hides the button with the specific ID
}
}
}

Vikram11
ServiceNow Employee
ServiceNow Employee

Hi @Hafsa1 

Ideally it is not recommended to have DOM manipulation this would be not a good solution from long term perspective.

It would be helpfull if you could elaborate your requirement