**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'
};
draymond10 We will take care of this case. '=' is missing during loop iteration.
draymond10 We will take care of this case. '=' is missing during loop iteration.
We also saw some other issues with this function when resources fell into multiple pages. Some Items wouldn't show up in any of the pages unless limit= was changed to fit all resources on one page. I suspect that without sorting, a random selection of items would go to each page, with some possibly falling outside of any page and getting set as retired. If I add &%24orderby=name+desc to the URL the missing items properly show up in one of the pages.
This issue was reproducible when tested in Postman tool using same API.
May be this behavior need to be taken care at vRA end. For now as a workaround as @ttyRazor
suggested, user can add &%24orderby=name+desc to the api endpoint in ServiceNow.