4 Replies · Latest reply on Jul 19, 2017 11:18 PM by Michael Swan

    Export data when it changes to an outside URL.

      Hi,

       

      We are consolidating our reporting platforms into a single metrics reporting system in real time. We would like to get updates whenever the data changes.

      The update will be read by a php script on a different server, and will update the our tables that contain servicenow data.

       

      eg. Something like this

       

      if (change_request database updated)

      {

           // eg send the data to eg. www.myurl.com/datafromservicenow.php

           var r = new sn_ws.RESTMessageV2('Changed Data', 'post');

           r.body = "Data that has changed" ;

           var response = r.execute();

      }

       

      I’ve looked at the “outbound REST message” in Service now, but I don’t know how to attach the data ( ie a query that selects the changed data) to the outbound request.

      I don’t know how to make service now  schedule an outbound request on change.

      I don’t know how to test the outbound request – In the video example they had a “test link” that doesn’t appear ( http://wiki.servicenow.com/index.php?title=Outbound_REST_Web_Service#gsc.tab=0 )

       

       

       

      Any suggestion on the best way to go about this. I am very new with service now.

        • Re: Export data when it changes to an outside URL.
          Vipul Gupta

          Hi Michael,

           

          We can use Async Update BR to complete the above requirement. Please find the code for the BR below:

           

          var json = new global.JSON();

           

          //Display an information message for each change to the record

          var gru = GlideScriptRecordUtil.get(current);

          var changedFields = gru.getChangedFields(); //Get changed fields with friendly names

          var changedValues = gru.getChanges(); //Get changed field values

          //Convert to JavaScript Arrays

          gs.include('j2js');

          changedFields = j2js(changedFields);

          changedValues = j2js(changedValues);

           

          //Process the changed fields

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

               var val = changedValues[i];

               jsonData[changedFields[i]] = val.getDisplayValue();

          }

          var text = json.encode(result);

          try {

          var r = new sn_ws.RESTMessageV2('NAME', 'post');

           

          //override authentication profile

          //authentication type ='basic'/ 'oauth2'

          //r.setAuthentication(authentication type, profile name);

          r.setRequestBody(text);

          var response = r.execute();

          var responseBody = response.getBody();

          var httpStatus = response.getStatusCode();

          }

          catch(ex) {

          var message = ex.getMessage();

          }

           

           

          Please find the useful links below:

           

          https://www.servicenowguru.com/scripting/business-rules-scripting/checking-modified-fields-script/

          http://wiki.servicenow.com/index.php?title=Outbound_REST_Web_Service#gsc.tab=0

           

          Regards,

          Vipul

          1 of 1 people found this helpful
            • Re: Export data when it changes to an outside URL.
              Michael Swan

              Nearly right.

               

              changedFields and changedValues never contain any data.

               

              "result" didn't exist so I changed it to jsonData.

               

              The rest is right.

               

              function logObj(obj, sep){

                 for (var x in obj){

                    if (typeof obj[x] != "function"){

                       gs.log(sep + x + ":: " + obj[x]);

                    }

                    logObj(obj[x], sep + "*" );

                 }

              }

               

               

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

               

               

              // Add your code here

              var json = new global.JSON();

              gs.log("Entered");

               

              logObj(current, '*');

               

              //Display an information message for each change to the record

              var gru = GlideScriptRecordUtil.get(current);

              gs.log("Entered: 1");

              var changedFields = gru.getChangedFields(); //Get changed fields with friendly names

              gs.log('j : ' + changedFields.toString() + ' : ' + changedValues.toString() ); 

              //gs.log("Entered 2");

              var changedValues = gru.getChanges(); //Get changed field values

              gs.log('j2 : ' + changedValues.toString() + ' : ' + changedValues.toString() ); 

              //gs.log("Entered: 3");

               

              //Convert to JavaScript Arrays

              gs.include('j2js');

              gs.log("Entered: 4");

              changedFields = j2js(changedFields);

              gs.log("Entered: 5");

              changedValues = j2js(changedValues);

              gs.log("Entered: 6");

               

               

              var jsonData;

              //Process the changed fields

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

              var val = changedValues[i];

              jsonData[changedFields[i]] = val.getDisplayValue();

              gs.log(val.getDisplayValue());

              }

               

               

               

              var text = json.encode(jsonData);

              //gs.log(text);

              gs.log("Entered: 7");

               

              try {

              var r = new sn_ws.RESTMessageV2('name', 'post');

               

               

              //override authentication profile

              //authentication type ='basic'/ 'oauth2'

              //r.setAuthentication(authentication type, profile name);

              gs.log("RequestBody=" + JSON.stringify(text));

              r.setRequestBody(text);

              var response = r.execute();

              var responseBody = response.getBody();

               

              var httpStatus = response.getStatusCode();

              gs.log("httpStatus" + httpStatus);

              }

               

              catch(ex) {

              var message = ex.getMessage();

              gs.log("ERROR!");

              gs.log(message);

              }

               

               

              })(current, previous);

               

              As you can see the "j2" variable below is empty. When I dump everything in current you can see variables like work_start is being returned, which is one of the variable I changed.