简体   繁体   English

如何在 NetSuite 中一次性获取所有有效货币汇率?

[英]How to get All the effective Currency exchange rate by date in NetSuite one time?

[![enter image description here][1]][1] [![在此处输入图像描述][1]][1]

Any method to query All the effective Currency exchange rates by date in NetSuite one time?有什么方法可以一次性在NetSuite中按日期查询所有有效货币汇率? Just like the Field "AS OF" in the Currency Exchange Rates Page did.就像货币汇率页面中的“AS OF”字段一样。

I thought about the "N/curency" module and the https.get function, but it seems to be high cost, any tips or solution?我想过“N/curency”模块和https.get函数,但它似乎成本很高,有什么技巧或解决方案吗? [1]: https://i.stack.imgur.com/1KPMK.png [1]: https ://i.stack.imgur.com/1KPMK.png

The table is exposed in SuiteTalk and the Analytics browser so you can get the values either way.该表在 SuiteTalk 和 Analytics 浏览器中公开,因此您可以通过任一方式获取值。 Via Analytics/SuiteQL通过分析/SuiteQL

require(['N/query'], function(query) {
   var sql =
        "SELECT " +
        " cr.id, b.symbol as basecurrency, c.symbol as transactioncurrency,  cr.effectivedate, cr.exchangerate" +
        " FROM " +
        "  currencyrate as cr, currency as c, currency b where c.id = transactioncurrency and b.id = basecurrency and cr.effectivedate = '5/2/2022' ";

    var results = query.runSuiteQL({
        query: sql
    }).asMappedResults();

    console.log(results);
});

The script below is my final solution to fulfill my demand, You may run it on any Record page's Console, It will print the result JUST like the "Currency Exchange Rates" page shows.下面的脚本是我满足我需求的最终解决方案,您可以在任何记录页面的控制台上运行它,它将像“货币汇率”页面显示的那样打印结果。

I'm not sure if it's a good one, but it did returns the data that I want.我不确定它是否好用,但它确实返回了我想要的数据。

require(['N/query'], function getEffCcyExchRtArr(query) {

    const getQueryResBySql = (sql) => {

        const resObj = query.runSuiteQL({query: sql, });
        return resObj.asMappedResults();
    };

    const getPagedQueryResBySql = (sql) => {

        const maxPageSize = 1_000;
        const resultSet = query.runSuiteQLPaged({query: sql, pageSize: maxPageSize, });

        const tempArr = [];
        if (resultSet && resultSet.count > 0) {
            for (let i = 0; i < resultSet.pageRanges.length; i++)  {
                const page = resultSet.fetch(i);
                page.data.results.forEach(item => tempArr.push(item.asMap()));
            }
        }

        return tempArr;
    };

    const getAllCurrencyArr = () => {

        const sql = `
            SELECT
                Currency.id AS id,
                Currency.isbasecurrency AS is_base_ccy,
                Currency.symbol AS iso_symbol
            FROM
                Currency
            WHERE
                NVL(Currency.isinactive, 'F') = 'F'
        `;

        return getQueryResBySql(sql);
    };

    const getAllEffectiveCcyExchRtArr = (effectiveDateStr) => {

        let effectiveDate = !!effectiveDateStr ? `to_date('${effectiveDateStr}','YYYY-MM-DD')` : 'CURRENT_DATE';
        let sql = `
            SELECT
                currencyRate.basecurrency AS base_ccy,
                currencyRate.transactioncurrency AS trans_ccy,
            
                currencyRate.exchangerate AS exchange_rt,
                currencyRate.effectivedate AS effective_date
            FROM
                currencyRate
            WHERE
                currencyRate.effectivedate <= ${effectiveDate}
            ORDER BY currencyRate.effectivedate DESC, currencyRate.id DESC
        `;

        return getPagedQueryResBySql(sql);
    };

    const currencyArr = getAllCurrencyArr();
    const baseCurrencyIdArr = currencyArr.filter(item => item['is_base_ccy'] === 'T').map(item => item.id);
    const allCurrencyIdArr = currencyArr.map(item => item.id);
    const currencyIdSymbolObj = currencyArr.reduce((pre, cur) => {

        pre[cur?.id] = cur?.['iso_symbol'];
        return pre;
    }, {});
    const allEffectiveCcyExchRtArr = getAllEffectiveCcyExchRtArr();

    const effectiveCcyExchRtArr = [];
    for (const baseCurrencyId of baseCurrencyIdArr) {
        for (const currencyId of allCurrencyIdArr) {
            for (const currencyObj of allEffectiveCcyExchRtArr) {

                if (currencyObj?.['base_ccy'] === baseCurrencyId && currencyObj?.['trans_ccy'] === currencyId) {

                    effectiveCcyExchRtArr.push({
                        baseCurrency: currencyIdSymbolObj[baseCurrencyId],
                        transactionCurrency: currencyIdSymbolObj[currencyId],
                        exchangeRate: currencyObj?.['exchange_rt']
                    });
                    break;
                }
            }
        }
    }

    window.console.table(effectiveCcyExchRtArr);
}, );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM