How to add UI macro on choice field

anshul_goyal
Kilo Sage

Hi Everyone,

Can anybody tell me how to add a UI macro to the choice field?

Thanks
Anshul

5 REPLIES 5

Shaqeel
Mega Sage

Hi @anshul_goyal 

 

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

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

Hi @anshul_goyal 

 

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

@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