500 Internal Server Error when calling Service now Scripted REST API

RA4
Tera Contributor

Hi I am getting below error when i am trying  to call the scripted REST API with below script. Can someone please verify below script and correct  me if anything wrong.

 

Error: <?xml version="1.0" encoding="UTF-8"?><response><error><detail>ReferenceError: &quot;XMLHelper&quot; is not defined. (sys_ws_operation.b35de1cdff0f2010905087f8b721f1cc.operation_script; line 22)</detail><message>&quot;XMLHelper&quot; is not defined.</message></error><status>failure</status></response>

 

Script. 

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

// implement resource heere
var ShortDesc;
var TOnum = request.pathParams.change_number;
var body = {};

var grTO = new GlideRecord('change_request');
grTO.addQuery('number', TOnum);
grTO.query();
if (grTO.next()) {
body.GroupName = grTO.getDisplayValue('assignment_group');
body.State = grTO.getDisplayValue('state');
}

/* response.setContentType('application/JSON');
var writer = response.getStreamWriter();
writer.writeString(JSON.stringify(body));
*/
response.setContentType('application/xml');
var writer = response.getStreamWriter();
var xhelper = new XMLHelper();
writer.writeString(xhelper.toXMLStr(body));

})(request, response);

 
1 ACCEPTED SOLUTION

Kieran Anson
Kilo Patron

Hi,

Is the scripted rest API within a custom scope? XMLHelper is only available in the global scope.

View solution in original post

10 REPLIES 10

No worries,

Alternatively you can use the XMLDocument2 API within a scoped application.

Please mark my answer helpful & correct to close the thread.

Hi @Kieran Anson 

Can you please let me know how did you resolve this issue .

I am getting below error.

I am using Scripted REST API in Scoped application and I am using below code

response.setContentType('application/xml');
                var writer=response.getStreamWriter();
				var xhelper = new XMLDocument2();
				writer.writeString(xhelper.toXMLStr(body1));

{
"error": {
"detail": "TypeError: Cannot find function toXMLStr in object <?xml version=\"1.0\" encoding=\"UTF-8\"?>. (sys_ws_operation.df79514c1bd0b4d0c123a8e3b24bcbd8.operation_script; line 117)",
"message": "Cannot find function toXMLStr in object <?xml version=\"1.0\" encoding=\"UTF-8\"?>."
},
"status": "failure"

toXMLStr isn't a function of XMLDocument2 as per the docs site

Hi , Could you please guide what can used here. I have created scripted rest api in scoped application.

What are you trying to achieve? The docs advise to use .toString if you're wanting to stringify the response.