Is there a good way via client script to tell if t...
# suitescript
c
Is there a good way via client script to tell if the current line has been commited to the sublist yet? Trying to determine if the user is editing a new line or editing an existing line
I'm slowly going through and testing fields that might not populate until a line is commited, but haven't had any luck yet
c
Have you checked the
line
field? From what I remember on a recent project, it's not populated on a new line until it's committed.
c
Nada (but thank you), just tried it but it's undefined in either case. Might be one of those that doesn't populate until after it's saved. For more context, this is on an invoice, and i've tried the following fields: line id lineuniquekey sys_id sys_parentid
c
Ah, yeah I think in our use case it's only for existing records' lines
What entry point are you using?
c
fieldChanged, this is during record creation as well, editing might have different behavior
c
Could you utilize a hidden custom sublist field that you set via validateLine?
keanu thanks 1
c
at this point, that's probably the best way, yeah. Trying to check fields on the line doesn't seem to be giving me anything
c
The only other thing that comes to mind is making a map of the sublist on lineInit (like storing a selection of fields from each line) then comparing to that during fieldChanged to detect where the change occurs
perhaps by creating an object on window
l
ChatGPT 4 gave me this answers In NetSuite's client-side scripting, determining whether the current line in a sublist is new or an existing one being edited can be crucial for various validations and operations. Here's a general approach to achieve this: Using a Marker Field One common method is to use a hidden 'marker' field on each line of the sublist. This field can be used to indicate whether the line has been committed or not. 1. Add a Custom Field: ◦ Create a custom field in your record that is hidden from the user interface. ◦ This field will serve as a marker to determine if a line has been committed. 2. Set the Marker on Line Commit: ◦ In your client script, use the
validateLine
or
validateInsert
function to set the marker field whenever a line is added or edited. ◦ Example:
Copy code
function validateLine(scriptContext) {
    var sublistName = scriptContext.sublistId;
    if (sublistName === 'your_sublist_id') {
        // Set the marker field value
        var currentLine = scriptContext.currentRecord.getCurrentSublistIndex({ sublistId: sublistName });
        scriptContext.currentRecord.setCurrentSublistValue({
            sublistId: sublistName,
            fieldId: 'custpage_marker_field',
            value: 'committed',
            ignoreFieldChange: true
        });
    }
    return true;
}
Check the Marker on Field Change: • In the
fieldChanged
function, check the value of the marker field to determine if the current line is new or being edited. • Example:
Copy code
function fieldChanged(scriptContext) {
    var sublistName = scriptContext.sublistId;
    var fieldId = scriptContext.fieldId;

    if (sublistName === 'your_sublist_id' && fieldId === 'a_field_in_sublist') {
        var markerValue = scriptContext.currentRecord.getCurrentSublistValue({
            sublistId: sublistName,
            fieldId: 'custpage_marker_field'
        });

        if (markerValue === 'committed') {
            // Line has been committed
        } else {
            // New line or uncommitted line
        }
    }
}
Other Approaches • Record ID Check: If each line in the sublist is associated with a unique ID (like an internal ID), you can check if this ID exists or is set. New lines usually won't have an ID until they are committed. • Line Index Tracking: Maintain the index of the last committed line. Any line index greater than this indicates a new line. Considerations • The specific implementation details can vary based on the NetSuite record type and the structure of your sublist. • Ensure that your client script does not adversely impact the user experience with unnecessary complexity or performance overhead. • Always thoroughly test your client scripts in a sandbox environment before deploying them to production. These methods provide a basis for differentiating between new and existing lines in a sublist using client scripting in NetSuite.
and Bard gave me this- Unfortunately, there isn't a direct way in client scripts to determine if the current line has been committed to the sublist. However, there are several methods you can use to achieve a similar result: 1. Checking the line ID: • Client scripts have access to the current sublist line ID using
currentRecord.getCurrentSublistValue({ sublistId: 'your_sublist_id', fieldId: 'internalid' })
. • You can compare this ID with the initial ID you assigned to the line when it was first added. If they are the same, the line hasn't been committed yet. • This approach works well for newly added lines but may not be reliable for existing lines edited within the sublist. 2. Checking the
isEditMode
property: • Client scripts also provide a
currentRecord.isEditMode()
property that indicates whether the current record is in edit mode. • While this doesn't directly tell you about the sublist line, you can combine it with the previous approach to improve accuracy. • If
isEditMode
is true and the line ID is different from the initial ID, it implies an existing line being edited. 3. Using custom fields: • You can create a custom field on the sublist record to track the commit status of each line. • When adding a new line, set this field to "Uncommitted" and update it to "Committed" after the line is saved. • In your client script, check the value of this field to determine the line's commit status. 4. Using
validateLine
or
sublistChanged
scripts: • SuiteScript provides
validateLine
and
sublistChanged
entry points for client scripts. • The
validateLine
script is triggered before a line is added to the sublist, while
sublistChanged
is triggered after any changes to the sublist. • You can use these scripts to set a flag or custom field indicating that the line is new and hasn't been committed yet.
@Corey Schwoebel, I am not sure on above answer but just for your reference. Thanks.
n
• Client scripts have access to the current sublist line ID using
currentRecord.getCurrentSublistValue({ sublistId: 'your_sublist_id', fieldId: 'internalid' })
. 🙄
😆 2
a
I love AI, the amount of work AI is going to generate in the next 2 years for developers to fix will be very good, I think some developers could retire by 2030 if they go freelance and start refactoring and fixing code generated by AI.
💯 2
l
True
a
@Corey Schwoebel You should be able to read
lineuniquekey
from existing lines while new lines should not have that value. Another approach I like, especially with Client Scripts and 2.1 is to read in the pageInit the lines and create a global object which could be used as your index or whatnot to compare. So on pageInit you build this object with the existing lines. Then on line changes you check if you already have that line in your object and go from there.
c
I'm trying the custom field route to mark when a line is commited, but i might not be able to get the timing right with the triggers. I might wind up taking the global object route
a
I only advise using Client Scripts as a last resort, the overhead of operations in Client Scripts is totally insane (I personally think those are not very well designed). I remember a conference by @mattdahse where he did a deep dive into all the events that fire etc, not sure if he made that public as a paper or document at some point.
The custom field as suggested by ChatGPT is not a good approach, in my opinion: • You are adding a not necessary field that you now need to worry about, track, maintain, etc. • This will fail with something as simple as copying a line or creating a copy of an existing record.
keanu thanks 1