Help with Code

dvelloriy
Kilo Sage

Hello all,

I need some help in building a fix script/ scheduled job script for below requirement:

I have created a report on asmt metric table to capture the experience feedback of our newly launched portal. This report has 2 critical columns: Portal page & Portal page params.

Portal page: target where the feedback was recorded. it has values like kb_article, ticket, sc_cat_item etc.

Portal page params - all required portal page parameters in json.

Refer screenshot below.

 

Requirement: Parse portal page params, extract the target sys_id or sysparm_article and display the hyperlinked target record in a new column. So, if portal page is kb_article and portal page param has KB0010149 then we need to show the KB article number so management can click there and check the article. Similarly, if it's a cat item, we need to reference the cat item sys_id and show the exact cat item name not the sys_id.

This is needed for leadership and management teams to check the feedback they are getting and action accordingly.

 

Please advise.

 

dvelloriy_0-1725397389713.png

dvelloriy_1-1725397784587.png

 

 

1 ACCEPTED SOLUTION

I'd probably try to use Document ID (if you're not familiar, see: Document ID field - Did You Know) set by a business rule on insert/update. Just make sure you set the conditions (e.g. Metric = Experience feedback).

 

Based on the information you've presented, the business rule might look something like this:

 

(function executeRule(current, previous /*null when async*/) {

    // Get the portal page params
    var params = current.getValue('params');

    // Ensure it's not null or empty before parsing
    if (params) {
        try {
            // Try to parse the string as JSON
            var parsedParams = JSON.parse(params);

            // Extract the sys_id, id, and sysparm_article safely
            var sys_id = parsedParams.sys_id;
            var table;
            var id = parsedParams.id;
            var articleNumber = parsedParams.sysparm_article;

            // Case 1: If 'id' is 'ticket', use 'table' field
            if (id === 'ticket') {
                table = parsedParams.table; // 'table' contains the actual table name
            }
            // Case 2: If 'id' is not 'ticket', treat 'id' as the table name
            else if (id === 'kb_article' && articleNumber) {
                // Special case: If 'id' is 'kb_article', use 'kb_knowledge' and 'sysparm_article'
                table = 'kb_knowledge';
            } else {
                table = id; // For all other cases, use 'id' as the table name
            }

            // Variable to hold the GlideRecord result
            var record = null;

            // Case logic to detect the table and execute the query
            switch (table) {
                case 'sc_cat_item':
                    var catItemGr = new GlideRecord('sc_cat_item');
                    if (catItemGr.get(sys_id)) {
                        record = catItemGr;
                    }
                    break;

                case 'kb_knowledge':
                    // Special case: When using KB number instead of sys_id
                    if (articleNumber) {
                        var kbArticleGr = new GlideRecord('kb_knowledge');
                        kbArticleGr.addQuery('number', articleNumber);
                        kbArticleGr.query();
                        if (kbArticleGr.next()) {
                            record = kbArticleGr;
                        }
                    }
                    break;

                case 'sc_req_item':
                    var reqItemGr = new GlideRecord('sc_req_item');
                    if (reqItemGr.get(sys_id)) {
                        record = reqItemGr;
                    }
                    break;

                // Add more cases as needed

                default:
                    var genericGr = new GlideRecord(table);
                    if (genericGr.get(sys_id)) {
                        record = genericGr;
                    }
                    break;
            }

			// Set document properties
            if (record) {
                current.u_document = record.getUniqueValue();
				current.u_document_table = table;
            } else {
                gs.log('Experience Feedback: No record found for table: ' + table + ' and sys_id: ' + sys_id);
            }

        } catch (e) {
            gs.log('Experience Feedback: Error parsing JSON: ' + e.message);
        }
    } else {
        gs.log('Experience Feedback: params is null or empty');
    }

})(current, previous);

 

 

If you decide that you'd rather write to a string value, you would update this section:

			// Set document properties
            if (record) {
                current.u_reference = record.getDisplayValue();
            }

View solution in original post

6 REPLIES 6

Sheldon  Swift
ServiceNow Employee
ServiceNow Employee

Hi @dvelloriy - Where are users viewing this report? How do you envision a fix script or scheduled script execution will address this requirement?

Hi Sheldon, This is all managed via Experience feedback dashboard. Experience feedback dashboard (servicenow.com)

I tweaked the report and dot walked to include portal page and portal page params as some of the reports in the dashboards were not adding any value without the feedback comments and target records.

Intention is to create a scheduled job which will parse the portal page params and extract the target record on nightly basis and store the value in a new custom field. It will be awesome if we can hyperlink the target record so our leaders can just click on that and it will take them to the target record.

Hope this helps.

This is a potentially complex requirement, particularly because, to my knowledge, URL fields in don't support a display value—only the URL itself. If displaying a long URL (e.g., https://{instance_name}.service-now.com/esc?id={route}&table={table_name}&sys_id={sys_id}) in this format is not acceptable, you may need to create your own component to handle it. Alternatively, you could explore dynamically setting the value of a Document ID field, though handling the routing (incident => ticket, sc_request => order_status, etc.) might be challenging.

Thanks Sheldon. Can we atleast display the string value if not url? Like parsing the portal params, extracting sys id, finding the sys id in task table and displaying RITMxxxx as string value as an example. This will atleast give them something. Portal page params is of no use to them.