Change OOTB VATopicsHelper Script Include to Show sc_req_item instead of sc_request.

Wardma
Tera Guru

Ive been racking my brain for a few days trying to understand where to change the sc_request variable to show the RITM instead of the REQ when the Virtual Agent lists out the Requests and Incidents. So far, Ive not been able to locate a single solution. Below is the OOTB script unmodified and I've tried changing the sc_request to sc_req_item. 

 

<?xml version="1.0" encoding="UTF-8"?><record_update table="sys_script_include"><sys_script_include action="INSERT_OR_UPDATE"><access>public</access><active>true</active><api_name>global.VATopicsHelper</api_name><caller_access/><client_callable>false</client_callable><description>Common helper functions used in the example topics</description><name>VATopicsHelper</name><script><![CDATA[var VATopicsHelper = Class.create();
VATopicsHelper.prototype = {
    initialize: function() {},

    getTableRecords: function(tblName, encQuery) {
        var records = [];

        var grCnt = new GlideRecord(tblName);
        if (encQuery != '')
            grCnt.addEncodedQuery(encQuery);
        grCnt.query();

        while (grCnt.next())
            records.push(grCnt.getUniqueValue());
        return records;
    },

    getUserOpenTicketsMessage: function(user, incQuery, hrQuery, reqQuery, portalName, userLanguage) {
        this.portalName = portalName;
        if (gs.nil(userLanguage))
            userLanguage = vaContext.getRequesterLang();
        var ints = this.getTableRecords('incident', incQuery);
        var reqs = this.getTableRecords('sc_request', reqQuery);
        var intCnt = ints.length;
        var reqCnt = reqs.length;
        var hrCnt = 0;
        var hrCases = [];
        if (!GlidePluginManager.isActive('com.sn_hr_core'))
            hrCnt = -1;
        else {
            try { //try catch, so any cross scope error for HR doesnt interrupt the flow
                hrCases = this.getTableRecords('sn_hr_core_case', hrQuery);
                hrCnt = hrCases.length;
            } catch (e) {
                gs.info("There was an error processing HR cases:" + e);
            }
        }

        var msg = '';

        var incUrl = this._getSPUrl('incident', incQuery, ints);
        var hrUrl = this._getSPUrl('sn_hr_core_case', hrQuery, hrCases);
        var reqUrl = this._getSPUrl('sc_request', reqQuery, reqs);

        var incLabel = (intCnt > 1) ? gs.getMessageLang('Incidents', userLanguage) : gs.getMessageLang('Incident', userLanguage);
        var hrLabel = (hrCnt > 1) ? gs.getMessageLang('HR Cases', userLanguage) : gs.getMessageLang('HR Case', userLanguage);
        var reqLabel = (reqCnt > 1) ? gs.getMessageLang('Requests', userLanguage) : gs.getMessageLang('Request', userLanguage);

        var tickets = [];
        if (intCnt > 0) {
            tickets.push(incUrl);
            tickets.push(intCnt.toFixed());
            tickets.push(incLabel);
        }
        if (hrCnt > 0) {
            tickets.push(hrUrl);
            tickets.push(hrCnt.toFixed());
            tickets.push(hrLabel);
        }
        if (reqCnt > 0) {
            tickets.push(reqUrl);
            tickets.push(reqCnt.toFixed());
            tickets.push(reqLabel);
        }

        if (tickets.length == 0)
            return '';

        var cnt = (tickets.length / 3);
        switch (cnt) {
            case 1:
                msg += gs.getMessageLang('ITSMVA_DynamicGreeting_1', userLanguage, tickets);
                break;
            case 2:
                msg += gs.getMessageLang('ITSMVA_DynamicGreeting_2', userLanguage, tickets);
                break;
            case 3:
                msg += gs.getMessageLang('ITSMVA_DynamicGreeting_3', userLanguage, tickets);
                break;
        }
        return msg;
    },

    _getSPUrl: function(table, filter, records) {
        var baseURL = gs.getProperty('glide.servlet.uri');
        var link = baseURL + this.portalName + '?id=list&table=' + table + '&filter=' + filter;
        if (records.length == 1)
            link = baseURL + this.portalName + '?id=form&table=' + table + '&sys_id=' + records.join();

        return link;
    },

    getOutages: function(outageTbl, encQuery, limit, userLanguage) {
        var message = '';
        if (!limit) limit = 3;
        if (gs.nil(userLanguage))
            userLanguage = vaContext.getRequesterLang();

        var grOutage = new GlideRecord(outageTbl);
        grOutage.addEncodedQuery(encQuery);
        if (limit > 0)
            grOutage.setLimit(limit);
        grOutage.orderByDesc('begin');
        grOutage.query();
        message += (grOutage.getRowCount() > 0) ? gs.getMessageLang('Service degradations I am currently aware of ', userLanguage) : '';
        while (grOutage.next()) {
            var msg = '<li>';
            var type = grOutage.getDisplayValue('type');
            var name = grOutage.getDisplayValue('cmdb_ci');
            var start_time = new GlideDateTime(grOutage.getValue('begin'));
            var end_time = grOutage.getValue('end');
            msg += gs.getMessageLang('{0} of {1} that began at {2}.', userLanguage, [type, name, start_time.getDisplayValue()]);
            if (end_time != '') {
                msg += gs.getMessageLang(' The {0} is expected to end at {1}.', userLanguage, [type, (new GlideDateTime(end_time)).getDisplayValue()]);
            }
            msg += '</li>';
            message += msg;
        }
        return message;
    },
    outagesInfo: function(outageTbl, encQuery, limit, userLanguage) {
        var message = [];
        if (gs.nil(userLanguage))
            userLanguage = vaContext.getRequesterLanguage();
        if (!limit) limit = 3;
        var grOutage = new GlideRecord(outageTbl);
        grOutage.addEncodedQuery(encQuery);
        if (limit > 0)
            grOutage.setLimit(limit);
        grOutage.orderByDesc('begin');
        grOutage.query();
        if (grOutage.getRowCount() > 0) {
            message.push(gs.getMessageLang('Service degradations I am currently aware of \n\n', userLanguage));
        }
        while (grOutage.next()) {
            var type = grOutage.getDisplayValue('type');
            var name = grOutage.getDisplayValue('cmdb_ci');
            var start_time = new GlideDateTime(grOutage.getValue('begin'));
            var end_time = grOutage.getValue('end');
            message.push("- ");
            message.push(gs.getMessageLang('{0} of {1} that began at {2}.', userLanguage, [type, name, start_time.getDisplayValue()]));
            if (end_time != '') {
                message.push(gs.getMessageLang(' The {0} is expected to end at {1}.', userLanguage, [type, (new GlideDateTime(end_time)).getDisplayValue()]));
            }
            message.push("\n\n");
        }
        return message.join('');
    },
    openTicketsInfo: function(deviceType, userLanguage, portalName) {
        var tables = [];
        var portal_pages = [];
        if (gs.nil(userLanguage))
            userLanguage = vaContext.getRequesterLang();
        if (gs.nil(portalName))
            portalName = vaVars.portalName;
        var records = this.getMyRequestSysIds(tables, portal_pages, deviceType);
        var recordsCount = records.length;
        var baseURL = gs.getProperty('glide.servlet.uri');
        var link = baseURL + portalName + '?id=my_requests';
        var reqLabel = (recordsCount > 1) ? gs.getMessageLang('Open Issues', userLanguage) : gs.getMessageLang('Open Issue', userLanguage);
        return gs.getMessageLang("You currently have [**{0} {1}**]({2})", userLanguage, [recordsCount.toFixed(), reqLabel, link]);
    },
    getMyRequestSysIds: function(tables, portal_pages, deviceType) {
        var ids = [];
        var rq_filter = new GlideRecord('request_filter');
        rq_filter.addActiveQuery();
        if (gs.nil(deviceType))
            deviceType = vaContext.deviceType;
        if (deviceType == 'android' || deviceType == 'ios') {
            // 100 Implies Mobile
            if (rq_filter.isValidField('applies_to'))
                rq_filter.addQuery('applies_to', 100);
        } else {
            // 1 Implies Service portal and 10 Implies Desktop/Service Portal
            if (rq_filter.isValidField('applies_to'))
                rq_filter.addQuery('applies_to', 1).addOrCondition('applies_to', 10);
        }
        rq_filter.query();
        while (rq_filter.next()) {
            var tableName = rq_filter.table_name;
            if (rq_filter.isValidField('table'))
                tableName = rq_filter.table;
            var gr = new GlideRecord(tableName);
            gr.addQuery(rq_filter.filter);
            gr.addActiveQuery();
            gr.query();
            while (gr.next()) {
                ids.push(gr.sys_id.toString());
                tables.push(rq_filter.table.toString());
                portal_pages.push(rq_filter.portal_page.id.toString());
            }
        }
        return ids;
    },
    openTicketsStatus: function(deviceType, userLanguage, portalName) {
        var statusMsg = [];
        if (gs.nil(userLanguage))
            userLanguage = vaContext.getRequesterLang();
        if (gs.nil(portalName))
            portalName = vaVars.portalName;
        var baseURL = gs.getProperty('glide.servlet.uri');
        var tables = [];
        var portal_pages = [];
        var records = this.getMyRequestSysIds(tables, portal_pages, deviceType);
        var recordsCount = records.length;
        for (var i = 0; i < recordsCount; i++) {
         // var statusLink = baseURL + portalName + '?id=' + portal_pages[i] + '&table=' + tables[i] + '&sys_id=' + records[i];
			var statusLink = baseURL + portalName + '?id=abbott_ticket' + '&sys_id=' + records[i];
            var statusGr = new GlideRecord(tables[i]);
            statusGr.get(records[i]);
            var statusRecordNumber = statusGr.number;
            var statusRecordShortDescription = statusGr.short_description;
            var statusRecordState = statusGr.state.getDisplayValue();
            if (tables[i] == 'sc_request') {
                var requestedItem = new GlideRecord('sc_req_item');
                requestedItem.addQuery('request', statusGr.sys_id);
                requestedItem.query();
                if (requestedItem.next()) {
                    statusRecordState = requestedItem.stage.getDisplayValue();
                }
            }

            if (statusRecordShortDescription != '') {
                statusMsg.push(gs.getMessageLang("[**{0}**]({1}) : **{2}** status is **{3}**", userLanguage, [statusRecordNumber, statusLink, statusRecordShortDescription, statusRecordState]));
            } else {
                statusMsg.push(gs.getMessageLang("[**{0}**]({1}) status is **{2}**", userLanguage, [statusRecordNumber, statusLink, statusRecordState]));
            }
        }
        return statusMsg.join('\n');
    },
    openTicketsCount: function(deviceType) {
        var tables = [];
        var portal_pages = [];
        var records = this.getMyRequestSysIds(tables, portal_pages, deviceType);
        return records.length;
    },
    type: 'VATopicsHelper'
};]]></script><sys_class_name>sys_script_include</sys_class_name><sys_created_by>admin</sys_created_by><sys_created_on>2020-03-03 22:44:35</sys_created_on><sys_id>e1bc2f8653170010c839ddeeff7b12f9</sys_id><sys_mod_count>30</sys_mod_count><sys_name>VATopicsHelper</sys_name><sys_package display_value="Service Management Virtual Agent Core" source="com.glideapp.sm_va_core">794c47c8db230110bc06287f059619a3</sys_package><sys_policy/><sys_scope display_value="Global">global</sys_scope><sys_update_name>sys_script_include_e1bc2f8653170010c839ddeeff7b12f9</sys_update_name><sys_updated_by>WARDMA</sys_updated_by><sys_updated_on>2023-05-17 19:33:52</sys_updated_on></sys_script_include></record_update>
1 ACCEPTED SOLUTION

After much research and investigation into the script include that calls the items in the fields, I found that regardless of the changes you make in the script include if you want to change the items that are displayed would be to use the service-now.com/request_filter_list.do and change the items one at a time unless you know exactly which one is being called for the item you want to change.

View solution in original post

11 REPLIES 11

Meghana3
Tera Contributor

Hi Wardma,

 

Could you please share the script here as I have same requirement and it is not working even after doing changes in the script include to display RITM instead of Requests

You'll need to locate the VATopicsHelper script include by navigating to; 
<yourServiceNowInstance>.Service-now.com/now/nav/ui/classic/params/target/sys_script_include_list.do
Alternatively, you can select the All filter and in the search input, type sys_script_include.list and enter. 

Once within the script include you'll want to comment bracket the default code and on the last just after the end comment bracket you can paste in the attached code.


You'll want to replace the destination variable with your destination. 

Did you get this to work? If not post below is more specific on where to make the necessary changes. 

PRS25
Tera Contributor

Could you please elaborate on this.

I did tested by updating the VATopicHelper Script Include, and it did not helped, as you mentioned.
However, i am not able to understand the updates to be made in request_filter.list

@PRS25 In the script include between 213 or 220 possibly yours may be different; 

if (tables[i] == 'sc_request') {
var requestedItem = new GlideRecord('sc_req_item');  //Here is where you change sc_request to sc_req_item
requestedItem.addQuery('request', statusGr.sys_id);
requestedItem.query();
if (requestedItem.next()) {
statusRecordState = requestedItem.stage.getDisplayValue();

In the request_filter.list there are three items that should look very similar to; 
Service Catalog Requested Item Portal
Service Catalog Request Portal
Service Catalog Requested Item

In the table field for each item specify, Request Item
Screenshot attached for reference.