Question about an Invoice After Submit User Event ...
# suitescript
s
Question about an Invoice After Submit User Event script (created by NS PS): The person who developed this script is loading the invoice record in the afterSubmit function, just to get the value of
amountremainingtotalbox
This is slowing things way down for us. So, the question is, is there some reason that value can’t be retrieved from newRecord directly? Is there a more efficient way to get the same value? The record load is almost doubling our invoice save times. The developer no longer works for NetSuite, as of a week or two ago, so we can’t even question that person about it now. Here is the beginning of the code:
a
@scottvonduhn Loading the record in the afterSubmit is believe or not a common practice in some scenarios, if you update that Invoice let say by an edit operation and the total amount changes you may not get the right value in the newRecord. Another reason to do that maybe that that scripts needs to get that value all the time which means it needs to consider XEDIT.
For a NetSuite professional service guy I would expect him to have a line like this: `var invoice_load = record.load(
context.newRecord
)`
s
I’m actually not convinced this script would need to run in an XEDIT or inline edit scenario. It should mainly be running upon creation of a new invoice. It provides very little value for further edits to existing invoices.
a
@scottvonduhn You can potentially update that but you need to find a reliable way to come up with the Invoice Total, you need to think about taxes, discounts, etc...
s
The entire purpose of the script is to apply open customer payments to invoices as they are created. Less than 1% of customers even have any. Maybe moving the search for overpayments to the beginning and exiting if none are found, would improve this in the majority of cases. I am assuming that search would be faster than the invoice load, of course.
i also wonder if we could get the remaining balance due from a search or SuiteQL query, since the script doesn’t do anything else with the record other than pul that value.
d
I think the problem is that (at least according to the record browser)
amountremainingtotalbox
is not available as a search column. So something like
Copy code
search.lookupFields({type: "invoice", id: rec.id, columns: "amountremainingtotalbox"});
would result in a SSS_INVALID_SRCH_COL error. Also, in afterSubmit newRecord only contains the values that have changed on the record, the others are empty. Since you said this is only for a customers with open customer payments you could consider checking if said customer has open customer payments and returning before loading the invoice if they dont
s
Yeah, i think checking for open payments before the invoice load is the best way to improve performance, and is just a matter of re-ordering the code, so hopefully it’s a fairly safe change.
148 Views