Genesys Cloud - Developer Community!

 View Only

Sign Up

  • 1.  Function Data Actions - Treat different responses

    Posted 6 hours ago

    Hi, EveryBody

    I have a situation here, I need treat different response in a API. So I would know if I could use the Fuction Data Action to fit the API.

    E. g.
    statuCode = 200

    {
      "data": {
        "costumerId": "<integer>"
      }
    }
    statuCode = 400
    {
      "code": "<string>",
      "message": "<string>",
      "details": "<string>",
      "fields": [
        {
          "name": "<string>",
          "value": "<string>",
          "message": "<string>",
          "details": "<string>"
        },
        {
          "name": "<string>",
          "value": "<string>",
          "message": "<string>",
          "details": "<string>"
        }
      ]
    }
    statuCode = 500
    {
      "code": "<string>",
      "message": "<string>",
      "details": "<string>"
    }

    #DataActions

    ------------------------------
    Lineu Romão.
    ------------------------------


  • 2.  RE: Function Data Actions - Treat different responses

    Posted 6 hours ago

    Hi Lineu, 

    Yes, it is possible to handle different API responses using a Function Data Action in Genesys Cloud. The idea is to normalize the response inside the function so Architect always receives a predictable structure, regardless of whether the API returned 200, 400, or 500.

    In one implementation I built, the API could also return different response bodies depending on the status code. Instead of trying to map each variation directly in the Data Action translation map, I handled the logic inside the function and returned a standardized output.

    Here is a simplified example similar to what I used:

    =-=-=-=-=-=-=

    const fetch = require('node-fetch');
     
    module.exports.handler = async function(input, context) {
     
      let resultsJson;
      let statusOriginal;
     
      try {
        const var_cpf = input.cpf;
        const var_oauthToken = input.oauthToken;
        const basePath = input.urlRequest;
     
        const url = `${basePath}/Cpf?cpf=${var_cpf}`;
     
        const headers = {
          'Accept': 'application/json',
          'Authorization': `Bearer ${var_oauthToken}`,
          'Accept-Encoding':'gzip, deflate, br',
          'Cache-Control':'no-cache',
          'Connection':'keep-alive',
          'Content-type':'application/json'
        };
     
        const response = await fetch(url, {
          method: 'GET',
          headers: headers
        });
     
        const results = await response.text();
        resultsJson = results ? JSON.parse(results) : null;
        statusOriginal = response.status;
     
        return {
          statusCode: 200,
          sucessoOriginal: response.ok,
          statusOriginal,
          resultsJson
        };
     
      } catch (error) {
        return {
          statusCode: 200,
          sucessoOriginal: false,
          statusOriginal,
          body: {
            erro: `Erro na execução: ${error.message}`
          }
        };
      }
    };
    =-=-=-=-=-=-=-=-=
     
     
    In this pattern:
     
     - The Function Data Action always returns statusCode: 200 to Genesys Cloud so the action itself does not fail.
     - The original API status (response.status) is preserved in statusOriginal.
     - The full parsed response (resultsJson) is returned, regardless of whether the API returned success or error.
    In Architect or the calling flow, you can evaluate:
    statusOriginal == 200
    statusOriginal == 400
    statusOriginal == 500
    statusOriginal != 200....etc
     
    This approach makes it much easier to handle APIs that return different schemas depending on the status code, because you centralize the logic inside the function instead of fighting with multiple translation maps.
     
    If your API responses sometimes change field types or structure, using this pattern also helps avoid mapping errors by letting the function normalize the response before Genesys processes it.



    ------------------------------
    Kaio Oliveira
    GCP - GCQM - GCS - GCA - GCD - GCO - GPE & GPR - GCWM

    PS.: I apologize if there are any mistakes in my English; my primary language is Portuguese-Br.
    ------------------------------



  • 3.  RE: Function Data Actions - Treat different responses

    Posted 5 hours ago

    Tank you @Kaio Oliveira

    It's was usefull



    ------------------------------
    Lineu Roberto
    ------------------------------



  • 4.  RE: Function Data Actions - Treat different responses

    Posted an hour ago

    Thanks for sharing@Kaio Oliveira this is a really helpful example.



    ------------------------------
    Phaneendra
    Technical Solutions Consultant
    ------------------------------