Scripted Code is not working in scoped application

String
Kilo Sage

Hi Team ,

Am using scoped application(x_cenv_integration) and written inbound with pagination 

 

URL:https://xyz.service-now.com/api/x_cenv_integration/oas_sr?size=2&number=1

 

inboundCode:

 

var toDelete = ['new_location_details', 'requester_details', 'request_details'];
var pageSize = request.queryParams.size;
var pageNumber = request.queryParams.number;
var pageRecords = [];
var grRequestedItem = new GlideRecord('sc_req_item');
grRequestedItem.orderByDesc('sys_created_on');
grRequestedItem.addEncodedQuery("sys_created_on>=javascript&colon;gs.dateGenerate('2023-07-01','00:00:00')^sys_created_on<=javascript&colon;gs.dateGenerate('2023-07-07','23:59:59')");
grRequestedItem.chooseWindow(pageSize * (pageNumber - 1), pageSize * pageNumber);
grRequestedItem.query();
while (grRequestedItem.next()) {
  var resJSON = {
    number: grRequestedItem.getValue('number'),
    cat_item: grRequestedItem.getValue('cat_item'),
    contact_type: grRequestedItem.getValue('contact_type'),
    state: grRequestedItem.getValue('state')
  };
 
  for (var prop in grRequestedItem.variables) {
    if (grRequestedItem.variables.hasOwnProperty(prop)) {
      if (!toDelete.includes(prop)) {
        var variable = grRequestedItem.variables[prop].getValue();
        resJSON[prop] = variable;
      }
    }
  }
 
  pageRecords.push(resJSON);
}
 
response.setBody(pageRecords);
 
Error:
{
"error": {
"message": "Method returned an object of type IdFunctionObject which is not allowed in scope x_cenv_integration",
"detail": "Method returned an object of type IdFunctionObject which is not allowed in scope x_cenv_integration"
},
"status": "failure"
}
 
NOTE:Same code is working in Global scope but not working in Customs scope 
 
Please guide me 
1 ACCEPTED SOLUTION

@String 

then this line is the issue

if (grRequestedItem.variables.hasOwnProperty(prop)) {

try changing as this

var toDelete = ['new_location_details', 'requester_details', 'request_details'];
var pageSize = request.queryParams.size;
var pageNumber = request.queryParams.number;
var pageRecords = [];
var grRequestedItem = new GlideRecord('sc_req_item');
grRequestedItem.orderByDesc('sys_created_on');
grRequestedItem.addEncodedQuery("sys_created_on>=javascript&colon;gs.dateGenerate('2023-07-01','00:00:00')^sys_created_on<=javascript&colon;gs.dateGenerate('2023-07-07','23:59:59')");

grRequestedItem.chooseWindow(pageSize * (pageNumber - 1), pageSize * pageNumber);
grRequestedItem.query();
while (grRequestedItem.next()) {
var resJSON = {
number: grRequestedItem.getValue('number'),
cat_item: grRequestedItem.getValue('cat_item'),
contact_type: grRequestedItem.getValue('contact_type'),
state: grRequestedItem.getValue('state')
};
for (var prop in grRequestedItem.variables) {
gs.info("check inside for" + prop) ;
if (grRequestedItem.variables[prop] != '') {
gs.info("check inside if");
 if (!toDelete.includes(prop)) {
var variable = grRequestedItem.variables[prop].getValue();
resJSON[prop] = variable;
}
}
}
pageRecords.push(resJSON);
}
response.setBody(pageRecords);

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

7 REPLIES 7

Ankur Bawiskar
Tera Patron
Tera Patron

@String 

which line is that?

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar  am not using type IdFunctionObject in my code ,but still it throws an error 

@String 

that's correct but definitely due to some line your script it's throwing the error

try adding gs.info() and check till where it's working fine

 

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar 

code is not going to if condition in below highlighted 

 

var toDelete = ['new_location_details', 'requester_details', 'request_details'];
var pageSize = request.queryParams.size;
var pageNumber = request.queryParams.number;
var pageRecords = [];
var grRequestedItem = new GlideRecord('sc_req_item');
grRequestedItem.orderByDesc('sys_created_on');
grRequestedItem.addEncodedQuery("sys_created_on>=javascript&colon;gs.dateGenerate('2023-07-01','00:00:00')^sys_created_on<=javascript&colon;gs.dateGenerate('2023-07-07','23:59:59')");
 
grRequestedItem.chooseWindow(pageSize * (pageNumber - 1), pageSize * pageNumber);
grRequestedItem.query();
while (grRequestedItem.next()) {
    var resJSON = {
        number: grRequestedItem.getValue('number'),
        cat_item: grRequestedItem.getValue('cat_item'),
        contact_type: grRequestedItem.getValue('contact_type'),
        state: grRequestedItem.getValue('state')
    };
    for (var prop in grRequestedItem.variables) {
      gs.info("check inside for");
        if (grRequestedItem.variables.hasOwnProperty(prop)) {
            gs.info("check inside if");
            if (!toDelete.includes(prop)) {
                var variable = grRequestedItem.variables[prop].getValue();
                resJSON[prop] = variable;
            }
        }
    }
pageRecords.push(resJSON);
}
response.setBody(pageRecords);
 
 
I can see logs: check inside for
check inside if is not printing