Skip to main content

ServiceNow Parse JSON Array

ServiceNow Parse JSON array

 

Example JSON response:

 

"task_instances": [

        {

            "dag_id": "create-federated-saml-app-poc-v1",

            "dag_run_id": "manual__2024-11-06T10:33:28.822918+00:01",

            "duration": 25.854218,

            "end_date": "2024-11-06T10:34:00.243529+00:01",

       }

       {

            "dag_id": "create-federated-saml-app-poc-v2",

            "dag_run_id": "manual__2024-11-06T10:33:28.822918+00:02",

            "duration": 25.854217,

            "end_date": "2024-11-06T10:34:00.243529+00:02",

       }

      {

            "dag_id": "create-federated-saml-app-poc-v3",

            "dag_run_id": "manual__2024-11-06T10:33:28.822918+00:03",

            "duration": 25.854216,

            "end_date": "2024-11-06T10:34:00.243529+00:03",

       }

 

],

    "total_entries": 10

}

 

 

Code:

 

function retrieveDetailedErrorLogs(grIntegrationAlert) {

    //--parameter: the u_integration_alerts entry where the error status was reported

    var dag_run_id = grIntegrationAlert.u_transaction_id;

    var DAG_ID = 'create-federated-saml-app-poc-v1';

    var sReturn = '';

    try {

        var r = new sn_ws.RESTMessageV2('Glidepath', 'GlidePathTaskInstances');

        r.setStringParameterNoEscape('dag_run_id', dag_run_id);

        r.setStringParameterNoEscape('dag_to_run', DAG_ID);

        //if the message is configured to communicate through ECC queue, either

        //by setting a MID server or calling executeAsync, one needs to set skip_sensor

        //to true. Otherwise, one may get an intermittent error that the response body is null

        r.setEccParameter('skip_sensor', true);

 

        var response = r.execute();

        var responseBody = response.getBody();

        var httpStatus = response.getStatusCode();

        sReturn = httpStatus;

        gs.print('httpStatus=' + httpStatus);

        if (httpStatus == 200) {

 

            var ojson = JSON.parse(responseBody);

            var taskInstances = ojson.task_instances; //--looking for 'success'

            gs.print('len=' + taskInstances.length);

            for (var i = 0; i < taskInstances.length; i++) {

                //var dag = jsonData.taskInstances[i];

                //gs.print(dag.dag_id);

               

                gs.print(taskInstances[i].dag_id)

            }

 

        }

 

    } catch (ex) {

        var message = ex.message;

        //gs.logError(this.log_prefix + 'retrieveDetailedErrorLogs:ERROR::' + message + ', ' + grIntegrationAlert.u_servicenow_id.number);

 

    }

    return sReturn;

}

Comments

Popular posts from this blog

ServiceNow check for null or nil or empty (or not)

Haven't tested these all recently within global/local scopes, so feel free to have a play! option 1 use an encoded query embedded in the GlideRecord , e.g.  var grProf = new GlideRecord ( 'x_cls_clear_skye_i_profile' ); grProf . addQuery ( 'status=1^ owner=NULL ' ); grProf . query (); even better use the glideRecord  addNotNullQuery or addNullQuery option 2 JSUtil.nil / notNil (this might be the most powerful. See this link ) example: if ( current . operation () == 'insert' && JSUtil . notNil ( current . parent ) && ! current . work_effort . nil ())  option 3 there might be times when you need to get inside the GlideRecord and perform the check there, for example if the code goes down 2 optional routes depending on null / not null can use gs.nil : var grAppr = new GlideRecord ( 'sysapproval_approver' ); var grUser = new GlideRecord ( 'sys_user' ); if ( grUser . get ( 'sys_id' , current . approver )){

Service Catalog: variable advanced reference qualifiers

Call a script include to apply a reference qualifier on a catalog item variable: - variable reference qualifier dependent on another variable selection, in this case a variable referencing sys_user (requested_for) On the catalog item form. variable name to apply ref qual filter : retail_equipment variable reference qualifier (on cmdb table ): javascript : new  refqual_functions (). lostStolen_getAssignedCIs (); client-callable script include ( refqual_functions)  function : lostStolen_getAssignedCIs : function (){         //--called from variable set client script, for lost/stolen request (service catalog)     gs . log ( current . variables . requested_for , 'retail_lostStolen_getAssignedCIs' );         return ( 'install_statusNOT IN8,7^owned_by=' + current . variables . requested_for );             //owned_by=1269b79937f1060041c5616043990e41^install_statusNOT IN8,7            },