How to write a client script to hide a UI Action (button) in Jakarta for a custom scoped application

Lon Landry2
Tera Contributor

After 8 hours; I am stumped trying to hide a button on a form.

I am aware of many solutions but none work in Jakarta for a custom scoped application.

It seems as though ServiceNow is moving away from custom scoped applications by limiting functionality and API availability.

Although I hope this is not the case...

I believe that I am limited to using the following APIs

  • GlideAjax
  • g_form
  • GlideDialogWindow

There is a condition, but I want to simply hide a button before moving on to more complicated tasks.

The end goal is hiding a button based on Session language settings.

The condition is based on browser settings (g_lang), but I could not figure out how to write an if / else statement in the UI Action Condition field.

This is an example of the (working) client script I use with g_lang:

function onLoad() {

    //Check session browser language settings

  //Display u_music_language field for English settings

  //Display u_music_idioma field for Spanish settings

if(g_lang == 'en'){

  g_form.setDisplay('u_music_language', true);

  g_form.setDisplay('u_music_idioma', false);

}

else if(g_lang == 'es'){

  g_form.setDisplay('u_music_language', false);

  g_form.setDisplay('u_music_idioma', true);

}

}

Thanks for your help and time,

1 ACCEPTED SOLUTION

You are very welcome. Thanks for participating in the community!


View solution in original post

14 REPLIES 14

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hello Lon,



Please refer blog post for reference. For a scoped app, you have to create an explicit system property to allow DOM manipulation.


How to hide/show an UI action on field changes



In the standard forms UI (eg client scripts, client ui actions) - by default, we block access to the DOM and certain top-level globals (window, document, $ / jquery). You can enable an application-specific property to gain access to the DOM.


You can enable an application-specific property to gain access to the DOM. This provides a good tradeoff by setting a default with best practice guard rails, and allowing a developer to take explicit action to opt out of the best practice.


To opt out of the best practice create the following property in your scope and set it to false. glide.script.block.client.globals


Hi Pradeep,


Thanks for the help clarifying this.


I had run across the need to create the system property; but was balking at the fact that this is opting out of best practices.


I am looking forward to seeing if the Developer (Owner) of the app is going to be cool with this.


Thanks again for your help and time,
Lon


Hi Pradeep,


I no longer have error messages, but the button still does not hide / show.


UI Action Name: Traducir


UI Action Action name: tecv


I have tried a Client Script and then a UI policy but neither work or return error / warning messages.
(Any ideas on what I am doing wrong?)



  1. Client Script - onChage

function onChange() {


var items = $$('tecv').each(function(item){


    if(item.innerHTML.indexOf('Traducir') > -1){


item.hide();  


    }


});


}



  1. UI Policy


Execute if true:


function onCondition() {


var buttons = document.GetElements.By.Class.Name('form_action_button action_context btn btn-default');


                              for (var i=0; i < buttons.length; i++){


                                                              if (buttons [i].id == 'tecv'){


                                                                                              buttons[i].hide();


                                                              }


                              }


}



Execute if false:


function onCondition() {


var buttons = document.GetElements.By.Class.Name('form_action_button action_context btn btn-default');


                              for (var i=0; i < buttons.length; i++){


                                                              if (buttons [i].id == 'tecv'){


                                                                                              buttons[i].show();


                                                              }


                              }


}



Any ideas on what I am doing wrong?


Hello Lon,



Can you please share the instance release info so that I can try on the same instance at my end.