Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Custom Variable Editor

rah dev
Tera Contributor

Hi Community, Good Day, there is a requirement to show table field values in custom variable edtior, basically we want to show a custom variable editor in RITM and Custom table, that's why I cloned my Variable editor and apply the following code in
UI Macro.
<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<j2:if test="$[current.canRead()]">
<g:requires name="scripts/js_includes_catalog.js" includes="true"/>
<g:requires name="styles/${new CatalogCssSelector().getVariableCss()}" includes="true"/>
<g:if_polaris>
<g:then>
<g:requires name="styles/${new CatalogCssSelector().getPolarisVariableCss()}" includes="true"/>
</g:then>
</g:if_polaris>

<!-- Debugging client script to confirm loading -->
<g2:client_script type="text/javascript">
alert('Client script loaded successfully');
</g2:client_script>

<!-- Catalog Item Retrieval with Handling for Null -->
<g2:evaluate var="jvar_catalog_item">
function eval_cat_item() {
var cat_item = "";
var className = current.getRecordClassName();
gs.log("Record Class Name: " + className, "Jelly Script Debug");
if (className == "sc_req_item") {
cat_item = current.cat_item;
} else {
gs.log("No catalog item associated or created yet.", "Jelly Script Debug");
cat_item = "No item";
}
return cat_item;
}
eval_cat_item();
</g2:evaluate>

<!-- Custom Table Record Retrieval -->
<g2:evaluate var="jvar_custom_records" jelly="true">
var gr = new GlideRecord("u_test");
gr.addQuery("u_request_item", "e79b5c2fc32422104758b1fdd401310b");//for testing purpose i picked a single record's sysid, but need to show for all
gs.log("Querying custom table records", "Jelly Script Debug");
gr.query();
var records = [];
while (gr.next()) {
records.push({
field1: gr.u_field1.toString(),
field2: gr.u_field2.toString(),
field3: gr.u_field3.toString()
});
}
gs.log("Custom Table Records: " + JSON.stringify(records), "Jelly Script Debug");
records;
</g2:evaluate>

<!-- Conditional Rendering for Valid and Invalid Items -->
<j2:if test="$[jvar_is_catalog_item_valid]">
<div>Catalog Item is Valid</div>
<div data-sn-macro-sys-id="${jvar_macro_sys_id}">
<g2:client_script type="catalog_question_editor" catalogItem="$[jvar_catalog_item]"/>
<g:inline template="catalog_ui_policy.xml" />
<g2:render_component componentName="com.glideapp.servicecatalog.VEditor"/>
</div>
<!-- Display Custom Table Records in Input Form -->
<div>
<h3>Custom Table Record Input Form:</h3>
<j2:forEach var="record" items="$[jvar_custom_records]">
<label for="field1">Field 1:</label>
<input type="text" id="field1" value="${record.field1}"/><br/>
<label for="field2">Field 2:</label>
<input type="text" id="field2" value="${record.field2}"/><br/>
<label for="field3">Field 3:</label>
<input type="text" id="field3" value="${record.field3}"/><br/>
<hr/>
</j2:forEach>
</div>
</j2:if>
<j2:else>
<div>No catalog item created or associated yet.</div>
</j2:else>
</j2:if>
</j:jelly>

variable name:
when user click on order now of catalog item, RITM will generate and it should display values in variable editor, field 1 is "test1", field2
is "test2" and etc. as well as when i open test table, in that variable editor values must autopopulate.

rahdev_0-1743661412193.png

thanks

0 REPLIES 0