Add Commerce category using script

Jira Code: MT 62

Add “Categories” and “Shop By” so they automatically list on site. I’m currently using manual add function: Lists>Website>Commerce Categories>Category>Edit>Add/Subtract/Reorder item.
This is included in item edits under “Web Store” but I understand we customised so maybe this doesn’t work. If not, I’d like a custom field to select these two designations with the ability to select more than 1 for each.

User Event script

/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 * @NModuleScope SameAccount
 */
/*******************************************************************************
 * CLIENTNAME:MERCHANDIZE 
 * MT-62 
 *************************************************************************
 * Date : 19/01/2019
 *
 * Author: Jobin & Jismi IT Services LLP
 * Script Description : To set the Item Category.
 *  			 DONE BY JJ TEAM
 *  Date created : 19/01/2019
 *
 * REVISION HISTORY
 *
 * Revision 1.0 ${19/01/2019} aj : created
 * 
 *
 ******************************************************************************/
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 {Record} scriptContext.oldRecord - Old record
	 * @param {string} scriptContext.type - Trigger type
	 * @Since 2015.2
	 */
	function afterSubmit(scriptContext) {
		try {
			// to get the internal id
			var id = scriptContext.newRecord.id;
			
			var currRec = scriptContext.newRecord;

			// to get the category custitem_mt62_commerce_category
			var category = currRec.getValue({
				fieldId : 'custitem_mt62_commerce_category'
			});
			
			var categoryCalculation = currRec.getValue({
				fieldId : 'custitem_mt62_commerce_category'
			});
			
			if (category) {
				if (scriptContext.type == 'create') {
					for (var i = 0; i < category.length; i++) {
						
						// to create in category
						var categoryRec = record.load({
							type : 'commercecategory',
							id : category[i],
							isDynamic : true
						});

						// to set in Item sublist items 
						var lineNum = categoryRec.selectNewLine({
							sublistId : 'items'
						});

						// to set the items
						categoryRec.setCurrentSublistValue({
							sublistId : 'items',
							fieldId : 'item',
							value : id
						
						});
						categoryRec.commitLine({
							sublistId : 'items'
						});
						var catId = categoryRec.save({
							ignoreMandatoryFields : true
						});
					}

				}
				else if(scriptContext.type == 'edit')
					{
					// to get the old record
					var oldRec = scriptContext.oldRecord;
					var oldCategory = oldRec.getValue({
						fieldId : 'custitem_mt62_commerce_category'
					});
					
					var oldCategoryCalculation = oldRec.getValue({
						fieldId : 'custitem_mt62_commerce_category'
					});
					
					// to check common elements in Category array
					
					var toRemove = [],toAdd=[];

					 var arrlen = oldCategoryCalculation.length;
					for (var i = 0; i<oldCategoryCalculation.length; i++) {
					   
					    for (var j = 0; j<arrlen; j++) {
					        if (categoryCalculation[i] == oldCategoryCalculation[j]) {
					        	oldCategoryCalculation = oldCategoryCalculation.slice(0, j).concat(oldCategoryCalculation.slice(j+1, arrlen));
					        }
					    }
					}
					
					
					// to get add elements
					for (var i = 0; i<categoryCalculation.length; i++) {
					    var newLength = categoryCalculation.length;
					    for (var j = 0; j<newLength; j++) {
					        if (oldCategory[i] == categoryCalculation[j]) {
					        	categoryCalculation = categoryCalculation.slice(0, j).concat(categoryCalculation.slice(j+1, newLength));
					        }
					    }
					}
					log.debug("toAdd",categoryCalculation);
					
					
					//To Remove from the category
					for (var j = 0; j < oldCategoryCalculation.length; j++) {
						
						// to create in category
						var categoryRec = record.load({
							type : 'commercecategory',
							id : oldCategoryCalculation[j],
							isDynamic : true
						});

						// to set in Item sublist items 
						var lineNumToRemove = categoryRec.findSublistLineWithValue({
							sublistId : 'items',
							fieldId : 'item',
							value:id
						});
if(lineNumToRemove>-1)
	{
	// to set the items
	categoryRec.removeLine({
		
		sublistId:'items',
		line:lineNumToRemove
		
	});
	}
						
						
						
						var catId = categoryRec.save({
							ignoreMandatoryFields : true
						});
					}
					
					//To add in the category
					for (var i = 0; i < categoryCalculation.length; i++) {
						
						// to create in category
						var categoryRec = record.load({
							type : 'commercecategory',
							id : categoryCalculation[i],
							isDynamic : true
						});

						// to set in Item sublist items 
						var lineNum = categoryRec.selectNewLine({
							sublistId : 'items'
						});

						// to set the items
						categoryRec.setCurrentSublistValue({
							sublistId : 'items',
							fieldId : 'item',
							value : id
						
						});
						categoryRec.commitLine({
							sublistId : 'items'
						});
						var catId = categoryRec.save({
							ignoreMandatoryFields : true
						});
					}
					

					}
			}

		} catch (e) {
			log.error("Err@ FN afterSubmit=", e.message);
		}

	}

	return {

		afterSubmit : afterSubmit
	};

});

Leave a comment

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