To update memo field in revenue arrangement records

Jira Code: NMS 287

User event script

/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 * @NModuleScope SameAccount
 */

/**
 * Script Description: This script is to update the memo field in revenue arrangment ,with 
 * the memo of the corresponding journel entry element of the revenue arrangment record. So that in report this memo field can be seen.
 */

/*******************************************************************************
 * * ESW * *
 * **************************************************************************
 * Date:5/11/18 
 * Script name: 
 * Script id: 
 * Deployment id: 
 * 
 * revision: 22/06/2018

 ******************************************************************************/

define([ 'N/record', 'N/search'],

		function(record,search) {

	/**
	 * 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 beforeSubmit(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 revId=scriptContext.newRecord.id;
			

			var memoSo = [];
			var source = scriptContext.newRecord.getSublistValue({
				sublistId: 'revenueelement',
				fieldId: 'referenceid',
				line: 0
			});
			
			if((source!=undefined))
			{
				var sourceIdArr = source.split('_');
				var sourceId = 0;
				var sourceArr =sourceIdArr[0];
				if(sourceIdArr.length>0){
					sourceId = sourceIdArr[1];
				}
				
				var invoiveId,foundInvoice=0 ;

				if(sourceArr=='Journal'){
					var journalentrySearchObj = search.create({
						type: "journalentry",
						filters:
							[
								["type","anyof","Journal"], 
								"AND", 
								["multisubsidiary","is","F"], 
								"AND", 
								["advintercompany","is","F"], 
								"AND", 
								["accounttype","anyof","DeferRevenue"], 
								"AND", 
								["internalidnumber","equalto",sourceId]
								],
								columns:
									[
										search.createColumn({name: "memomain", label: "Memo (Main)"})
										]
					});

					var journalentryResult = journalentrySearchObj.run().getRange({
						start :0,
						end:1
					});
					if(journalentryResult!=null && journalentryResult.length>0){
						record.submitFields({
							type: 'revenuearrangement',
							id: revId,
							values: {
								'memo': journalentryResult[0].getValue({
									name: "memomain"
								})
							}
						});
					}
				}
				// if it is a sales order
				else if(sourceArr=='SalesOrd'){
					try{
					var salesordersearch = search.create({
						type: "salesorder",
						filters:
							[
								["type","anyof","SalesOrd"], 
								"AND", 
								["internalidnumber","equalto",sourceId],
								"AND",
								["mainline","is","F"]
								],
								columns:
									[
										search.createColumn({name: "fulfillingtransaction", label: "Fulfilling/Receiving Transaction"})

										]
					});
				}catch(e)
				{
					log.debug('Err@Create search of SO',e.message);
					log.error('Err@Create search of SO',e.message);
				}
				try{
					var salesordersearchResult = salesordersearch.run().getRange({
						start :0,
						end:100
					});
					
					if(salesordersearchResult!=null && salesordersearchResult.length>0){

						for(var i=0;i<100;i++)
						{
							invoiveId = salesordersearchResult[i].getText({
								name: "fulfillingtransaction"
							});
							
							if(invoiveId.indexOf('Invoice')>-1)
							{

								memoSo = invoiveId.split('#');
								record.submitFields({
									type: 'revenuearrangement',
									id: revId,
									values: {
										'memo': memoSo[1]
									}
								});
								break;
							}
						}
					}
				}catch(e)
				{
					log.debug('Err@run serach ',e.message);
					log.error('Err@run serach ',e.message);
				}

				}
				else if(sourceArr=='CustInvc')
					{
					try{
					// search for the Invoice
						l
						var invoicesearch = search.create({
							type: "invoice",
							filters:
								[
									["type","anyof","CustInvc"], 
									
									"AND", 
									["internalidnumber","equalto",sourceId]
									],
									columns:
										[
											search.createColumn({name: "tranid", label: "tranid"})

											]
						});
						
						// to run 
						var invoicesearchResult = invoicesearch.run().getRange({
							start :0,
							end:1
						});
						
						if(invoicesearchResult!=null && invoicesearchResult.length>0){
							
							record.submitFields({
								type: 'revenuearrangement',
								id: revId,
								values: {
									'memo': invoicesearchResult[0].getValue({
										name: "tranid"
									})
								}
							});
						}
						
					}catch(e)
					{
						log.debug('Err@ Invoice',e.message);
						log.error('Err@ Invoice',e.message);
					}
					}
				
				
				
				else if(sourceArr=='CustCred')
				{
				try{
				// search for the Invoice
					
					var credsearch = search.create({
						type: search.Type.CREDIT_MEMO,
						filters:
							[
								["type","anyof","CustCred"], 
								"AND", 
								["internalidnumber","equalto",sourceId]
								],
								columns:
									[
										search.createColumn({name: "tranid", label: "tranid"})

										]
					});
					
					// to run 
					var credsearchResult = credsearch.run().getRange({
						start :0,
						end:1
					});
					
					if(credsearchResult!=null && credsearchResult.length>0){
						
						record.submitFields({
							type: 'revenuearrangement',
							id: revId,
							values: {
								'memo': credsearchResult[0].getValue({
									name: "tranid"
								})
							}
						});
					}
					
				}catch(e)
				{
					log.debug('Err@ Invoice',e.message);
					log.error('Err@ Invoice',e.message);
				}
				}
			
			}
			}
			catch(err)
			{
				log.debug("ERROR",err);
				log.error("ERROR",err);
			}
		}


		return {

			afterSubmit : afterSubmit
		};

	});

Leave a comment

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