User Event script to update Item Record fields

Jira Code: BTN-374

This user event script updates the features and brands on item record while editing the categories and series custom record.The script triggers when a custom record entry is submitted. It find the commerce categories for the custom record entry and get the items in the commerce category. The features and brands of those items are updated with the custom record entry.

define(['N/record', 'N/search'],
		/**
		 * @param {record} record
		 * @param {search} 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 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
	 * @Since 2015.2
	 */
	function afterSubmit(scriptContext) {
		var currentRecord = scriptContext.newRecord; //get the record values
		var features = currentRecord.getValue({fieldId:"custrecord_rb_featuresseries"}); //get the features selected
		logme('features',features);
		var brands = currentRecord.getValue({fieldId:"custrecord_rb_balajibrands"}); //get the brand selected
		logme('brands',brands);
		/* var name = record.getValue({fieldId:"name"}); //get the name of the record
        logme('name',name);*/
		var commerceCategory = findCommerceCategory(currentRecord.getValue({fieldId:"name"}));//pass records name to find the commerce category
		var itemArray = [];
		if(commerceCategory){ //if commercecategory is not empty
			for(var i=0 ; i <commerceCategory.length ; i++){
				var commerceCategoryRecord = record.load({ //load the commercecategory
					type : 'commercecategory',
					id : commerceCategory[i],
					isDynamic  :true
				});				
				var itemLineCount = commerceCategoryRecord.getLineCount({sublistId : 'items'});	//get the item list count			
				for(var j=0; j<itemLineCount ; j++){					
					var itemId=commerceCategoryRecord.getSublistValue({sublistId:'items',fieldId:'item',line:j});//get the item id
					itemArray.push(itemId);
					itemArray = itemArray.filter( function( item, index, inputArray ) { //removes duplicates from the item array
				           return inputArray.indexOf(item) == index;
				    });
				}
			}
		}
		for(var k = 0 ; k < itemArray.length ; k++){  //get the item records
			var itemRecord = record.load({ //load the item
				type : 'inventoryitem',
				id : itemArray[k],
				isDynamic  :true
			});
			itemRecord.setValue({
				fieldId : 'custitem_bwv_facets_balajibrands',
				value : brands
			});
			itemRecord.setValue({
				fieldId : 'custitem_rb_facets_features',
				value : features
			});
			
			
			var recordId = itemRecord.save({
			    enableSourcing: true,
			    ignoreMandatoryFields: true
			});
			logme('ItemrecordId',recordId);
		}
	}

	return {
		/* beforeLoad: beforeLoad,
        beforeSubmit: beforeSubmit,*/
		afterSubmit: afterSubmit
	};
	/**
	 * function to find the commerce category of the custom record.
	 * @param name
	 * @returns
	 */
	function findCommerceCategory(name){ 

		var commerceCategory = search.create({ // Search to find the commerce categories with the same name as that of custom record
			type :'commercecategory',
			columns : [{
				name : 'name'
			}],
			filters : [
				['name','is',name]
				]
		}).run().getRange({
			start : 0,
			end : 1000
		});	
		var commerceCategoryArray = new Array();
		if(commerceCategory){    		
			for(var i=0; i < commerceCategory.length; i++){
				commerceCategoryId = commerceCategory[i].id;//get the commerce category id    			
				commerceCategoryArray.push(commerceCategoryId);
			}
		}
		logme('commerceCategoryArray',commerceCategoryArray);
		return commerceCategoryArray;
	}

});

/*******************************************************************************
 * return error
 * 
 * @param e
 * @returns
 * 
 * Created on 09-Aug-2017 by rosemol
 */
function getError(e) {
	var stErrMsg = '';
	if (e.getDetails != undefined) {
		stErrMsg = '_' + e.getCode() + '<br>' + e.getDetails() + '<br>'
		+ e.getStackTrace();
	} else {
		stErrMsg = '_' + e.toString();
	}
	return stErrMsg;
}

/*******************************************************************************
 * Log these data
 * 
 * @param title
 * @param details
 * @returns
 * 
 * Created on 09-Aug-2017 by rosemol
 */
function logme(title, details) {
	log.debug({
		title : title,
		details : details
	});
}

Leave a comment

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