How to fetch carriage return values and parse in script include

Nikithesh R
Tera Contributor

Hello,

 

I have a functionality in Service Portal for particular catalog item. When RITM is raised and from Actions UI button configured another UI button as Submit again. 

It is designed to copy values from existing RITM to create a new RITM with existing values copied. It is copied using script include and catalog Onload client script.

I have MRVS used for the catalog item, when values are fetched from Multi line text variable to a new request it is throwing error in browser console. The issue when line breaking statements entered in multi line text variable it is unable to copy to new request.

Can anyone help me on the script how to fetch line breaking statements from multi line text variable and copy to another multi line text variable?

 

Thanks in advance.

9 REPLIES 9

Hello @Community Alums ,

 

 

 

Catalog Client Script is
function onLoad() {
	
    //Type appropriate comment here, and begin script below
    var url = top.location.href;
    var ritm = new URLSearchParams(url).get("sysparm_ritm");
    var ritm_number = new URLSearchParams(url).get("sysparm_ritm_number");

    if (ritm && ritm_number) {
        var ritmGA = new GlideAjax('global.RITMSubmitAgain');
        ritmGA.addParam('sysparm_name', 'getRITMVariables');
        ritmGA.addParam('sysparm_ritm', ritm);
        ritmGA.addParam('sysparm_user', g_user.userID);
        ritmGA.getXML(ajaxParse);
    }

    function ajaxParse(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");

        if (answer == false) {
            return;
        }

        var variable_values = JSON.parse(answer);
        variable_values.forEach(function(vv) {
            g_form.setValue(vv.question_name, vv.value);

        });
    }

}

 

 

 

 

 

Script Include is
var RITMSubmitAgain = Class.create();
RITMSubmitAgain.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    isPublic: function() {
        return true;
    },

    getRITMVariables: function() {

        // ritm to copy from
        var ritm = this.getParameter("sysparm_ritm");

        // user attempting to copy from the ritm
        var usr = this.getParameter("sysparm_user");

        var ritmGR = new GlideRecord('sc_req_item');
        ritmGR.get(ritm);

        var payload = [];

        var ritmMRVSvarGR = new GlideRecord('sc_multi_row_question_answer');
        ritmMRVSvarGR.addQuery('parent_id', ritm);
        ritmMRVSvarGR.orderBy('variable_set');
        ritmMRVSvarGR.query();

        var mrvsList = [];

        while (ritmMRVSvarGR.next){
        mrvsList.push(ritmMRVSvarG.getValue('variable_set'));
		}

        // get the distinct list of MRVS
        mrvsList = new ArrayUtil().unique(mrvsList);

        // get the rows for each MRVS		
        mrvsList.forEach(function(mrvs) {
			
            // get the MRVS
            var variablesetGR = new GlideRecord('item_option_new_set');
            variablesetGR.get(mrvs);

            // get the values from the RITM
            var ritmMRVSvarGR = new GlideRecord('sc_multi_row_question_answer');
            ritmMRVSvarGR.addQuery('parent_id', ritm);
            ritmMRVSvarGR.addQuery('variable_set', mrvs);
            ritmMRVSvarGR.orderBy('row_index');
            ritmMRVSvarGR.query();

            //build the string for final the MRVS value
            var mrvsValue = '[{';
			
            // track the rows in the MRVS
            var mrvsRow = '';

            while (ritmMRVSvarGR.next()) {
                if (mrvsRow != '' && mrvsRow != ritmMRVSvarGR.getValue('row_index')) {
                    mrvsValue += '},{';
                } else if (mrvsRow != '') {
                    mrvsValue += ',';
                }
                mrvsRow = ritmMRVSvarGR.getValue('row_index');

                //get link from mrvs to question
                var optionGR = new GlideRecord('sc_item_option');
                optionGR.get(ritmMRVSvarGR.getValue('sc_item_option'));

                //get the question
                var itemoptionGR = new GlideRecord('item_option_new');
                itemoptionGR.get(ritmMRVSvarGR.getValue('item_option_new'));

                mrvsValue += '"' + itemoptionGR.getValue('name') + '" : "' + ritmMRVSvarGR.getValue('value') + '"';
            }

            mrvsValue += '}]';

            var item = {
                'question': variablesetGR.getUniqueValue(),
                'question_name': variablesetGR.getValue('internal_name'),
                'value': mrvsValue
            };

            payload.push(item);

        });

        var json = new global.JSON().encode(payload);
        return json;

    },

    type: 'RITMSubmitAgain'
});

 

 

 

The values from the field are fetching by script include, the issue is with line breaking statements while parsing values from value field.

Can you help me on this?

 

Thanks

Community Alums
Not applicable

Hi @Nikithesh R ,

 

Please find the below code-

Client Script

function onLoad() {
    var url = top.location.href;
    var ritm = new URLSearchParams(url).get("sysparm_ritm");
    var ritm_number = new URLSearchParams(url).get("sysparm_ritm_number");

    if (ritm && ritm_number) {
        var ritmGA = new GlideAjax('global.RITMSubmitAgain');
        ritmGA.addParam('sysparm_name', 'getRITMVariables');
        ritmGA.addParam('sysparm_ritm', ritm);
        ritmGA.addParam('sysparm_user', g_user.userID);
        ritmGA.getXML(ajaxParse);
    }

    function ajaxParse(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");

        if (answer == false) {
            return;
        }

        var variable_values = JSON.parse(answer);
        variable_values.forEach(function(vv) {
            g_form.setValue(vv.question_name, vv.value.replace(/\\n/g, "\n")); // Replace escaped newline characters with actual newlines
        });
    }
}

 

Script Include-

var RITMSubmitAgain = Class.create();
RITMSubmitAgain.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    isPublic: function() {
        return true;
    },

    getRITMVariables: function() {
        var ritm = this.getParameter("sysparm_ritm");
        var usr = this.getParameter("sysparm_user");

        var ritmGR = new GlideRecord('sc_req_item');
        ritmGR.get(ritm);

        var payload = [];

        var ritmMRVSvarGR = new GlideRecord('sc_multi_row_question_answer');
        ritmMRVSvarGR.addQuery('parent_id', ritm);
        ritmMRVSvarGR.orderBy('variable_set');
        ritmMRVSvarGR.query();

        var mrvsList = [];

        while (ritmMRVSvarGR.next()){
            mrvsList.push(ritmMRVSvarGR.getValue('variable_set'));
        }

        mrvsList = new ArrayUtil().unique(mrvsList);

        mrvsList.forEach(function(mrvs) {
            var variablesetGR = new GlideRecord('item_option_new_set');
            variablesetGR.get(mrvs);

            var ritmMRVSvarGR = new GlideRecord('sc_multi_row_question_answer');
            ritmMRVSvarGR.addQuery('parent_id', ritm);
            ritmMRVSvarGR.addQuery('variable_set', mrvs);
            ritmMRVSvarGR.orderBy('row_index');
            ritmMRVSvarGR.query();

            var mrvsValue = '[{';
            var mrvsRow = '';

            while (ritmMRVSvarGR.next()) {
                if (mrvsRow != '' && mrvsRow != ritmMRVSvarGR.getValue('row_index')) {
                    mrvsValue += '},{';
                } else if (mrvsRow != '') {
                    mrvsValue += ',';
                }
                mrvsRow = ritmMRVSvarGR.getValue('row_index');

                var optionGR = new GlideRecord('sc_item_option');
                optionGR.get(ritmMRVSvarGR.getValue('sc_item_option'));

                var itemoptionGR = new GlideRecord('item_option_new');
                itemoptionGR.get(ritmMRVSvarGR.getValue('item_option_new'));

                var value = ritmMRVSvarGR.getValue('value').replace(/\n/g, "\\n"); // Escape newlines
                mrvsValue += '"' + itemoptionGR.getValue('name') + '" : "' + value + '"';
            }

            mrvsValue += '}]';

            var item = {
                'question': variablesetGR.getUniqueValue(),
                'question_name': variablesetGR.getValue('internal_name'),
                'value': mrvsValue
            };

            payload.push(item);
        });

        var json = new global.JSON().encode(payload);
        return json;
    },

    type: 'RITMSubmitAgain'
});

 

Please update the scripts as above and verify if line break are not causing the issue.

 

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

Hello @Community Alums ,

 

There are other type variables in the variable set. While fetching the data will it impact other type of variables?

I have updated the script but it is not fetching the data.

 

Thanks

Nikithesh R

Community Alums
Not applicable

Hi @Nikithesh R ,

 

The client script remains same

Here is the updated script include to accomplish-

var RITMSubmitAgain = Class.create();
RITMSubmitAgain.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    isPublic: function() {
        return true;
    },

    getRITMVariables: function() {
        var ritm = this.getParameter("sysparm_ritm");
        var usr = this.getParameter("sysparm_user");

        var ritmGR = new GlideRecord('sc_req_item');
        ritmGR.get(ritm);

        var payload = [];

        // Fetch variables from the RITM
        var ritmVarGR = new GlideRecord('sc_item_option_mtom');
        ritmVarGR.addQuery('request_item', ritm);
        ritmVarGR.query();

        while (ritmVarGR.next()) {
            var questionName = ritmVarGR.sc_item_option.item_option_new.name;
            var value = ritmVarGR.value;

            // Escape newlines in multi-line text variables
            if (ritmVarGR.sc_item_option.item_option_new.type == '12') { // 12 is the type for multi-line text
                value = value.replace(/\n/g, "\\n");
            }

            var item = {
                'question_name': questionName,
                'value': value
            };

            payload.push(item);
        }

        var json = new global.JSON().encode(payload);
        return json;
    },

    type: 'RITMSubmitAgain'
});

Hello @Community Alums ,

 

The values stored in sc_multi_row_question_answer right. I have updated with latest script and added log statements in Script include. I don't see any values fetched.