Netsuite – Workflow action scripting

I had to write a workflow action recently and I wanted to detail some thoughts on the process.

I had a requirement whereby a custom record attached to a support case needed to update the case upon the record being edited. There was already a workflow which was handling changes to the custom record, so I decided to use a workflow action.

Having never written a WF Action script before, I found it a bit tricky to work out exactly what was required to make it work in the system – so I have detailed some points below, as well as including the script too for anyone who needs to do something similar

  • The action script must return a value in order for it to work (see my return 1)
  • The script will always need a parameter to work. This should be defined on the workflow setup, and should be retrieved at the start of the script
  • Ensure you feed the script with the parameter in the workflow..!
  • Use the Netsuite Debugger tool to debug. As this is a server script, the execution log will not help
  • I got caught out whilst setting field values in the loaded record. See below, on line xx I got it to work by using record.submitFields and not by loading the record with record.load and using record.setValue to set the values – this doesn’t seem to work. Only submitFields worked – see SuiteAnswers article on this
    • record.submitFields is neat in the fact that its a function that can take a record type and id, as well as at the same time setting field values all in one go – handy
/**
 * @NApiVersion 2.x
 * @NScriptType WorkflowActionScript
 */
define(['N/runtime', 'N/record'],
    function(runtime, record) {
        function onAction(scriptContext) {
			
				var currentScript = runtime.getCurrentScript();
				var param = currentScript.getParameter({
					name : "custscript_refcase"
				});     
				
				/* NOTE - this script didn't work (as it is commented out) but I will leave it in for brevity
				var objRecord = record.load({
				type: record.Type.SUPPORT_CASE, 
				id: param,
				isDynamic: true,
				});
          
          		 /*objRecord.setValue({
                  fieldId: 'custevent_case_is_esc',
                  value: false
              });
          
          objRecord.setValue({
                  fieldId: 'custevent_case_curr_esc',
                  value: ''
              }); */
          
				record.submitFields ({
					type: record.Type.SUPPORT_CASE, 
					id: param,
					values: {
						custevent_case_is_esc: false,
						custevent_case_curr_esc: ''
					}
				});          
         
            return 1;
        }
        return {
            onAction: onAction
        }
    });