Retrieve values ​​from variables in a RITM

jkelvynsant
Tera Contributor

Hi, I'm trying to retrieve all the variables from a RITM using a script include to send all the item's variables via email as soon as it's created (I'm creating it via script include because all requests should generate this email), but I can't get their values. Can anyone figure out where I'm going wrong?

 

...
... 
getRecordVariables: function(record) {
            var varHtml = [];
            var tableName = record.getTableName();
            var recordSysId = record.getUniqueValue();

            try {  
                if (tableName === 'sc_req_item') {
                    varHtml = this._getVariablesFromRitmDirecto(recordSysId);
                    return varHtml.join('');
                }

--------------

 _getVariablesFromRitmDirecto: function(ritmSysId) {
            var varHtml = [];
            var gr = new GlideRecord("sc_item_option_mtom");
            gr.addQuery("request_item", ritmSysId);
            gr.query();
            while (gr.next()) {
                var question = gr.sc_item_option.item_option_new;
                var label = question.getDisplayValue("question_text");
                var value = question.getValue("value");
                if (label && value) varHtml.push('<b>' + label + ':</b> ' + value + '<br>');
            }
            return varHtml;
        },

 

4 REPLIES 4

Siddhesh Jadhav
Kilo Sage
Kilo Sage

Hi @jkelvynsant ,

 

You can use the below script to correctly fetch all RITM variables. It reads from sc_item_option_mtom, gets the variable definition, and then pulls the actual value from sc_item_option:

_getVariablesFromRitmDirecto: function(ritmSysId) {
    var varHtml = [];
    var mtom = new GlideRecord("sc_item_option_mtom");
    mtom.addQuery("request_item", ritmSysId);
    mtom.query();

    while (mtom.next()) {
        var question = mtom.sc_item_option.item_option_new;  
        var label = question.getDisplayValue("question_text");

        var opt = new GlideRecord("sc_item_option");
        if (opt.get(mtom.sc_item_option)) {
            var value = opt.value.toString();

            if (label && value)
                varHtml.push('<b>' + label + ':</b> ' + value + '<br>');
        }
    }
    return varHtml;
}

 

This will return all your catalog item variables in clean HTML format.

 

Thanks & Regards,

Siddhesh Jadhav


Please mark my answer as correct if it helped!

lauri457
Giga Sage

When you dot-walk in a gliderecord you get a GlideElement. The getValue() for GlideElement does not take arguments. For the value you've also dot-walked too deep into the question itself

If you want to use your method then see below how to access the value

while (gr.next()) {
    var question = gr.sc_item_option.item_option_new;
    var label = question.getDisplayValue(/*"question_text"*/);
    //var value = gr.getValue("value");
    var value = gr.sc_item_option.value.getValue();
    if (label && value) varHtml.push('<b>' + label + ':</b> ' + value + '<br>');
}

 But consider Scriptable service catalog variables to get values way easier

var current = new GlideRecord("sc_req_item");
if (current.get("382e979583f43e10557ff0d6feaad34e")) {
	var vars = current.variables.getElements();
	for (i in vars) {
		gs.info(gs.getMessage("Label:{0}, Value:{1}, DisplayValue:{2}", [vars[i].getLabel(), vars[i].getValue(), vars[i].getDisplayValue()]))
	}
}

  Or if you need more information on the variable you can use

var variables = new GlideappVariablePoolQuestionSet();
variables.setRequestID('382e979583f43e10557ff0d6feaad34e'); // RITM Sys ID
variables.load();
var vs = variables.getFlatQuestions();
for (var i = 0; i < vs.size(); i++) {
	gs.info(gs.getMessage("Label:{0}, Value:{1}, DisplayValue:{2}, Type:{3}", [vs.get(i).getLabel(), vs.get(i).getValue(), vs.get(i).getDisplayValue(), vs.get(i).getType()]))
}

 

Sarthak Kashyap
Mega Sage

Hi @jkelvynsant ,

 

Try below script in your script include

 

var varHtml = [];
var ritm = new GlideRecord("sc_req_item");

if (!ritm.get("f8d886d093a1ba189305f520ed03d615")) //Your sys_id here
    gs.print("varHtml = " + varHtml);

var vars = ritm.variables;

for (var v in vars) {
	gs.print("29 = " + vars[v]);

    var label = vars[v].getLabel();
    var value = vars[v].getDisplayValue();

    if (label) {
        varHtml.push('<b>' + label + ':</b> ' + value + '<br>');
    }
}

gs.print("varHtml = " + varHtml);
retrun varHtml;

 

Result

SarthakKashyap_0-1764663242074.png

 

ChallaR
Giga Guru

Hi @jkelvynsant ,

 

please find the updated code -

getRecordVariables: function(record) {
    var varHtml = [];
    var tableName = record.getTableName();
    var recordSysId = record.getUniqueValue();

    try {
        if (tableName === 'sc_req_item') {
            varHtml = this._getVariablesFromRitmDirect(recordSysId);
            return varHtml.join('');
        }
    } catch (e) {
        gs.error('Error retrieving variables: ' + e.message);
    }
    return '';
},

_getVariablesFromRitmDirect: function(ritmSysId) {
    var varHtml = [];
    var gr = new GlideRecord("sc_item_option_mtom");
    gr.addQuery("request_item", ritmSysId);

NOTE :

  1. Value Retrieval

    • value is on sc_item_option, not item_option_new.
    • So use gr.sc_item_option.value.
  2. Question Text Retrieval

    • item_option_new is a reference to the question definition.
    • Use getDisplayValue("question_text") on that reference.
  3. HTML Building

    • Use join('') at the end to return a single string.

Steps to set up email inbound -

  • Call getRecordVariables(current) in your Business Rule (after insert on sc_req_item).
  • Append the returned HTML to your email body.
  • Use gs.eventQueue() or GlideEmailOutbound to send the email.

 

Thanks,

Rithika.ch