Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

138
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda