Scripted rest API is not working on another developer instance

mareks_p
Giga Contributor

Hi,

I developed scripted rest API on one instance, tested, and it worked. After Export to XML and upload to different developer instance I got an error:

{
  "error": {
  "detail": "TypeError: undefined is not a function. (sys_ws_operation.81ccc7210feb2200d1be489ce1050e41; line 4)",
  "message": "undefined is not a function."
  },
  "status": "failure"
}

2016-12-08 14_25_38-Log _ ServiceNow.png

My rest api:

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

  // implement resource here

  var JSONG = new global.JSONG();

  var testName = request.body.data.name;

  var gr = new GlideRecord("sysauto_script");

  gr.get("name", testName);

  var execute = new global.ExecuteScheduledJob();

  execute.executeNow(gr);

  sleep(10000);

  function sleep(ms) {

  var unixtime_ms = new Date().getTime();

  while(new Date().getTime() < unixtime_ms + ms) {}

  }

  var testResult;

  var result = new GlideRecord("u_unit_test_result");

  result.orderByDesc("sys_created_on");

  result.query();

  var resultArr = [];

  if(result.next()){

  resultArr.push(JSONG.encodeGr(result));

  }

  response.setBody(resultArr);

  return;

})(request, response);

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

find_real_file.png

find_real_file.png

What could be wrong?

1 ACCEPTED SOLUTION

mareks_p
Giga Contributor

Ok, these two functions were defined globally. Moved them in REST API code, it works.


View solution in original post

4 REPLIES 4

axw1n
Kilo Explorer

What is JSONG() in   "var JSONG = new global.JSONG();" ? Shouldn't it be JSON?


mareks_p
Giga Contributor

Seems a little bit better, now it fails on


org.mozilla.javascript.EcmaError: undefined is not a function.


Caused by error in sys_ws_operation.81ccc7210feb2200d1be489ce1050e41 at line 9



6: var gr = new GlideRecord("sysauto_script");


7: gr.get("name", testName);


8:


==> 9: var execute = new global.ExecuteScheduledJob();


10: execute.executeNow(gr);


11:


12: sleep(10000);



But it was working fine as


var JSONG = new global.JSONG();


on instance I developed this Rest API


mareks_p
Giga Contributor

Ok, these two functions were defined globally. Moved them in REST API code, it works.


Hello, 

I need to create a Webservice API where input is: Sys ID of any record and output is: all *related* active records to that sys id. The relationships between the tables is maintained in Relationships under system definition.

Could you please guide me how to implement this? Sample code would be great.

Input: Sys ID of the record 
Output: All *RELATED* active records from *VARIOUS* tables (in the following JSON format):
{
"result": [
{
"Sys ID": "5520267",
"CI Name": "Record 1",
"Table Name": "u_table_a"
},
{
"Sys ID": "5520367",
"CI Name": "Record 2",
"Table Name": "u_table_a"
},
{
"Sys ID": "8331210",
"CI Name": "Record 1",
"Table Name": "u_table_b"
},
{
"Sys ID": "8321210",
"CI Name": "Record 2",
"Table Name": "u_table_b"
},
{
"Sys ID": "3042006",
"CI Name": "Record 3",
"Table Name": "u_table_b"
},
{
"sys_id": "4509847",
"CI Name": "Record 1",
"Table Name": ""u_table_c"
}
{
"sys_id": "4509247",
"CI Name": "Record 2",
"Table Name": ""u_table_c"
}
]
}