1

I'm creating a NetSuite invoice in dynamic mode with SuiteScript 2.0, and when I run commitLine on my first item, it errors with 'Please enter a value for amount.' even though I have already supplied it using setCurrentSublistValue.

The relevant section of code is START CREATING ITEM through FINISHED CREATING ITEM, but I've included the full restlet in case the problem is in my setup.

Here's the JSON data I'm using to generate the item:

{"item":26,"amount":5706.48,"custcol_unit_price":"7.863035405","quantity":725735,"description":"July Impressions - 2019 Digital (April-July) - Custom Affinity (CAF) Dynamic CPM 7.5","line":1}

I'm logging a call to getCurrentSublistValue immediately after the setter, to ensure the system is accepting the value I send, and get 5706.48 back out for amount, so I believe setCurrentSublistValue is inserting it.

/**
 *@NApiVersion 2.x
 *@NScriptType Restlet
 */
define(['N/record', 'N/error', 'N/log'],
  function(record, error, log) {
    function post(context) {
      log.audit("invoice", context);
      var rec = record.create({ type: "invoice", isDynamic: true });

      for (var fldName in context) {
        if (context.hasOwnProperty(fldName)) {
          if (fldName === "trandate") {
            rec.setValue(fldName, new Date(context[fldName]));
          } else if (fldName === "items") {
            var items = context[fldName]

            for (var idx in items) {
              var item = items[idx]
              // START CREATING ITEM
              log.audit('item', item)

              rec.selectNewLine({ sublistId: 'item' });
              for (var itemFldName in item) {
                log.audit(itemFldName, item[itemFldName])
                rec.setCurrentSublistValue({
                  sublistId: 'item',
                  fieldId: itemFldName,
                  value: item[itemFldName]
                })
                log.audit("current value", rec.getCurrentSublistValue({ sublistId: 'item', fieldId: itemFldName }))
              }

              rec.commitLine({ sublistId: 'item' });
              // FINISHED CREATING ITEM
            }
          } else {
            rec.setValue(fldName, context[fldName]);
          }

        }
      }
      var recordId = rec.save();

      return { success: true, message: recordId }
    }

    return {
      post: post
    };
  }
);

This is the error I'm seeing in the logs (line 34 is the commitLine call):

{"type":"error.SuiteScriptError","name":"USER_ERROR","message":"Please enter a value for amount.","stack":["anonymous(N/serverRecordService)","post(/SuiteScripts/api/submitInvoice.js:34)"],"cause":{"type":"internal error","code":"USER_ERROR","details":"Please enter a value for amount.","userEvent":null,"stackTrace":["anonymous(N/serverRecordService)","post(/SuiteScripts/api/submitInvoice.js:34)"],"notifyOff":false},"id":"","notifyOff":false,"userFacing":false}

Thanks in advance for any insight!

quarks
  • 33,478
  • 73
  • 290
  • 513
Bill Gathen
  • 88
  • 1
  • 5
  • I just tried prepending a dollar sign to the amount and that changes the error to: "You have entered an Invalid Field Value $5706.48 for the following field: amount" – Bill Gathen Aug 29 '19 at 19:10

2 Answers2

5

Try setting the quantity to 1. System might be overwriting your value with a calculated value of quantity * amount when you commit the line.

Rusty Shackles
  • 2,802
  • 10
  • 11
  • Thanks! That wasn't precisely it, but I tried setting the quantity BEFORE setting the amount, and it worked (both with quantity of 1 and the actual quantity I need). You're a lifesaver. – Bill Gathen Aug 29 '19 at 20:36
3

You are setting the Amount first, then the Quantity, and you are not setting the Rate. Because you are working with the record in Dynamic mode, you will need to set field values in the exact same order as you would in the UI, otherwise your data will be off.

If you need to continue working in Dynamic mode, then you will likely need to do a few things:

  1. Ensure that sourcing happens when you set the Item field by setting the forceSyncSourcing option of setCurrentSublistValue to true. This should ensure that any columns dependent on the Item are set correctly.
  2. Ensure you are setting the Item columns in the correct order; probably Item, then Quantity, then Amount. This way, Dynamic mode should hopefully not recalculate your Amount.

I generally avoid this problem entirely by working in Standard mode instead of Dynamic. I have found it extremely rare that I need to work in Dynamic mode. In Standard mode, field order no longer matters as you let the server figure it out after you submit the entire record rather than in real-time as you set each field.

erictgrubaugh
  • 8,519
  • 1
  • 20
  • 28
  • Thanks! I'm using dynamic mode because all the examples in their docs use it, but standard mode does seem simpler. I'm currently having problems with the top-level values I'm setting (using record.setValue(fieldname, fieldvalue)) are being ignored. Could ordering be the issue there, as well? I'm adding them in this order: trandate, otherrefnum, tobeprinted, message, entity, customform, createdfrom But the UI navigates customform, entity, trandate, otherrefnum, tobeprinted, message – Bill Gathen Aug 30 '19 at 14:07
  • 1
    That was the problem there, as well! All my data is flowing through correctly now. Thanks again! – Bill Gathen Aug 30 '19 at 14:21
  • This is the best answer imo – jaycarleton Oct 27 '20 at 20:55