function createDataset(fields, constraints, sortFields) {
    var dataset = DatasetBuilder.newDataset();

    dataset.addColumn("C8_FILIAL");
    dataset.addColumn("C8_NUM");
    dataset.addColumn("C8_NUMPRO");
    dataset.addColumn("C8_ITEM");
    dataset.addColumn("C8_PRODUTO");
    dataset.addColumn("C8_QUANT");
    dataset.addColumn("C8_PRECO");
    dataset.addColumn("C8_TOTAL");
    dataset.addColumn("C8_FORNECE");
    dataset.addColumn("C8_LOJA");
    dataset.addColumn("C8_FORNOME");
    dataset.addColumn("C8_COND");
    dataset.addColumn("C8_NUMSC");
    dataset.addColumn("C8_ITEMSC");
    dataset.addColumn("C8_NUMPED");
    dataset.addColumn("C8_ITEMPED");
    dataset.addColumn("C8_DATPRF");
    dataset.addColumn("A2_NOME");
    dataset.addColumn("A2_NREDUZ");
    dataset.addColumn("B1_DESC");
    dataset.addColumn("B1_UM");
    dataset.addColumn("C8_DESCRI");
    dataset.addColumn("STATUS");
    dataset.addColumn("data_consulta");
    dataset.addColumn("hora_consulta");
    dataset.addColumn("sucesso");
    dataset.addColumn("erro");

    function emptyRow(sucesso, erro) {
        return [
            "", "", "", "", "", "", "", "", "", "", "",
            "", "", "", "", "", "", "", "", "", "", "",
            "", "", "", sucesso ? "true" : "false", erro || ""
        ];
    }

    function normalizeDoc(value) {
        var v = String(value || "").trim();
        return v.replace(/^0+/, "");
    }

    function consultarCotacao(clientService, numeroCot) {
        var endpoints = [
            "/UF_MATA130/" + numeroCot,
            "/rest/UF_MATA130/" + numeroCot,
            "/rest/uf_mata130/" + numeroCot
        ];

        var ultimoErro = "";

        for (var e = 0; e < endpoints.length; e++) {
            try {
                var data = {
                    companyId: "1",
                    serviceCode: "Postprod",
                    endpoint: endpoints[e],
                    method: "get",
                    timeoutService: "60000",
                    headers: {
                        "Content-Type": "application/json"
                    }
                };

                var response = clientService.invoke(JSON.stringify(data));
                if (!response || !response.getResult()) {
                    ultimoErro = "Sem resposta da API";
                    continue;
                }

                var result = JSON.parse(response.getResult());
                if (result && result.sucesso && result.cotacoes && result.cotacoes.length) {
                    return {
                        sucesso: true,
                        endpoint: endpoints[e],
                        result: result
                    };
                }

                if (result && !result.sucesso && result.message) {
                    ultimoErro = String(result.message);
                } else {
                    ultimoErro = "Cotacao sem itens retornados";
                }
            } catch (errEndpoint) {
                ultimoErro = String(errEndpoint);
            }
        }

        return {
            sucesso: false,
            erro: ultimoErro || "Falha ao consultar cotacao"
        };
    }

    try {
        var numeroCot = "";
        if (constraints) {
            for (var i = 0; i < constraints.length; i++) {
                if (constraints[i].fieldName == "numCotacao") {
                    numeroCot = String(constraints[i].initialValue || "").trim();
                }
            }
        }

        if (!numeroCot) {
            dataset.addRow(emptyRow(false, "Numero da cotacao nao informado"));
            return dataset;
        }

        var clientService = fluigAPI.getAuthorizeClientService();
        var retorno = consultarCotacao(clientService, numeroCot);
        if (!retorno.sucesso) {
            dataset.addRow(emptyRow(false, retorno.erro));
            return dataset;
        }

        var result = retorno.result;
        var cotacoes = result.cotacoes || [];
        if (!cotacoes.length) {
            dataset.addRow(emptyRow(false, "Cotacao sem itens retornados"));
            return dataset;
        }

        var filtro = normalizeDoc(numeroCot);
        var adicionados = 0;
        for (var j = 0; j < cotacoes.length; j++) {
            var item = cotacoes[j] || {};

            var numeroItem = String(item.C8_NUM || "").trim();
            if (filtro && normalizeDoc(numeroItem) !== filtro) {
                continue;
            }

            dataset.addRow([
                item.C8_FILIAL || "",
                numeroItem,
                item.C8_NUMPRO || "",
                item.C8_ITEM || "",
                item.C8_PRODUTO || "",
                item.C8_QUANT || "",
                item.C8_PRECO || "",
                item.C8_TOTAL || "",
                item.C8_FORNECE || "",
                item.C8_LOJA || "",
                item.C8_FORNOME || "",
                item.C8_COND || "",
                item.C8_NUMSC || "",
                item.C8_ITEMSC || "",
                item.C8_NUMPED || "",
                item.C8_ITEMPED || "",
                item.C8_DATPRF || "",
                item.A2_NOME || "",
                item.A2_NREDUZ || "",
                item.B1_DESC || "",
                item.B1_UM || "",
                item.C8_DESCRI || "",
                item.STATUS || "",
                result.data_consulta || "",
                result.hora_consulta || "",
                "true",
                ""
            ]);
            adicionados++;
        }

        if (adicionados === 0) {
            dataset.addRow(emptyRow(false, "Numero da cotacao nao localizado no retorno"));
        }
    } catch (e) {
        dataset.addRow(emptyRow(false, "Erro ao consultar cotacao: " + e));
    }

    return dataset;
}
