User Event script to add a custom field in Requisition record which gives the Exchange Rate corresponding to the Transaction Date.

 /**
         * defines a search of exchange rate
         */
        const currencyExchangeRateSearch = (transactionCurrency,transactionDate) => {

            try{

                var exchangeRate;
                var currencyrateSearchObj = search.create({
                    type: "currencyrate",
                    filters:
                        [
                            ["basecurrency","anyof","1"],
                            "AND",
                            ["transactioncurrency","anyof",transactionCurrency],
                            "AND",
                            ["effectivedate","onorbefore",transactionDate]
                        ],
                    columns:
                        [
                            search.createColumn({
                                name: "effectivedate",
                                sort: search.Sort.DESC,
                                label: "Effective Date"
                            }),
                            search.createColumn({name: "internalid", label: "Internal ID"}),
                            search.createColumn({name: "exchangerate", label: "Exchange Rate"}),
                            search.createColumn({name: "basecurrency", label: "Base Currency"}),
                            search.createColumn({name: "transactioncurrency", label: "Transaction Currency"})
                        ]
                });
                var searchResultCount = currencyrateSearchObj.runPaged().count;
                log.debug("currencyrateSearchObj result count",searchResultCount);
                currencyrateSearchObj.run().each(function(result){

                    exchangeRate= result.getValue({
                        name: "exchangerate",
                        label: "Exchange Rate"
                    });
                    return false;
                });
                    return exchangeRate;

            }catch (e) {
                log.error("error @ currencyExchangeRateSearch ", e);
            }

        }

        /**
         * Defines the function definition that is executed after record is submitted.
         * @param {Object} scriptContext
         * @param {Record} scriptContext.newRecord - New record
         * @param {Record} scriptContext.oldRecord - Old record
         * @param {string} scriptContext.type - Trigger type; use values from the context.UserEventType enum
         * @since 2015.2
         */
        const afterSubmit = (scriptContext) => {

            try{
                if((scriptContext.type == scriptContext.UserEventType.CREATE)||(scriptContext.type == scriptContext.UserEventType.EDIT)){

                    const newRecord=scriptContext.newRecord;

                    const transactionDate1=newRecord.getValue({
                        fieldId:'trandate'
                    });

                    const month=transactionDate1.getMonth()+1;
                    log.debug("month",month);

                    const date=transactionDate1.getDate();

                    const year=transactionDate1.getFullYear();

                    const transactionDate=date+"/"+month+"/"+year;


                    const transactionCurrency=newRecord.getValue({
                        fieldId:'custbody20'
                    });

                    if(transactionCurrency){

                        var exchangeRate=currencyExchangeRateSearch(transactionCurrency,transactionDate);

                        if(exchangeRate){
                            record.submitFields({
                                type: 'purchaserequisition',
                                id: newRecord.id,
                                values: {
                                    'custbody_jj_us_exchange_rate': exchangeRate
                                }
                            });

                        }
                    }else{
                        record.submitFields({
                            type: 'purchaserequisition',
                            id: newRecord.id,
                            values: {
                                'custbody_jj_us_exchange_rate':""
                            }
                        });
                    }
                }
            }catch (e) {
                log.error("error @afterSubmit", e);
            }
        }

        return {beforeLoad, beforeSubmit, afterSubmit}

    });

Leave a comment

Your email address will not be published. Required fields are marked *