It’s hard to believe that it’s been seven years since I wrote a post showing how you can use client scripts to manipulate the appearance of form UI action buttons. This solution has been used in countless ServiceNow instances to help add more clarity to form buttons and processes in the system. A few months ago, I decided to try and see if I could expand on this client script method a bit to add even more button flexibility with the addition of an option to include an icon along with the button text. Fortunately, with the addition of a new icon set in recent ServiceNow releases, this has become pretty simple to do…all while leveraging a consistent, nice-looking icon set.
Available icons
You’ve undoubtedly seen them in various parts of the system, but you may not have realized until now that there are over 250 icons available to choose from in ServiceNow! You can use these icons in various ways (client scripts, UI pages, UI macros, CMS and Service Portal), including with UI action buttons as I show here. All you have to do to show an icon is to add a class attribute with a name matching the icon you want to add.
ServiceNow had previously published a style guide that you could reference to view all of the icons (and corresponding names to reference them by). For the time being, you can access the icons in your instance by navigating to https://your_instance.service-now.com/styles/retina_icons/retina_icons.html
The following script can be used in any standard form client script. The key is the ‘transformButton’ function at the bottom. This could be included as a global UI script in your instance so that you don’t need to include it in every single client script. Once that’s in place (whether in a global UI script or in the client script itself) you can just call ‘transformButton’ with the appropriate parameters to change the button however you want. The parameters used are as follows…
- UI action name (Mandatory)
- Button background color (Optional)
- Button text color (Optional)
- Button icon name (Optional)
//Change the color of the 'Approve' button to green
transformButton('approve', '#77FF77', 'white', 'icon-success-circle');
//Change the color of the 'Reject' button to red
transformButton('reject', '#FF0022', 'white', 'icon-error-circle');
//Change the color of the 'Info' button to blue
transformButton('info_button', '#31708f', 'white', 'icon-info');
//Change the color of the 'Warning' button to yellow
transformButton('warning_button', '#f0ad4e', 'white', 'icon-warning-circle');
}
function transformButton(buttonID, buttonBackgroundColor, buttonTextColor, buttonIconName) {
try{
//Find the button(s) by ID and change the background color
$$('button[id=' + buttonID + ']').each(function(elmt) {
elmt.style.backgroundColor = buttonBackgroundColor;
if(buttonTextColor){
elmt.style.color = buttonTextColor;
}
if(buttonIconName){
elmt.addClassName(buttonIconName);
//Add some spacing between the icon and button label
elmt.innerHTML = ' ' + elmt.innerHTML;
}
});
}catch(e){}
}
This is awesome. Thanks for sharing.
Thanks Donte!
Fantastic post Mark.
Hi Mark,
As you mentioned, could you please extend how we can put the “transformButton” code in ui script and utilize from client side.
Thanks in advance,
Karthik
Sure. Just navigate to ‘System UI -> UI Scripts’ in your left nav. Create a new record there, paste in the ‘transformButton’ function from the script in this post and check the ‘Global’ checkbox. Once that’s done, you can remove the ‘transformButton’ function from any client script since it will already be available as part of the global UI script anywhere you need it. Here’s the SN documentation explaining further.
https://docs.servicenow.com/bundle/istanbul-servicenow-platform/page/script/client-scripts/concept/c_GlobalUIScripts.html
Thanks Mark. Not seen another instance with this before, been playing with it already and will be pushing to use it more and more
This is an SNGuru original so this is the only place you’ll find it to my knowledge. I hope it works well for you!
Another gem from my daily trolling of SN | Guru!
Thanks Mark!
Thanks Eric, glad you like it!
How can I get the button background color to use the Bootstrap color theme, like you have in your examples?
One way to get at any of the colors you can already see in the system is to install a free browser color picker plugin. For the specific example in the screenshot I’ve updated the script above so that you can see.
Hi Mark,
With Scoped application created with Studio (Istanbul Patch3-hotfix0a), the color did not apply on UI Action => we got a error into the Chrome console “Error Button color : TypeError: $$ is not a function”.
Is there a workaround with that issue ?
the original error message is “TypeError: $$ is not a function” => “Error Button color:” goes from my client script try catch function 😉
Solution found on the SNC community :
https://community.servicenow.com/thread/224200
Add the following scoped system property for your apps
Name: glide.script.block.client.globals
Type: true/false
Value: false
Where can i find the list of available icons by name in SNow? 🙂
ServiceNow has published a style guide that you can reference to view all of the icons (and corresponding names to reference them by).
You can view the style guide here: https://styleguide.service-now.com/styles/styleguide/guidelines_-_icons.html
Hmm, the style guide link doesn’t work anymore.
Thanks! I’ve updated the article. You can also view the icons within an instance by navigating to: https://your_instance.service-now.com/styles/retina_icons/retina_icons.html