netsuite returns this error: org.mozilla.javascrip...
# suitescript
s
netsuite returns this error: org.mozilla.javascript.EcmaError: ReferenceError: "format" is not defined. (/SuiteScripts/altera_data_ue.js#55) What is wrong? Line 55, in this case, is this: var newAcceptableDate = format.format
Copy code
var d = new Date(transDueDate);
    var newDueDate = d.setDate(d.getDate() + transTerms);
   
    var newAcceptableDate = format.format({
      value: newDueDate,
      type: format.Type.DATE
    })

  
    curRec.setValue({
      fieldId: 'duedate',
      value: newAcceptableDate,
      ignoreFieldChange: true
      // opcional, o padrão é falso
    });
b
Add N/format module at the top of your script along with other modules like
Copy code
define(["N/record", "N/render", "N/format]
😁 1
s
Thanks
b
The code you shared still has the same problem from yesterday
N/format will not help you
s
The problem now is that it doesn't return any more errors, but the date doesn't change
I don't know if the problem is in the logic, even because I have to make some changes to the code.
b
What does the code look like now
s
I haven't changed anything
still the same
b
As in all the code in your script
You made several errors and i want to know which ones i still have to go over
s
Copy code
/**
 *@NApiVersion 2.x
 *@NScriptType UserEventScript
 */
define(['N/record', 'N/format', 'N/render'], function (record, format, render) {

  function beforeLoad(context) {

  }

  function beforeSubmit(context) {

  }

  function afterSubmit(context) {

    //load record
    var curRec = context.newRecord; //can substitute "context.oldRecord", or "currentRecord.get();"
  
    //get current due date
    var transDueDate = curRec.getValue({fieldId: 'duedate'});
  
    //get the terms, this will likely come as an internal id. use getText if you want the text.
    var transTerms = curRec.getValue({fieldId: 'terms'});
  
    //empty string to hold terms as a number of days
    var addtlDays;
  
    //transform the internal id to terms as a number of days
    switch (transTerms){
      case 1: // Ex: 1 = internal id for term "Net 15"
        addtlDays = 15;
        break;
  
      case 2: // Ex: 2 = internal id for term "Net 30"
        addtlDays = 30;
        break;
  
      //add additional case statements as needed
  
      default:
        addtlDays = 0;
    }
  
    //calculuate the new due date
    var d = new Date(transDueDate);
    var newDueDate = d.setDate(d.getDate() + addtlDays);

    var NewAcceptableDate = format.format({
      value: newDueDate,
      type: format.Type.DATE
    })
  
    //set the new due date
    curRec.setValue({
      fieldId: 'duedate',
      value: newAccept,
      ignoreFieldChange: true //optional, default is false
    });
  }

  return {
    beforeLoad: beforeLoad,
    beforeSubmit: beforeSubmit,
    afterSubmit: afterSubmit
  }
});
this is the code
Now it returns this error: invalid date value (must be DD/MM/YYY)
b
back to the original error
s
Exactly
b
there are 2 ways to set a date field
the first is Record.setValue using a Date as the value
the second is _Record.setText_, using a string as the text
for the second option, the text must match the current user's date format settings, which usually involves using _format.format_ to convert a Date into a string
although the error message you are receiving is not as useful as it could be, its saying that you are not setting the duedate with a Date
using format.format is not helping you here since at best it returns a string, which is not a Date
s
I don't have that exact date. What I want to do is access the date in this field and manipulate it
b
you are also using format.format wrong, since setDate also does not return a Date
s
Do I necessarily need to know a field date?
Because the dates are different for each invoice.
b
the list of problems is actually not complete yet
your code basically never had a chance at setting the duedate, afterSubmit's newRecord is read only
my recommendation is to start smaller, settle for setting the duedate to today's date
then move onto your date logic
s
So it's not possible to do it the way I want it? I have to set a date, correct?
Do I have to set a date and then apply this logic?
b
You need to use setValue with a Date
You need to be more specific about what you mean by this logic
You can also use setText with a string
s
Where do I use setText in the code block? Only after using setText, can I use format.format?
b
Using setText will make your task harder not easier
If you still want to use it, then you would need to use it before setText
Primarily because you need its return value to use with setText
s
Rereading the conversation, you said to set the deadline for today's date and then apply that logic. Right? But would that mess up the original billing date?
b
Yes, do development on test records
Its not sane to do it on production data
s
Using setDate(), I would set a value for my date, correct? It's easier and more practical that way, isn't it?
Even with invoice dates being varied, should I set the dates for today and apply logic?
I know my questions are boring, but I learn by asking a lot. 😄
b
Go over the Date documentation
Creating a Date representing today should be easy
s
Okay. I will try here. Thank you very much 😄
155 Views