ADD ALERT MESSAGE IN FORM USING USER EVENT SCRIPT

This script helps to use N/ui/message in the serverside script (Using a User event script). For showing alert, we have to create a new Inline Text field using the user event script. Write client script for UI message in HTML format. Apply script to the created field.

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

define(["N/record", "N/ui/serverWidget"], function (record, serverWidget) {

	function beforeLoad(context) {
		try {
			var entryString = context.newRecord.getValue({ fieldId: 'entryformquerystring' });
			var indexOfiscustom = entryString.indexOf('adjust');
			if (indexOfiscustom > 0) {
				var request = context.request;
				var adjustVal = request.parameters.adjust;
				log.debug("adjustVal", adjustVal);
				if (adjustVal == "true") {
				var soForm = context.form;
 				var msgField = soForm.addField({
            	id: "custpage_alertonview_adjust",
            	label: "Adjusted",
            	type: serverWidget.FieldType.INLINEHTML,
            	});
            	var html = '<script>';
            	    //html += 'alert("Quantity adjusted due to the available quantity of item")';
            	    html += 'require([\'N/ui/message\'], function (message){';
	                html += 'var onViewMessage = message.create({'; // Creates a message
	                html += 'title: \'Adjusted\', '; // Sets message title
	                html += 'message: \'Some item quantities have been adjusted due to the available quantity changing since you added the item to your cart\', '; // Sets the message content
	                html += 'type: message.Type.INFORMATION'; // Sets the type of the message using enum
	                html += '}); ';
                    html += 'onViewMessage.show(25000);'; // Sets the amount of time (in ms) to show the message
                    html += '})';
                    html += '</script>';
					msgField.defaultValue = html;
				}
			}

		}
		catch (err) {
			log.debug("error @ berforeLoad", err)
		}
	}
	return {
		beforeLoad: beforeLoad
	}


})

Leave a comment

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