How to add UI macro on choice field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-03-2024 10:07 AM
Hi Everyone,
Can anybody tell me how to add a UI macro to the choice field?
Thanks
Anshul
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-03-2024 10:17 AM
You cannot directly place a UI macro inside a choice field itself in ServiceNow, you can use a combination of client scripts, UI policies, and custom UI macros to dynamically show content based on the user’s selection in the choice field.
You can use following code for onChange client scripts:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
// Check the new value of the choice field
if (newValue == 'specific_value') {
// Dynamically inject the UI macro's content into a field or label
var macroContent = '<div>' + GlideUI.get('example_macro') + '</div>';
g_form.addInfoMessage(macroContent); // Add the UI macro as an informational message
} else {
g_form.clearMessages(); // Clear the message if other choices are selected
}
}
Regards
Shaqeel
***********************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting "Accept as Solution" and " Helpful." This action benefits both the community and me.
***********************************************************************************************************************
Regards
Shaqeel
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-03-2024 10:26 AM
Hi @Shaqeel, Thanks for the quick response! However, I want to add a UI macro beside the choice field. Is there a way to achieve it?
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-04-2024 12:17 AM
Follow this link:
Solved: Re: Create UI Macro Button next to Priority Field ... - ServiceNow Community
Regards
Shaqeel
***********************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting "Accept as Solution" and " Helpful." This action benefits both the community and me.
***********************************************************************************************************************
Regards
Shaqeel
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-04-2024 01:27 AM
@ShaqeelThank you for the detailed explanation. However, I want to add a UI macro to a choice field, like the "State" field on the incident form. In the link you provided, the "Priority" field is an integer type. I’ve noticed that UI macros can be added to integer and reference type fields, but I want to apply it to a choice field.
Thanks