Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

142
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda