Set URL for item fields

Jira Code: PS 8

This task is to set URL of image files from a field in item record. The client needed full URL.

Workflow action script

/**
 * @NApiVersion 2.x
 * @NScriptType workflowactionscript
 */
/*******************************************************************************
 * CLIENTNAME:PARKSLOPE
 * PS -8 Item Workflow to set URL of imgs
 *************************************************************************
 * Date : 11/02/2019
 *
 * Author: Jobin & Jismi IT Services LLP
 * Script Description : This workflow action script is to set URL for 4 fields
 * On the item record under the tab Groupon you can see the 4 fields
 Main Image (Required) (AUTOFILL)
 Alternate Image 1 (Optional) (AUTOFILL)
 Alternate Image 2 (Optional) (AUTOFILL)
 Alternate Image 3 (Optional) (AUTOFILL)

 The way it is currently is that when you save the item record (after record submit) those 4 fields are autofilled with the filenames below;
 Item Image (under Primary Information)
 Other Image 1 (Marketplace tab, optional subtab)
 Other Image 2 (Marketplace tab, optional subtab)
 Other Image 3 (Marketplace tab, optional subtab)

 This is done in workflow 35 Groupon CSV

 The problem is that it autofills the filename. I need it to autofill with the URL of the file.
 * 
 *  Date created : 11/02/2019
 *
 * REVISION HISTORY
 *
 * Revision 1.0 ${11/02/2019} aj : created
 * 
 *
 ******************************************************************************/
define([ 'N/file', 'N/record', 'N/search', 'N/ui/serverWidget' ],
/**
 * @param {file} file
 * @param {record} record
 * @param {search} search
 * @param {serverWidget} serverWidget
 */
function(file, record, search, serverWidget) {

	/**
	 * Definition of the Suitelet script trigger point.
	 *
	 * @param {Object} scriptContext
	 * @param {Record} scriptContext.newRecord - New record
	 * @param {Record} scriptContext.oldRecord - Old record
	 * @Since 2016.1
	 */
	function onAction(scriptContext) {
		try {
			var itemRecord = scriptContext.newRecord;
			
			var Id = itemRecord.id;
			// to get Item Image  value
			var itemImg = itemRecord.getValue({
				fieldId : 'custitem_atlas_item_image'
			});

			var mainImg, altImg1, altImg2, altImg3;

			

			if (checkIfNull(itemImg)) {
				// to get URL
				var itemImgFile = file.load({
					id : itemImg
				});
				
				// to get the other img1
				mainImg = itemImgFile.url;
				
			}
			var otherImg1 = itemRecord.getValue({
				fieldId : 'custitem_fa_oth_img_url01'
			});
			if (checkIfNull(otherImg1)) {
				// to get URL
				var otherImg1File = file.load({
					id : otherImg1
				});
				
				altImg1 = otherImg1File.url;
			}

			// to get the other img2

			var otherImg2 = itemRecord.getValue({
				fieldId : 'custitem_fa_oth_img_url02'
			});

			if (checkIfNull(otherImg2)) {
				// to get URL
				var otherImg2File = file.load({
					id : otherImg2
				});
				
				altImg2=otherImg2File.url;
			}

			// to get the other img3

			var otherImg3 = itemRecord.getValue({
				fieldId : 'custitem_fa_oth_img_url03'
			});
			if (checkIfNull(otherImg3)) {

				// to get URL
				var otherImg3File = file.load({
					id : otherImg3
				});

				
				altImg3 = otherImg3File.url;
			}

			// to set main Img
			itemRecord.setValue({
				fieldId : 'custitemgrpnmainimage',
				value : checkForParameter(mainImg)

			});
			// to set Alternate img 1 
			itemRecord.setValue({
				fieldId : 'custitemgrpnimagealt1',
				value : checkForParameter(altImg1)

			});
			// to set Alternate img 2 
			itemRecord.setValue({
				fieldId : 'custitemgrpnaltimage2',
				value : checkForParameter(altImg2)

			});
			// to set Alternate img 3 
			itemRecord.setValue({
				fieldId : 'custitemgrpnaltimage3',
				value : checkForParameter(altImg3)

			});

		} catch (e) {
			log.error("Err # on Action ", e);
		}

	}
	/*******************************************************************
	 * To check whether a value exists in parameter
	 ******************************************************************/
	function checkForParameter(parameter) {
		try {
			if (parameter !== null && parameter !== undefined
					&& parameter !== "null" && parameter !== "undefined"
					&& parameter != "" && parameter != " ") {
				return "https://system.na3.netsuite.com" + parameter;
			} else {

				return "-";
			}
		} catch (e) {
			log.error("Err@ FN checkForParameter", e);
		}
	}

	/*******************************************************************
	 * To check whether a value exists in parameter
	 ******************************************************************/
	function checkIfNull(parameter) {
		try {
			if (parameter !== null && parameter !== undefined
					&& parameter !== "null" && parameter !== "undefined"
					&& parameter != "" && parameter != " ") {
				return true;
			} else {

				return false;
			}
		} catch (e) {
			log.error("Err@ FN checkIfNull", e);
		}
	}

	return {
		onAction : onAction
	};

});

Leave a comment

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