{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":614,"path":"api-reference/scripts/c_UIMacros.md","title":"UI macros","bundle":null,"doc_type":null,"release":"australia","body":"\n# UI macros\n\nUI macros are discrete scripted components administrators can add to the user interface.\n\nUI macros are typically controls that provide inputs or information not provided by existing field types. By default, the system provides UI macros for a variety of user interface elements such as:\n\n-   All formatters\n-   The Service Catalog cart\n-   The action icons next to fields\n-   The action icons on forms and lists\n-   The widgets of a content management system\n-   The Orchestration activity designer\n\nAdministrators can create their own UI macros to provide custom controls or interfaces. Creating UI macros requires knowledge of [Jelly script](http://commons.apache.org/proper/commons-jelly/). Review the existing UI macros for examples and suggested approaches. Those who want to build custom interfaces with JavaScript technologies should consider Service Portal as an alternative.\n\n**Note:** To view available UI macros, navigate to **All** &gt; **System UI** &gt; **UI Macros**.\n\n**Parent Topic:**[Creating custom UI Pages and UI macros](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/scripts/create-custom-ui-pages.md)\n\n**Related topics**  \n\n\n[Service Portal](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/platform-user-interface/c_ServicePortal.md)\n\n[Create a UI macro for a formatter](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/platform-administration/t_CreateAUIMacro.md)\n\n## UI macro basics\n\nUI macros can be used to build solutions that can’t be built using the available catalog variable types.\n\n### Accessible UI macros\n\nSeveral UI macros are available in the UI macros \\[ui\\_macros\\] table. These UI macros have names starting with `ui_` and emulate the behavior of a subset of standard form field types for use in UI pages. For example, the ui\\_date\\_time UI macro can act like a glide\\_date\\_time field in a UI page, providing a type-able input field with a date/time selector.\n\nA UI macro can be included in a UI page with the &lt;g:&gt; Jelly tag. The following example includes the ui\\_date\\_time UI Macro, providing two optional attributes to the UI Macro:\n\n```\n<g:ui_date_time name=\"due_date\" value=\"2023-11-24 08:30:00\" onchange=\"checkDateValue()\" />\n```\n\nThe attribute values in the &lt;g:&gt; Jelly tag are provided to the UI macro as jvar-prefixed variables. The UI macro can use the jvar-prefixed variables in its XML.\n\n|Attributes|jvar-prefixed variables|\n|----------|-----------------------|\n|name|jvar\\_name|\n|value|jvar\\_value|\n|onchange|jvar\\_onchange|\n\n**Note:** These UI macros aren’t intended to support all features of their corresponding form field type. In many cases, the macros are only intended for specific ServiceNow application use cases.\n\nThe `ui_`-prefixed UI macros include descriptions specifying supported attributes that can \\(or must\\) be provided in the UI page's &lt;g:&gt; Jelly tag. To view available UI macros, navigate to **All** &gt; **System UI** &gt; **UI macros**.\n\n/\n\n### Custom UI macros\n\nThe ui\\_example UI macro uses three jvar-prefixed variables: jvar\\_name, jvar\\_test\\_attribute and jvar\\_laptop\\_type. These variables can be provided to the UI macro as name, test\\_attribute and laptop\\_type attributes in a &lt;g:&gt; Jelly tag in a UI page.\n\n-   **Creating the ui\\_example UI macro**\n\n    Navigate to **All** &gt; **System UI** &gt; **UI Macros** and select **New**.\n\n    Name the macro `ui_example` and add the following script to the **XML** field:\n\n    ```\n    <?xml version=\"1.0\" encoding=\"utf-8\" ?>\n    <j:jelly trim=\"true\" xmlns:j=\"jelly:core\" xmlns:g=\"glide\">\n        <div>name jvar: ${jvar_name}</div>\n        <div>test_attribute jvar: ${jvar_test_attribute}</div>\n        <div>laptop_type jvar: ${jvar_laptop_type}</div>\n    </j:jelly>\n    ```\n\n-   **Using the macro in a UI page**\n\n    Navigate to **All** &gt; **System UI** &gt; **UI Pages** and select **New**. You will be asked to select an administrator role.\n\n    Name the UI page and add the following script to the **HTML** field:\n\n    ```\n    <?xml version=\"1.0\" encoding=\"utf-8\" ?>\n    <j:jelly trim=\"false\" xmlns:j=\"jelly:core\" xmlns:g=\"glide\" xmlns:j2=\"null\" xmlns:g2=\"null\">\n        <div style=\"text-decoration:underline\">Template include one:</div>\n        <g:ui_example name=\"Fred Luddy\" test_attribute=\"I am an attribute\" laptop_type=\"ThinkPad\" />\n        <hr/>\n        <div style=\"text-decoration:underline\">Template include two:</div>\n        <g:ui_example name=\"Pat Casey\" test_attribute=\"I am a different attribute\" laptop_type=\"Macbook Pro\" />\n    </j:jelly>\n    ```\n\n-   **Checking output**\n\n    In the UI page, you can click **Try It** to view the results.\n\n\n**Note:** If you create a macro and it doesn’t display as expected in the UI page, clearing the cache might help. To clear the cache of your instance, in your browser, enter `<server_url>/cache.do`.\n\n**Related topics**  \n\n\n[UI pages](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/scripts/r_UIPages.md)\n\n[Jelly tags](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/scripts/r_JellyTags.md)\n\n[&lt;g:ui\\_form/&gt;](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/scripts/c_ExtensionsToJellySyntax.md)\n\n[&lt;g:ui\\_input\\_field /&gt;](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/scripts/c_ExtensionsToJellySyntax.md)\n\n[&lt;g:ui\\_checkbox/&gt;](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/scripts/c_ExtensionsToJellySyntax.md)\n\n## Calling UI macros\n\nAdministrators can call UI macros from certain record types associated with the user interface.\n\n<table id=\"table_j2v_pzq_rv\"><thead><tr><th>\n\nRecord type\n\n</th><th>\n\nExample\n\n</th></tr></thead><tbody><tr><td>\n\nDictionary attribute\n\n</td><td>\n\nDisplay an icon for a reference field:\n\n ```\nref_contributions=ui_macro_name\n```\n\n</td></tr><tr><td>\n\nUI page\n\n</td><td>\n\nDisplay something on a UI page:\n\n ```\n<g:macro_invoke macro=\"ui_macro_name\" />\n```\n\n</td></tr><tr><td>\n\nUI macro\n\n</td><td>\n\nCall a UI macro from another UI macro:\n\n ```\n<?xml version= \"1.0\" encoding=\"utf-8\"&nbsp;?>\n<j:jelly trim= \"false\" xmlns:j=\"jelly:core\" xmlns:g=\"glide\" xmlns:j2=\"null\" xmlns:g2=\"null\">\n  <g:ui_macro_name />\n  <g:ui_macro_name_2 />\n</j:jelly>\n```\n\n</td></tr></tbody>\n</table>## UI macro form\n\nEach UI macro record consists of a name and an XML document written in Jelly code.\n\n|Field|Description|\n|-----|-----------|\n|Name|A unique and descriptive name for this macro.|\n|Active|Select the check box to render the element as defined. Clear the check box to disable the element without deleting the code. For example, the **email\\_reply** macro is inactive by default.|\n|Description|Describe the purpose of the macro and parameters passed to it.|\n|XML|Jelly script that defines the macro.|\n\n## Custom approval UI macro\n\nThis section describes how to create a custom approval UI macro.\n\n**Warning:** The customization described here was developed for use in specific instances, and is not supported by Now Support. This method is provided as-is and should be tested thoroughly before implementation. Post all questions and comments regarding this customization to our community [forum](http://community.service-now.com/).\n\n**Script Name**: Custom Approval UI Macro\n\n**Type**: UI Macro\n\n**Description**: Here is an option to get more detail out of the My Approvals view of an Execution Plan. This can be done by creating a new UI macro. Navigate to **System UI** and click **UI Macros**. First, rename the existing `approval_summarizer_sc_task` to something like `approval_summarizer_sc_task_old` and deactivate it. Then create a new one using the same name \\(`approval_summarizer_sc_task`\\). The name should basically tell you what the macro does and what it applies to. In this case, we're replacing an existing one so we decided to re-use the existing name.\n\nCopy the XML script at the bottom of this article into the XML code window in the new UI macro. This is great way to give some detail to an approver when you are making line item approvals via approval tasks within the Service Catalog Execution Plans.\n\nThe following example script has a link into the item ordered, a short description \\(which contains the ability to expand the variables from the item\\), price, quantity and the total price. This helps the approver in that it shows more detail. They can now see what they are actually approving.\n\n```\n<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n    <j:jelly trim=\"true\" xmlns:j=\"jelly:core\" xmlns:g=\"glide\" xmlns:j2=\"null\" xmlns:g2=\"null\">\n    <tr>\n        <td class=\"label_left\" width=\"100%\">\n            <label style=\"margin-left: 10px\">\n                ${gs.getMessage('Summary of Item being approved')}:\n                <input style=\"visibility: hidden\" NAME=\"make_spacing_ok\"></input>\n            </label>\n        </td>\n    </tr>\n    <g:evaluate var=\"jvar_ni\" expression=\"\n        var sc_task = ${ref}.sysapproval;\n        var sc_req_labels = new GlideRecord('sc_req_item');\n        sc_req_labels.initialize();\n        var sc_req_item = new GlideRecord('sc_req_item');\n        sc_req_item.addQuery('request_item', sc_task.request_item.sys_id);\n        sc_req_item.query();\n    \" />\n    <tr>\n        <td width=\"100%\">\n            <table width=\"100%\">\n                <tr>\n                    <td class=\"label_left\" width=\"150px\">\n                        <label style=\"margin-left: 10px\">\n                            ${sc_task.request_item.request.opened_by.sys_meta.label}:\n                        </label>\n                    </td>\n                    <td>\n                        ${sc_task.request_item.request.opened_by.getDisplayValue()}\n                    </td>\n                </tr>\n                <tr> \n                    <td class=\"label_left\" width=\"150px\">\n                        <label style=\"margin-left: 10px\">\n                            ${sc_task.request_item.request.requested_for.sys_meta.label}:\n                        </label>\n                    </td>\n                    <td>\n                        ${sc_task.request_item.request.requested_for.getDisplayValue()}\n                    </td>\n                </tr>\n                <tr>\n                    <td class=\"label_left\" width=\"150px\">\n                        <label style=\"margin-left: 10px\">${gs.getMessage('Total')}:</label>\n                    </td>\n                    <td>\n                        <g:currency_format value=\"${sc_task.request_item.request.price}\" />\n                    </td>\n                </tr>\n            </table>\n        </td>\n    </tr>\n    <j:set var=\"jvar_line_num\" value=\"0\" />\n    <tr>\n        <td width=\"100%\">\n            <table width=\"100%\">\n                <tr class=\"header\">\n                    <td colspan=\"2\">\n                        ${sc_req_labels.number.sys_meta.label}\n                    </td>\n                    <td>\n                        ${sc_req_labels.description.sys_meta.label}\n                    </td>\n                    <td>\n                        ${sc_req_labels.price.sys_meta.label}\n                    </td>\n                    <td>\n                        ${sc_req_labels.quantity.sys_meta.label}\n                    </td>\n                    <td>\n                        ${gs.getMessage('Total')}\n                    </td>\n                </tr>\n                <j:set var=\"jvar_item_price\" value=\"${sc_task.request_item.price * sc_task.request_item.quantity}\"/>\n                <j:set var=\"jvar_overall_total\" value=\"${jvar_overall_total + jvar_item_price}\"/>\n                <j:set var=\"jvar_line_color\" value=\"odd\"/>\n                <j:set var=\"jvar_line_num\" value=\"${jvar_line_num + 1}\"/>\n                <j:if test=\"${jvar_line_num % 2 == 0}\">\n                    <j:set var=\"jvar_line_color\" value=\"even\"/>\n                </j:if>\n                <g:evaluate var=\"ni\" expression=\"\n                    var smart_description = sc_task.request_item.cat_item.short_description;\n                    if (smart_description == null || smart_description == '' || smart_description == 'undefined')\n                        smart_description = sc_task.request_item.cat_item.name;\n                \"/>\n                <tr class=\"${jvar_line_color}\">\n                    <td valign=\"top\">\n                        <g2:evaluate var=\"jvar_pop_target\" expression=\"$[ref].getRecordClassName()\" />\n                        <a class=\"linked\" target=\"gsft_main\"\n                            href=\"sc_req_item.do?sys_id=${sc_task.request_item.sys_id}\"\n                            onmousemove=\"popListDiv(event, 'sc_req_item', '${sc_task.request_item.sys_id}','${sysparm_view}')\"\n                            onmouseout=\"lockPopup(event)\">\n                            <img src=\"images/icons/hover_icon.gifx\" class=\"clsshort\"/>\n                        </a>\n                    </td>\n                    <td valign=\"top\">\n                        <a class=\"linked\" target=\"gsft_main\"\n                            href=\"sc_req_item.do?sys_id=${sc_task.request_item.sys_id}\">\n                            ${sc_task.request_item.number}\n                        </a>\n                    </td>\n                    <td valign=\"top\" width=\"50%\">\n                        <g:call function=\"variable_summary_approval.xml\" \n                            question_name=\"${sc_task.request_item.sys_id}\"\n                            question_help_tag=\"${smart_description}\" \n                            sc_req_item=\"${sc_task.request_item.sys_id}\"\n                            help_class=\"${jvar_line_color}\"/>\n                    </td>\n                    <td valign=\"top\"> <g:currency_format value=\"${sc_task.request_item.price}\"/> </td>\n                    <td valign=\"top\"> ${sc_task.request_item.quantity}</td>\n                    <td valign=\"top\"> <g:currency_format value=\"${jvar_item_price}\"/></td>\n                </tr>\n            </table>\n        </td>\n    </tr>\n</j:jelly>\n```\n\n"}}