Issue with JSON.parse() in a scoped application

Brendan Hallida
Kilo Guru

Hi all,

I am creating a mail script that will add all of the impacted services and affected CI's to a list.  it contains 2 glide queries to the following tables:

  • Affected CIs: task_ci
  • Impacted Services: task_cmdb_ci_service 

I then populate 2 arrays and then parse them into template.prints for formatting.  The Affected CIs parsing is working fine, however, I get the following error regarding the Impacted Services parse.

Evaluator: org.mozilla.javascript.EcmaError: Unexpected token: J

Please note - this mail script is in the Major Incident application scope.

Here is the mailscript:

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */
    email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */
    event) {

    var affectedCIArray = [];
    var impactedArray = [];

    // Glide into the affected cis relationship table
    var affected = new GlideRecord('task_ci');
    affected.addQuery('task', current.incident_alert.source_incident.sys_id); // Need to work out how to get the incident sys_id dynamically
    affected.query();

    // Push affected cis to array
    while (affected.next()) {
        affectedCIArray.push(affected.ci_item.getDisplayValue());
    }

    // Glide into the impacted services relationship table
    var impacted = new GlideRecord('task_cmdb_ci_service');
    impacted.addQuery('task', current.incident_alert.source_incident.sys_id); // Need to work out how to get the incident sys_id dynamically
    impacted.query();

    // push impacted services to array
    while (impacted.next()) {
        impactedArray.push(impacted.cmdb_ci_service.getDisplayValue());
    }

    // logging
    gs.info('VF | affectedCIArray: ' + affectedCIArray);
    gs.info('VF | impactedArray: ' + impactedArray);

    template.print("<p>Affected CIs:</p>");

    for (var i = 0; i < affectedCIArray.length; i++) {
        // Uplift the use of the JSON parser to accommodate scoped applications
        var parser = new global.JSON();
        var ciParsed = JSON.parse(affectedCIArray[i]);
        gs.info('VF | ciParsed: ' + ciParsed);

        if (ciParsed != "") {
            template.print('<div style="line-height: 17pt;">');
            template.print(ciParsed);
            template.print('</div>');
        }
    }

    template.print("<br/>");

    template.print("<p>Impacted Services:</p>");

    // this for loop is not working
    for (var j = 0; j < impactedArray.length; j++) {
        gs.info('VF | impactedArray[j]: ' + impactedArray[j]);
       
        var servicesParsed = JSON.parse(impactedArray[j]);
        gs.info('VF | servicesParsed: ' + servicesParsed);
        if (servicesParsed != "") {
            template.print('<div style="line-height: 17pt;">');
            template.print(servicesParsed);
            template.print('</div>');
        }
    }
})(current, template, email, email_action, event);

It is odd, as with the logging, the line gs.info('VF | impactedArray[j]: ' + impactedArray[j]); is logging correctly, however, the error suggests that it is empty?

find_real_file.png

I have tested this in a background script by itself as well... I get the Same error.  below is the background script:

    var impactedArray = [];
    var impacted = new GlideRecord('task_cmdb_ci_service');
    impacted.addQuery('task', '1d8ea6b41b54b090166542ebdc4bcb87'); // Incident sysID.
    impacted.query();

    // push impacted services to array
    while (impacted.next()) {
        impactedArray.push(impacted.cmdb_ci_service.name);
    }

    // logging
    gs.info('impactedArray: ' + impactedArray);

       // this for loop is not working
    for (var j = 0; j < impactedArray.length; j++) {
        gs.info('impactedArray[j]: ' + impactedArray[j]);
        // Uplift the use of the JSON parser to accommodate scoped applications
        var servicesParser = new global.JSON();
       var servicesParsed = JSON.parse(impactedArray[j]);
        //gs.print('servicesParsed: ' + servicesParsed);
        //if (servicesParsed != "") {
            //gs.print('servicesParsed is working');
        //}
    }

I am stumped here.  I'm sure there is something minor I am missing, but for the life of me, I cannot find it haha.

1 ACCEPTED SOLUTION

From the logs it looks like you are trying to parse an array of strings. If that's the case then there is no need to parse it. Just loop through the array and output the string. Like so: var servicesParsed = impactedArray[j];

View solution in original post

5 REPLIES 5

DScroggins
Kilo Sage

Hi Brendan,

When in a scoped app you must specify global scope for JSON methods. So use this line for parsing the impacted services:

var servicesParsed = new global.JSON.parse(impactedArray[j]);

Hope this helps.

--David

Hi David,

Thanks for your reply.  I just tried that in my background script and it still returns the same error.

find_real_file.png

I just cannot see the difference between the two parts of the script, 1 works and 1 does not.

From the logs it looks like you are trying to parse an array of strings. If that's the case then there is no need to parse it. Just loop through the array and output the string. Like so: var servicesParsed = impactedArray[j];

mate... sometimes I get too tricky for myself.  I have been banging my head against the wall over this over the course of the weekend.  I reused this from another script I had, which required the parse. doh!  

Whilst I still have no idea why it worked for one and not the other, this indeed fixes my issue.  I wanted the output as stings, so have no need for the JSON parsing! 

Thanks mate, and have a great rest of the weekend!