Is it possible for a client script to check for a particular button on a form?

peter_foreman
Tera Guru

For example: I would like to test for the 'Apply Proposed Changes' Button on the Change form when the status changes.

1 ACCEPTED SOLUTION

poyntzj
Kilo Sage

Here is an old function I use to hide buttons for approval resets.


You could use something like this (create it as an onLoad Client script) and your onLoad calls the function as well as setting it.


Then create an onChange Client script that calls the same routine when something triggers the change to a field / status.



function resetRequestApproval() {



      var refs = document.getElementsByTagName("BUTTON");


      if (g_form.getValue('approval') == 'not requested')


      {


              // hide both "Request Reset" and "Reset Approval buttons"


              if (refs) {


                      for (i=0; i < refs.length; i++)


                      {


                              ref = refs[i];


                              buttonphrase = ref.innerHTML;


                              if (buttonphrase == '<span>Request Reset</span>')


                                      ref.style.display = 'none';


                              if (buttonphrase == '<span>Reset Approval</span>')


                                      ref.style.display = 'none';


                      }


              }


      }


      else


      {


              // Hide Reset Approval and show Request Reset


              if (refs) {


                      for (i=0; i < refs.length; i++)


                      {


                              ref = refs[i];


                              buttonphrase = ref.innerHTML;


                              if (buttonphrase == '<span>Request Reset</span>')


                                      ref.style.display = '';


                              if (buttonphrase == '<span>Reset Approval</span>')


                                      ref.style.display = 'none';


                      }


              }


   


      }


}


View solution in original post

6 REPLIES 6

Harish KM
Kilo Patron
Kilo Patron

Hi Peter, If u want to check the condition for the button..y dont u use ui action ?


Regards
Harish

alexandrabellog
ServiceNow Employee
ServiceNow Employee

H Peter,


are you meaning that you would like to have a conditional button? i.e. the button should you when the status changes to a particular status?



You could look at UI Policies:


Creating a UI Policy - ServiceNow Wiki


and at the UI Actions (editing and creating):


UI Actions - ServiceNow Wiki



Not sure if this blog could be of help:


Conditional Filters



Let me know if you need more help.


Kind regards,


Alexandra


arthikamugundan
Kilo Expert

Are you looking for something like the below


var action = g_form.getActionName();


It returns the name of the action on button click.


marcguy
ServiceNow Employee
ServiceNow Employee

there's a method I've used before to change the colour of a button which checks to see if the button is there, you need to find out the 'action_name' of the button and then swap, buttonID for your action_name value:



    try{


          //Find the button(s) by ID and change the background color


          $$('button[id=' + buttonID + ']').each(function(elmt) {


                elmt.style.backgroundColor = backgroundColor;


          });


    }catch(e){}



the reason I used that example is so you can see there is a way to check if the button exists, so you can return true rather than change the background



EDIT: I remember where I get this from now, the SNGURU site: http://www.servicenowguru.com/scripting/client-scripts-scripting/change-form-button-color/