logo

NJP

Generating a Catalog Item Variables summary

Import · Apr 06, 2020 · article

Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

Hi there,

When working with Catalog Items, and which have Variables, at a certain point you'll cross a question on how to write those Variables to a Short Description, a Notification, etcetera. There are several ways to achieve this, and loads of Community answers can be found. Though the majority of the answers concern large scripting, hardcoded Variable names, etcetera.

So could this be done more dynamically? And with as limited information possible, only knowing what the Requested Item is?

Current or GlideRecord

Depending on where you would like to get your hands on the Variables, you are on the current record or you would need to perform a GlideRecord query.

If a GlideRecord query needed, it could be something like:

var catitemSysId = current.getValue('request_item');

var grRITM = new GlideRecord('sc_req_item');
grRITM.get(catitemSysId);

Obviously change "current.getValue('request_item')" if needed. This could also be a hardcoded sys_id.

Variables (Variable Pool)

Variables for submitted Catalog Items and Record Producers are stored in multiple tables. sc_item_option and sc_item_option_mtom for Catalog Items and question_answer for Record Producers. We could query these tables, though there's a shortcut: ".variables" (or from the old days, still works, ".variable_pool").

For example: current.variables or grRITM.variables.

Looping through Variables

With .variables we could directly access all variables. For example if there's a Variable phone_number, "grRITM.variables.phone_number". With this, we could already create a script that contains all Variables. Though, these variable labels and names would then all be hardcoded, not very dynamic or maintainable.

Also for this, there's a solution.

Looping through the Variables would be possible with using "key in grRITM.variables" or "grRITM.variables.getElements()". We then dimply need to add the usage of ".getLabel()" and ".getDisplayValue()".

(function() {

    var catitemSysId = current.getValue('request_item');

    var grRITM = new GlideRecord('sc_req_item');
    grRITM.get(catitemSysId);

    var summaryStr = '';
    for(var key in grRITM.variables) {
        summaryStr += grRITM.variables[key].getLabel() + ": " + grRITM.variables[key].getDisplayValue() + "\n";
    }

    gs.info(summaryStr);

})();

Result

When applying this code in a Notification Email Script, a Business Rule, etcetera, this would generate a summary of all Variables on the Requested Item queried.

---

And that's it actually. Hope you like it. If any questions or remarks, let me know!

Kind regards,

Mar k Roethof

ServiceNow Technical Platform Architect @ Quint Technology

2x ServiceNow Developer MVP

2x ServiceNow Community MVP

---

View original source

https://www.servicenow.com/community/developer-articles/generating-a-catalog-item-variables-summary/ta-p/2321038