Dynamic Form-Field (Jelly UI Macro)
Sometimes there will be a requirement to show a dynamic "field" on a form. This could be used to show custom UI for the current user or for the record.
From the technical side, you would use a UI Formatter, which basically calls a UI Macro in-line when included in the form. These formatters will be visible in the Form Designer.
Below is how I set one up in my personnel developer instance.
Creating the UI Macro
UI Macros are written in Jelly-Script, which can be somewhat of a learning curve for some. They can include tags and use jQuery or g_form like the rest of client scripts.</p> <p>Jelly uses `evaluate` scripts that run server-side before the macro renders, so if you need to add the current caller’s details, this would need to be done using a client script, perhaps as an onChange on a caller field.</p> <p>Here’s some jelly to get you started, which adds a rudimentary form field with user initials in it. <u>(using gs.getUser(). ie. the current user, not caller)</u></p> <pre><code class="markup"><?xml version="1.0" encoding="utf-8" ?> <j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null"> <div class="custom-form-group"> <g:evaluate var="jvar_user_fullname" jelly="true"> var user = JSUtil.escapeAttr(gs.getUser().getFullName()); user; </g:evaluate> <style> .custom-form-group { overflow: hidden; } .custom_caller_icons { height: 60px; } </style> <div data-type="label"> <label for="caller_details" dir="ltr" class="col-xs-12 col-md-3 col-lg-4 control-label"> <span title="information relating to the caller" class="label-text">Caller Details</span> </label> </div> <div class="col-xs-10 col-sm-9 col-md-6 col-lg-5 form-field input_controls custom_caller_icons"> <!-- you could populate this using a client-script (onchange perhaps)--> <img style="height: 60px;" height="60" src="https://ui-avatars.com/api/?name=${jvar_user_fullname}" /> </div> </div> </j:jelly> </code></pre> <h2>Creating the UI Formatter</h2> <p>UI Formatters can be created under the "System UI" -> "Formatters" module.</p> <p>When creating the formatter, the "Formatter" field must include the UI Macro you created previously, as "<ui\_macro\_name>.xml".</p> <p>Choose the table that the formatter will be added to, and give the formatter a unique name.</p> <h2><a href="https://docs.servicenow.com/bundle/helsinki-servicenow-platform/page/administer/form-administration/task/t%5FCreateAFormatter.html?ref=blog.tgxn.net" target="_blank" rel="noopener">Adding the Formatter to the Form.</a></h2> <p>On the table that you selected when creating the UI Formatter, navigate to the form designer, or form layout tool.</p> <p>Your UI Formatter should be visible in the list, with the other Formatters, this can be dragged onto the form like any other field/formatter.</p> <h2>Final Result</h2> <p><img src="https://blog.tgxn.net/content/images/2018/11/image-1.png" alt=""></p> <p>The UI Formatter should be visible on the modified form layout</p>
https://blog.tgxn.net/dynamic-form-field-jelly/