Hello everyone! I'm doing a UserEvent script to p...
# suitescript
k
Hello everyone! I'm doing a UserEvent script to populate the total quantity field on sales order. The script debugs the proper quantity, but the field isn't populated after creating/editing the transcation. Type is free-form text and display type is inline text. Could anyone explain why this isn't working as expected?
Copy code
const beforeSubmit = (scriptContext) => {
    try {
      // Initilize variable for total quantity
      var totalQuantity = 0;

      // Count number of lines in item sublist
      let objRecord = scriptContext.newRecord;
      let lineCount = objRecord.getLineCount({
        sublistId: 'item',
      });

      // For each line item, increment totalQuantity
      for (var i = 0; i < lineCount; i++) {
        lineLevelQuantity = objRecord.getSublistValue({
          sublistId: 'item',
          fieldId: 'quantity',
          line: i,
        });

        if (!isEmpty(lineLevelQuantity)) {
          totalQuantity += parseInt(lineLevelQuantity);
        }
      }

      // Total Quantity is 35
      objRecord.setValue({
        fieldId: 'custbody_surf9_total_qty',
        value: totalQuantity,
        ignoreFieldChange: false,
      });
     
    } catch (e) {
      log.debug({
        title: 'Error details',
        details: e,
      });
    }
  };
s
Try having the field you are saving into as numeric?
e
Seems like every time something like this happens to me, it’s because of permissions. Make sure the script “execute as role” has permissions to write to that field. I wish that NetSuite would throw an exception in this case, but typically it just suppresses the error and ignores the update.
k
Tried setting field type to integer and set 'execute as role' to both admin and current role.
message has been deleted
e
I would eliminate all of the surrounding code except for setting the value and just try setting a static value. That would rule out any permissions issue.
k
That failed too
e
could be another script or workflow overwriting your field, you could check the system notes after you enter a static value in the UI
e
look at the “Access” tab on the field and see what the “Default Access Level” is set to. it is possible to lock out writing to a field even for the Administrator role I believe.
k
The Default Access Level is Edit and there aren't workflow/script details in the system notes.
e
what is isEmpty?
k
Copy code
const isEmpty = (value) =>
    value === null ||
    false ||
    value === 'undefined' ||
    value === '' ||
    value === 'NaN';
e
You’ve probably already tried this, but can you log out a message at the top of the beforeSubmit to confirm that this script is being called when you expect it. I only see a log message on an exception in your code.
k
After saving the sales order, I received an error related to console not being define at beforeSubmit.
Here's the code before the entry point function:
Copy code
/**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 *
 * Author - Kenneth Jules
 */

define([], () => {
  /**
   * Defines the function definition that is executed before record is submitted.
   * @param {Object} scriptContext
   * @param {Record} scriptContext.newRecord - New Record
   * @param {Record} scriptContext.oldRecord - Old Record
   * @param {String} scriptContext.type - Trigger type; use values from context
   * UserEventType enum
   * @since 2015.2
   */
e
That looks fine. I was suggesting just using the normal logging functionality at the top of your beforeSubmit function to confirm it’s running.
Copy code
log.audit({
        title: 'UPDATING_TOTAL_QTY',
        details: 'Updating total quantity on sales order',
      });
d
@Kenneth Jules, hey, some notes on your
isEmpty
func:
Copy code
const isEmpty = (value) =>
    value === null ||
    false ||					// does nothing
    value === 'undefined' ||	// comparing against string 'undefined'
    value === '' ||
    value === 'NaN';			// comparing against string 'NaN'
have a read of: undefined - JavaScript | MDN NaN - JavaScript | MDN
🙌 1
k
log.audit confirmed the script is running.
Do I need to do anything else for the transaction? I'm currently editing and saving, no changes.
e
If you edit and save the field in the UI does it save it correctly?
r
Is some other script setting it? Try re-ordering the scripted record for that record type.
k
I was able to add edit and save, but the value was not stored.
How can I check the run order of scripted records?
e
Is store value checked on the custom field definition?
👍🏻 1
r
Customization → Scripting → Scripted Records. Hit edit on your record type. You can then drag scripts to reorder them.
k
Thanks! I see now.
I shifted the desired deployment to the top, but the result is still blank even after setting a value.
r
I would try the bottom. If something is setting it after your script, it will be lower in the queue.
k
It was at the bottom initially.
r
Log the value and type of totalQuantity?
b
make the field display normally instead of inline text and try setting it from the ui
k
Hello everyone, I was able to figure this out. Thank you for your help!