How to Clear the List/Record Field using SuiteScript

Description: Ability to clear the value on a field using SuiteScript features.

Solution

Is necessary to load a existent record and to also submit after clearing the values

  1. Navigate to Customization Scripting Scripts New
  2. Click the (+) icon to upload the Script File with the sample code below
  3. Click Create Script Record
  4. Click Suitlet as the 1.0 Script Type
  5. Basic Information:
  6. Name: Enter Script Name 
  7. ID:Enter the Script ID
  8. Function:Enter clearFieldOnRecord
  9. Click Save & Deploy
  10. Basic Information:
  11. Title: Enter Clear Field 
  12. ID: Enter Deployment ID 
  13. Status:Select Released
  14. Log Level:Select Debug
  15. Execute As Role:Select Administrator
  16. Click Save

Example Using SuiteScript 1.0:

/**
 * Module Description
 *
 * Version    Date            Author           Remarks
 * 1.00       DD/MM/YYYY      author            SA
 *
 */

/**
 * @param {‌nlobjRequest} request Request object
 * @param {‌nlobjResponse} response Response object
 * @returns {‌Void} Any output is written via response object
 */
function clearFieldOnRecord(request, response) {‌
	try {‌
		var recTrans = nlapiLoadRecord('salesorder', 157);

		// Using JS null keyword
		recTrans.setFieldValue('custbody_field', null);

		// Passing a empty string
		recTrans.setFieldValue('custbody_field', '');

		nlapiSubmitRecord(recTrans);
	} catch (error) {‌
		nlapiLogExecution('DEBUG', 'The package sublist is empty', '0');
	}
}

2.Example  SuiteScript 2.0.

  1. Navigate to Customization Scripting Scripts New
  2. Click the (+) icon to upload the Script File with the sample code below
  3. Click Create Script Record
  4. Basic Information:
  5. Name: Enter Script Name 
  6. ID:Enter the Script ID
  7. Click Save & Deploy
  8. Basic Information:
  9. Title: Enter Clear Field 
  10. ID: Enter Deployment ID 
  11. Status:Select Released
  12. Log Level:Select Debug
  13. Execute As Role:Select Administrator
  14. Click Save
/**
 * @NApiVersion 2.0
 * @NScriptType Suitelet
 * @NModuleScope SameAccount
 */
define(['N/record']
/**
 * @param {‌record} record
 */, function (record) {‌
	/**
	 * Definition of the Suitelet script trigger point.
	 *
	 * @param {‌Object} context
	 * @param {‌ServerRequest} context.request - Encapsulation of the incoming request
	 * @param {‌ServerResponse} context.response - Encapsulation of the Suitelet response
	 * @Since 2015.2
	 */
	function clearFieldOnRecord(context) {‌
		try {‌
			var salesOrder = record.load({‌
				type: record.Type.SALES_ORDER,
				id: 157,
				isDynamic: true
			});

			// Using JS null keyword
			salesOrder.setValue({‌
				fieldId: 'memo',
				value: null
			});

			// Passing a empty string
			salesOrder.setValue({‌
				fieldId: 'memo',
				value: ''
			});

			var recordId = salesOrder.save({‌
				enableSourcing: true,
				ignoreMandatoryFields: true
			});
			log.debug({‌ title: 'recordId', details: recordId });
		} catch (error) {‌
			log.debug({‌ title: 'Catch Error', details: error });
		}
	}

	return {‌
		onRequest: clearFieldOnRecord
	};
});

Leave a comment

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