Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

135
Views
Can we reset streamwriter data for inbound integration?

Just wondering if we are able to re-write data that we was set via the setWriteString() method while responding to an inbound api call. For example, let's say the scripted rest resource code is as follows:

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

    var body = request.body.data;
    /* do something with request data
    ..
    ..
    ..
    
    then start preparing the response
    */
    var writer = response.getStreamWriter();
    try
    {
        response.setContentType('application/json');
        response.setStatus(200);        
        writer.writeString("{\"results\":[");
        var inc = new GlideRecord('incident');
        inc.query();
        while(inc.next()){
            var obj = {};
            obj.id = inc.getValue('number');
            obj.sys_id = inc.getUniqueValue();
            writer.writeString(global.JSON.stringify(obj));
            
            if (inc.hasNext()) {
                writer.writeString(",");
            }
        }
        writer.writeString("]}");
    }
    catch (ex)
    {
        // let's say exception was thrown on the 3rd iteration while gliding the incident table
        // oh no...an exception..so we need to write something else to the stream
        // is it possible to erase/remove everything that was added to the stream up until the exception occured?
        // so that the response will contain details only about the error?
        
        // something like below:
        response.setContentType('application/json');
        response.setStatus(500);
        writer.writeString("{\"error\":\"Something went wrong\"}"); // this isn't working btw...the stream contained both the content generated in "try" block as well as the "catch" block
        
        // this should not contain anything related to whatever was written from the earlier iterations....
    }

})(request, response);
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

For the errors you can use the Scripted REST API Error objects. Which should reset the output stream.
https://developer.servicenow.com/dev.do#!/learn/courses/paris/app_store_learnv2_rest_paris_rest_integrations/app_store_learnv2_rest_paris_scripted_rest_apis/app_store_learnv2_rest_paris_scripted_rest_api_error_objects

(function run(request, response) {      
  try {
   ...
  } catch (e) {
      var myError = new sn_ws_err.ServiceError();
      myError.setStatus(500);
      myError.setMessage('Something went wrong');
      myError.setDetail('Error while retrieving your data: ' + e);
      return myError;
  }
})(request,response);

It might also be useful to get the error message from the GlideRecord

gr.getLastErrorMessage(); 
// something like aborted by businessrule or acl, etc...

For the details of your error message.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!