I am trying to determine why the field "Due Date" ...
# suitescript
a
I am trying to determine why the field "Due Date" is being automatically set when I create an invoice. I can't seem to find a client script responsible. What else could be setting it? It's definitely happening in the client, when I create a new invoice. After a rather lengthy delay, 5-6s, the due date field autopopulates in the UI.
c
Does it populate after you set the Customer? It's probably being populated based on the invoice date plus whatever days are determined by the payment terms.
e
^ This is most likely. You could also check client Workflows.
a
@Clay Roper is that native Netsuite behavior? Can it be disabled?
c
@Aaron Lozier As far as I know, it's native behavior. As for disabling, I'm curious about the business need to prevent setting due dates based on customer payment terms.
I can only speak for our NetSuite environment, but even when a customer has no value set in their terms field, the invoice defaults the due date to the same date as the transaction date. I don't believe we have any special settings or customizations in this area.
z
Due Date is calculated with "formula" = Invoice date + Terms day. Terms is sourced from Customer. Recalculated every time when the Customer or Payment term are changed. It can't be disabled. But you have two ways to set your logic: 1. If you want zero day between Invoice and Due date, you can set one Payment term for all customers 2. Create client side workflow (or client script) setting the Due date I realized the second option, even modified formula because the client has custom field "delivery date" different than invoice date
a
@Clay Roper just that we want to implement some more nuanced logic around due date. We have customers that sometimes pay by check, sometimes prepay, sometimes ACH debit. So depending upon their payment method for a given SO, the due date will be calculated differently
c
@Aaron Lozier That makes sense! Zoran is right about client script, which you could use to customize the due date based on your business logic after sourcing the customer on the invoice.
z
@Aaron Lozier it seems to be standard Payment terms
a
I REALLY appreciate you guys helping me understand what's going on. I guess my last question is whether there is a best practice surrounding the potential "race condition" here? Like any way to ensure my client script fires only after the due date gets set by Netsuite's native behavior?
z
c
If you prefer SuiteScript, look into postSourcing
z
a
That's exactly what I was looking for. Thanks so much!
z
And take care about what @Clay Roper wrote > You have two kinds of events: After field edit and After Field sourcing sourcing is important to wait for “background” changes. For example, when you change the value for Customer (List/Record), NetSuite sources for many other fields like subsidiary, term …. and you need to wait sourcing is finished to have actual values
c
@Zoran R-DATAGRAM Thank you for your detailed responses! Just to split the hair in front of me: "After Field Edit" and "After Field Sourcing" pertain to Workflow Triggers, whereas the "postSourcing" I referred to is the entry point in SuiteScript. I recommended this since OP originally posted in #C29HQS63G. @Aaron Lozier I hope this thread gives you enough to go on to achieve your goals!
❤️ 1
a
Absolutely! Very helpful. Now my only issue is I feel guilty for not contributing more to this channel. I'll work on that 🙂
👍 1
z
@Clay Roper yes, you are correct. Behind the scenes, each workflow is converted to standard SuiteScript. All that you put in workflow’s “After Field Sourcing” is going to be converted into “postSourcing” entry point of ClientScript.
a
So... initially this seemed to be working perfectly, but I don't know if something changed behind the scenes or if I only thought it was working... Take this very simplified CS script:
Copy code
/**
 * @NApiVersion 2.1
 * @NScriptType ClientScript
 */
define([],
    () => {
        return {
            saveRecord: () => {},
            fieldChanged: () => {},
            postSourcing: (context) => {
                const {currentRecord} = context;
                currentRecord.setValue({
                    fieldId: 'duedate',
                    value: new Date()
                });
                console.log(`Due Date: ${currentRecord.getValue({fieldId: 'duedate'})}`);
            },
            pageInit: () => {}
        }
    });
So this should just set the due date to today after sourcing, right?
But what happens is the field appears to be set to 2/12 (which I assume is calculated from the terms, as you guys pointed out) but the result of the console.log statement is:
Due Date: Fri Feb 09 2024 00:00:00 GMT-0700 (Mountain Standard Time)
So it is somehow setting the value but not showing the value??
c
Maybe the text and value aspects of the date field are decoupled -- have you tried saving the record to see if it saves correctly?
a
Well, for my use case, it doesn't really matter
I want to populate the due date field with what the logic would determine based on the payment terms, so that the user could change it if they wanted
c
swinging a bit here, but maybe try "setText" with a MM/DD/YYYY formatted string. Are you familiar with N/format?
a
No... but I tried setting text and still no luck. I even set a different date field on the page and it works fine. It kind of feels like setting due date with a client script is just not possible
c
Your requirements here are that the due date should be set to a date you calculate based on particular logic, then leave that field open to be manually edited before saving?
a
Yeah. But I just discovered something interesting. If I set the due date in the pageInit method, it works
postSourcing not even required!
c
Do you know the customer in pageInit?
a
Yes, by way of the createdfrom (SO) record
And if there isn't an SO associated I'm not worried about it
💯 1
c
Got it, so you're not doing standalone invoices where the customer is selected
a
The customer could be changed but I don't know why they would
c
Sounds like you landed on the right solution!
a
We generally just generate invoices right from SOs
Yeah it's really interesting
Thank you again!
c
Glad you got it sorted
a
It's working like a charm now
z
Hi @Aaron Lozier here is a sample of my ClientScript for a similar purpose. Just check out how to realize condition WHAT FIELD is changed
Copy code
function fieldChanged(context) {
  var currentRecord = context.currentRecord;
  var field = context.fieldId;
  if (field === 'postingperiod') {
    var period = currentRecord.getValue('postingperiod');

    var endDate = search.lookupFields({
      type: search.Type.ACCOUNTING_PERIOD,
      id: period,
      columns: ['enddate']
    }).enddate;

    endDate = format.parse({
      value: endDate,
      type: format.Type.DATE,
      timezone: format.Timezone.EUROPE_BUDAPEST
    });

    currentRecord.setValue({
      fieldId: 'custbody_popdv_datum',
      value: endDate
    });
  }
}
From Oracle docs ..Note: The event is not triggered by field changes for a field that does not have any cascaded fields…
…and finally about pageInit… Important: Client scripts only execute in edit mode. If you have a deployed client script with a pageInit entry point, that script does not execute when you view the form. It executes when you click Edit.
a
Thank you @Zoran R-DATAGRAM! All very helpful info