3 Replies Latest reply on Mar 28, 2018 6:09 AM by Manish Patil

    VRASNImportCMDB - bug in code??? ; marking CI records RETIRED

    draymond10 Enthusiast

      **thanks to my ServiceNow developers...

       

      var numberOfPages = parsed.metadata["totalPages"];

      for(var i=1; i<numberOfPages; i++){

       

      If total pages = 3

      and our resource is listed on page 3,

      This loop will check pages 1 and 2 (missing page 3 because the criteria should be i<=numberOfPages)

       

       

      Full function below:

      reconcileCMDB : function() {

      try{

      VRASNLogger.debug('Inside  VRASNImportCMDB.reconcileCMDB: used to Reconcile the CMDB CI records');

      var midServer = gs.getProperty('vrasn.midserver.name');

      var endPoint = gs.getProperty('vrasn.end.point');

      var _endPoint = endPoint+"/catalog-service/api/consumer/resources?withExtendedData=false&page=1&limit=20";

      var request = new sn_ws.RESTMessageV2();

      request.setEndpoint(_endPoint);

      request.setHttpMethod('get');

      var auth = new VRASNPropertiesUtil();

      var authGen = auth.getSNProperties('AuthToken');

      request.setRequestHeader("Accept", "application/json");

      request.setRequestHeader("Authorization", "Bearer "+authGen);

      request.setRequestHeader("Content-Type", "application/json");

      request.setMIDServer(midServer);

      var ciList = "";

      var gotException = false;

      var resourceID="";

      var response = request.execute();

      if(response.haveError()) {

      VRASNLogger.error('REST call error found inside VRASNImportCMDB.reconcileCMDB for pagination: '+ response.getErrorMessage());

      this.errorHandler.notify("REST call error found inside VRASNImportCMDB.reconcileCMDB for pagination: "+ response.getErrorMessage());

      gotException = true;

      }

      else {

      var responsedata = response.getBody();

      var parser = new JSONParser();

      var parsed = parser.parse(responsedata);

      var numberOfPages = parsed.metadata["totalPages"];

      var cmdbItems = parsed.content.length;

      for(var j=0;j<cmdbItems;j++){

          resourceID = parsed.content[j]["id"];

      ciList = ciList+resourceID+";";

      }

       

      for(var i=1; i<numberOfPages; i++){

      _endPoint = endPoint+"/catalog-service/api/consumer/resources?withExtendedData=false&page="+i+"&limit=20";

      request.setEndpoint(_endPoint);

      request.setHttpMethod('get');

      request.setRequestHeader("Accept", "application/json");

      request.setRequestHeader("Authorization", "Bearer "+authGen);

      request.setRequestHeader("Content-Type", "application/json");

      response = request.execute();

       

      if(response.haveError()) {

      VRASNLogger.error('REST call error found inside VRASNImportCMDB.reconcileCMDB: '+ response.getErrorMessage());

      this.errorHandler.notify("REST call error found inside VRASNImportCMDB.reconcileCMDB: "+ response.getErrorMessage());

      gotException = true;

      }

      else {

      responsedata = response.getBody();

      parsed = parser.parse(responsedata);

      cmdbItems = parsed.content.length;

      for(j=0;j<cmdbItems;j++){

         resourceID = parsed.content[j]["id"];

      ciList = ciList+resourceID+";";

      }

      }

       

      }

       

      if(JSUtil.notNil(ciList)) {

      var gr = new GlideRecord("cmdb_ci");

      gr.addEncodedQuery("u_vra_uidISNOTEMPTY^install_status!=7^ORinstall_status=NULL");

      gr.query();

      while(gr.next()) {

      var a = ciList.indexOf(gr.u_vra_uid+"");

      if(a>-1) {

      VRASNLogger.debug('The record exists in the list '+ gr.name);

      }

      else {

      gr.install_status = 7;

      gr.update();

      }

      }

      }

      }

      }

      catch(e){

      VRASNLogger.error('Exception caught inside VRASNImportCMDB.reconcileCMDB: '+e);

      this.errorHandler.notify("Exception caught inside VRASNImportCMDB.reconcileCMDB: "+e);

      }

      },

       

      type: 'VRASNImportCMDB'

      };