How can I get the variables in catalog task by writing emails script

MaramA
Tera Contributor

Hello,

I am trying to retrieve the values of the variables in the catalog task form. I am using the flowing 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_1-1716708814496.png

 

the main variable is:

MaramA_2-1716708843520.png

 

1 ACCEPTED SOLUTION

SN_Learn
Kilo Patron
Kilo Patron

Hi @MaramA ,

 

Please try the below code:

 

(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.

View solution in original post

2 REPLIES 2

SN_Learn
Kilo Patron
Kilo Patron

Hi @MaramA ,

 

Please try the below code:

 

(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.

MaramA
Tera Contributor

Thank you very much, it works perfectly