Amount in words with currency type.

Jira Code: ME-176

Needs to update the total amount of a transaction record into words according to the currency type. A user event script is created which will work in after submission of the record.

User Event

/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 * @NModuleScope SameAccount
 */
/*******************************************************************************
 * CLIENTNAME:MANGALAM
 * ME 101 -Automatically set Amount in words 
 *************************************************************************
 * Date : 08/03/2019
 *
 * Author: Jobin & Jismi IT Services LLP
 *
 * REVISION HISTORY
 *
 * Revision 1.0 ${08/013/2019} aj : created
 * 
 *
 ******************************************************************************/

define(['N/record', 'N/search','N/file'],
		/**
		 * @param {record} record
		 * @param {search} search
		 */
		function(record, search,file) {

	/**
	 * Function definition to be triggered before record is loaded.
	 *
	 * @param {Object} scriptContext
	 * @param {Record} scriptContext.newRecord - New record
	 * @param {string} scriptContext.type - Trigger type
	 * @param {Form} scriptContext.form - Current form
	 * @Since 2015.2
	 */
	function beforeLoad(scriptContext) {

	}

	/**
	 * Function definition to be triggered before record is loaded.
	 *
	 * @param {Object} scriptContext
	 * @param {Record} scriptContext.newRecord - New record
	 * @param {Record} scriptContext.oldRecord - Old record
	 * @param {string} scriptContext.type - Trigger type
	 * @Since 2015.2
	 */
	function afterSubmit(scriptContext) {
		try{

			var curRec = scriptContext.newRecord;
			var curRecType = scriptContext.newRecord.type;
			var curRecId = scriptContext.newRecord.id;
			

			   var billRec = record.load({
                    type: curRecType, 
                    id: curRecId
//                  isDynamic: true,
                });
    

                var currencytype = billRec.getValue({
                    fieldId: 'currency'
                });
                log.debug('currencytype',currencytype)



								if(curRecType=='vendorcredit')
								{
	
var total  = billRec.getValue({
	fieldId: 'usertotal'
});

			}			else{

				var total  = billRec.getValue({
					fieldId: 'total'
				});



								}
         
			if(curRecType=='creditmemo' ||  curRecType=='vendorcredit')
			{
				total=total*-1;
				
			}
			//function to call the words fn
			var amountInWords = findAmountInWords(total,currencytype);
			  log.debug("amountInWords",amountInWords);
			
			// to set amount in words
			var id=record.submitFields({
				type:curRecType,
				id:curRecId,
				values:{
					'custbody_amount_in_wrds':amountInWords
				}
			})

		}catch(e)
		{
			log.debug("Err@ FN beforeSubmit",e);
			log.error("Err@ beforeSubmit FN ",e);

		}

	}


	/************************************
	 * to find amount in words fn
	 ************************************/
	function findAmountInWords(num,currencytype)
	{
		try{
var currency;
			if(currencytype == 1 ){

				currency = "paisa"

			}else if(currencytype == 2){
				currency = "cent"

			}
			else if(currencytype == 4){
				currency = "eurocent"

			}
			else {
				currency = "pence"

			}
			var a = ['','One ','Two ','Three ','Four ', 'Five ','Six ','Seven ','Eight ','Nine ','Ten ','Eleven ','Twelve ','Thirteen ','Fourteen ','Fifteen ','Sixteen ','Seventeen ','Eighteen ','Nineteen '];
			var b = ['', '', 'Twenty','Thirty','Forty','Fifty', 'Sixty','Seventy','Eighty','Ninety'];

			var paisa= (num.toString()).split(".")[1];


          log.debug("paisa",paisa);

			num =(num.toString()).split(".")[0];



			if ((num = num.toString()).length > 9) return 'overflow';
			n = ('000000000' + num).substr(-9).match(/^(\d{2})(\d{2})(\d{2})(\d{1})(\d{2})$/);
			if (!n) return; var str = '';
			str += (n[1] != 0) ? (a[Number(n[1])] || b[n[1][0]] + ' ' + a[n[1][1]]) + 'crore ' : '';
			str += (n[2] != 0) ? (a[Number(n[2])] || b[n[2][0]] + ' ' + a[n[2][1]]) + 'lakh ' : '';
			str += (n[3] != 0) ? (a[Number(n[3])] || b[n[3][0]] + ' ' + a[n[3][1]]) + 'thousand ' : '';
			str += (n[4] != 0) ? (a[Number(n[4])] || b[n[4][0]] + ' ' + a[n[4][1]]) + 'hundred ' : '';
			str += (n[5] != 0) ? ((str != '') ? 'and ' : '') + (a[Number(n[5])] || b[n[5][0]] + ' ' + a[n[5][1]]) + '' : '';
			
			if(paisa!=null &&paisa!=undefined && paisa!='null' && paisa!=" " && paisa!="" && paisa!=00 && paisa !='00')
			{


				m = ('000000000' + paisa).substr(-9).match(/^(\d{2})(\d{2})(\d{2})(\d{1})(\d{2})$/);
				if (!m) return; var str1 = '';
				str1 += (m[1] != 0) ? (a[Number(m[1])] || b[m[1][0]] + ' ' + a[m[1][1]]) + 'crore ' : '';
				str1 += (m[2] != 0) ? (a[Number(m[2])] || b[m[2][0]] + ' ' + a[m[2][1]]) + 'lakh ' : '';
				str1 += (m[3] != 0) ? (a[Number(m[3])] || b[m[3][0]] + ' ' + a[m[3][1]]) + 'thousand ' : '';
				str1 += (m[4] != 0) ? (a[Number(m[4])] || b[m[4][0]] + ' ' + a[m[4][1]]) + 'hundred ' : '';
				str1 += (m[5] != 0) ? ((str != '') ? 'and ' : '') + (a[Number(m[5])] || b[m[5][0]] + ' ' + a[m[5][1]]) + currency +' '+ 'Only' : '';
			}else if(paisa==00||paisa==null ||paisa==undefined || paisa=='null' || paisa==" " || paisa=="" ||paisa=='00'){
				var str1='Only';
log.debug("paisa else in","inn");log.debug("str+str1",str+str1);}

					return str+str1;


		}catch(e)
		{
			log.debug("Err@ FN findAmountInWords",e);
			log.error("Err@ findAmountInWords FN ",e);

		}
	}

	return {

		afterSubmit: afterSubmit
	};

});

Leave a comment

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