Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

@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

Thanks @Ankur Bawiskar  it works !

Hi @Ankur Bawiskar  can you please share  ref document for what syntax have to use in scope application