Estimate Value calculation using UE script

Jira Code: UN-1

Updating the estimated value of a record on the save of an invoice record .

/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 * @NModuleScope SameAccount
 */
/*******************************************************************************
 * UN-1 Estimate Value
 * **************************************************************************
 * 
 * Date: 12-11-2018
 * 
 * Author: Jobin & Jismi IT Services LLP
 * 
 *****************************************************************************
 **/
define(['N/file', 'N/url', 'N/search', 'N/runtime', 'N/record', 'N/https', 'N/ui/serverWidget', 'N/email'],

    function (file, url, search, runtime, record, https, serverWidget, email) {
        var main = {
            beforeSubmit: function (scriptContext) {
                var newrec = scriptContext.newRecord;
                var oldrec = scriptContext.oldRecord;
                var newfield = newrec.getValue({
                    fieldId: 'custbody_ofi_shipping_estimate'
                })
                log.debug("newfield", newfield)

                var oldfield = oldrec.getValue({
                    fieldId: 'custbody_ofi_shipping_estimate'
                })
                log.debug("oldfield", oldfield)

                if (oldfield != newfield) {
                    var newsetvalue = newfield + ((newfield * 25) / 100);
                    log.debug("newsetvalue", newsetvalue)

                    newrec.setValue({
                        fieldId: 'custbody_ofi_shipping_estimate',
                        value: newsetvalue
                    });
                }
            },

        };
        for (var key in main) {
            if (typeof main[key] === 'function') {
                main[key] = trycatch(main[key], key);
            }
        }

        function trycatch(myfunction, key) {
            return function () {
                try {
                    return myfunction.apply(this, arguments);
                } catch (e) {
                    log.debug("e in  " + key, e);
                }
            }
        };
        return main;
    });

Leave a comment

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