Issue Retrieving Variable Values in Catalog Task Form

MaramA
Tera Contributor

Hello,
I am trying to retrieve the values of the variables in the catalog task form. I am using a flow script, but instead of returning the values of the variables, it is giving me something like an ID (as shown in the picture).

 

script:

(function runMailScript(current, template, email, email_action, event) {
    // Retrieve and format the catalog task variables for display
    var formattedVariables = getTaskVariableResponse(current);
    template.print(formattedVariables);
})(current, template, email, email_action, event);

function getTaskVariableResponse(taskGr) {
    var formattedList = [];
    if (taskGr.request_item) {
        var optionRecord = new GlideRecord('sc_item_option_mtom');
        optionRecord.addQuery('request_item', taskGr.request_item.sys_id);
        optionRecord.query();
        while (optionRecord.next()) {
            var displayName = optionRecord.sc_item_option.item_option_new.getDisplayValue();
            var value = optionRecord.sc_item_option.value;
            // Ensure that both display name and value are not null before appending
            if (displayName !== null && value !== null) {
                formattedList.push(displayName + ': ' + value);
            }
        }
    }
    return formattedList.join('<br>');
}

MaramA_0-1716705439092.png

the main variable"

MaramA_1-1716705467146.png

 

 

2 REPLIES 2

SN_Learn
Kilo Patron
Kilo Patron

Hi @MaramA ,

 

Please try the below:

(function runMailScript(current, template, email, email_action, event) {
    // Retrieve and format the catalog task variables for display
    var formattedVariables = getTaskVariableResponse(current);
    template.print(formattedVariables);
})(current, template, email, email_action, event);

function getTaskVariableResponse(taskGr) {
    var formattedList = [];
    if (taskGr.request_item) {
        var optionRecord = new GlideRecord('sc_item_option_mtom');
        optionRecord.addQuery('request_item', taskGr.request_item.sys_id);
        optionRecord.query();
        while (optionRecord.next()) {
            var displayName = optionRecord.sc_item_option.item_option_new.getDisplayValue();
            var value1 = optionRecord.sc_item_option.value;

            var sysUser = new GlideRecord('sys_user');
            if (sysUser.get(value1)) {
                var value = sysUser.name;
            }

            // Ensure that both display name and value are not null before appending
            if (displayName !== null && value !== null) {
                formattedList.push(displayName + ': ' + value);
            }
        }
    }
    return formattedList.join('<br>');
}

 

Please mark this response as correct or helpful if it assisted you with your question.

----------------------------------------------------------------
Mark this as Helpful / Accept the Solution if this helps.

Community Alums
Not applicable

Hi @MaramA ,

 

The issue in the above script that it is not handling reference type variables correctly. When retrieving variables from the sc_item_option_mtom table, the script is returning the internal sys_id values for reference fields instead of their display values. This is why you see IDs rather than the actual values you expected.

 

Here is the better approach-

(function runMailScript(current, template, email, email_action, event) {
    // formatting the catalog task variables for display
    var formattedVariables = getTaskVariableResponse(current);
    template.print(formattedVariables);
})(current, template, email, email_action, event);

function getTaskVariableResponse(taskGr) {
    var formattedList = [];
    if (taskGr.request_item) {
        var optionRecord = new GlideRecord('sc_item_option_mtom');
        optionRecord.addQuery('request_item', taskGr.request_item.sys_id);
        optionRecord.query();
        while (optionRecord.next()) {
            var displayName = optionRecord.sc_item_option.item_option_new.getDisplayValue();
            var value = optionRecord.sc_item_option.value;
            
            // get the display value of the option if it is a reference
            var actualValue = getDisplayValueForOption(optionRecord.sc_item_option);

            // Ensure that both display name and value are not null before appending
            if (displayName !== null && actualValue !== null) {
                formattedList.push(displayName + ': ' + actualValue);
            }
        }
    }
    return formattedList.join('<br>');
}

function getDisplayValueForOption(optionGr) {
    var value = optionGr.value;
    var type = optionGr.item_option_new.type; // Determine the type of variable
    
    // Handling reference type variables
    if (type == 'reference') {
        var refGr = new GlideRecord(optionGr.item_option_new.reference);
        if (refGr.get(value)) {
            return refGr.getDisplayValue();
        }
    }
    return value; // Return the value for non-reference types
}

 

The function getDisplayValueForOption has been added to handle and fetch display values for reference fields.

 

If my response has resolved your query, please consider giving it a thumbs up ‌‌ and marking it as the correct answer‌‌!

 

Thanks & Regards,

Sanjay Kumar