Convert Amount into words

To convert Total amount of record into words on the after submit

/**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 */
 define(['N/record','N/search'],

 (record,search) => {


         /**
          * 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{

                         var recId = scriptContext.newRecord.id;
                         var recType = scriptContext.newRecord.type;
                         var amount;
                         var amountToThai;
                        
                         var poRec = record.load({
                                 type: recType,
                                 id: recId,
                                 isDynamic: false
                         }) ;
                         amount = poRec.getValue({
                            fieldId:'total'
                        });
                       var amountToThai = convertToEng(amount);

                         //set the thai words to the custom field on the customer payment
                         poRec.setValue({
                                 fieldId:'custbody_jj_amount_in_words_js_16',
                                 value:amountToThai
                         })
                        
                         var saveID = poRec.save({
                                 enableSourcing: true,
                                 ignoreMandatoryFields: true
                         });

                 }
                 catch (e) {
                         log.error('error@saveRecord',e.message);
                 }

         }
         function convertToEng(s){
                 var th_val = ['', 'Thousand', 'Million', 'Billion', 'Trillion'];
                 var dg_val = ['Zero', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine'];
                 var tn_val = ['Ten', 'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen'];
                 var tw_val = ['Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety'];
                 s = s.toString();
                 s = s.replace(/[\, ]/g, '');
                 if (s != parseFloat(s))
                         return 'not a number ';
                 var x_val = s.indexOf('.');
                 if (x_val == -1)
                         x_val = s.length;
                 if (x_val > 15)
                         return 'too big';
                 var n_val = s.split('');
                 var str_val = '';
                 var sk_val = 0;
                 for (var i = 0; i < x_val; i++) {
                         if ((x_val - i) % 3 == 2) {
                                 if (n_val[i] == '1') {
                                         str_val += tn_val[Number(n_val[i + 1])] + ' ';
                                         i++;
                                         sk_val = 1;
                                 } else if (n_val[i] != 0) {
                                         str_val += tw_val[n_val[i] - 2] + ' ';
                                         sk_val = 1;
                                 }
                         } else if (n_val[i] != 0) {
                                 str_val += dg_val[n_val[i]] + ' ';
                                 if ((x_val - i) % 3 == 0)
                                         str_val += 'Hundred ';
                                 sk_val = 1;
                         }
                         if ((x_val - i) % 3 == 1) {
                                 if (sk_val)
                                         str_val += th_val[(x_val - i - 1) / 3] + ' ';
                                 sk_val = 0;
                         }
                 }
                 if (x_val != s.length) {
                         var y_val = s.length;
                         str_val += 'Point ';
                         for (var i = x_val + 1; i < y_val; i++)
                                 str_val += dg_val[n_val[i]] + ' ';
                 }
                 return str_val.replace(/\s+/g, ' ');
         }
        

         return {afterSubmit}

 });

Leave a comment

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