I want this script to highlight component rows wit...
# ask-stanley-ai
a
I want this script to highlight component rows with Gals as units and it is not working. Can you adjust? /** * @NApiVersion 2.1 * @NScriptType ClientScript */ define(['N/currentRecord'], function(currentRecord) { /** * pageInit event: runs when the record page is initialized. */ function pageInit(context) { log.debug("pageInit triggered."); // Delay execution to allow the sublist to render fully. setTimeout(highlightComponentRows, 1500); } /** * Iterates through the 'component' sublist lines (from the record's data) * and highlights the corresponding DOM row if the units field equals "Gals". */ function highlightComponentRows() { var rec = currentRecord.get(); var lineCount = rec.getLineCount({ sublistId: 'component' }); log.debug("Number of component lines", lineCount); for (var i = 0; i < lineCount; i++) { // Retrieve the unit value from the current record's sublist line. var unitVal = rec.getSublistValue({ sublistId: 'component', fieldId: 'units', line: i }); log.debug("Line " + i + " unit value", unitVal); // Check if the unit value equals "Gals" if (unitVal === "Gals") { // Construct the expected DOM id for the sublist row. // Adjust this pattern if your page uses a different row id format. var rowId = "rowcomponent_" + i; var rowElem = document.getElementById(rowId); if (rowElem) { rowElem.style.backgroundColor = "yellow"; log.debug("Highlighted row " + i + " with id " + rowId); } else { log.debug("DOM element not found for row " + i + " with id " + rowId); } } } } return { pageInit: pageInit }; });
c
AI generated answer (using internal Slack threads):
Based on the conversations, it seems like the issue is with the timing of the execution of the
highlightComponentRows
function. The function is set to execute after a delay of 1500ms (1.5 seconds) to allow the sublist to render fully. However, if the sublist takes longer than this to render, the function will execute before the sublist is fully loaded, and it won't be able to find the rows to highlight. One potential solution could be to increase the delay time. However, this might not be a reliable solution as the rendering time can vary. A better approach might be to use an event that triggers after the sublist is fully loaded. Unfortunately, NetSuite does not provide such an event out of the box. Here's a modified version of your script that uses a setInterval to periodically check if the sublist is loaded, and once it is, it calls the
highlightComponentRows
function and clears the interval:
Copy code
javascript
/**
 * @NApiVersion 2.1
 * @NScriptType ClientScript
 */
define(['N/currentRecord'], function(currentRecord) {

    /**
     * pageInit event: runs when the record page is initialized.
     */
    function pageInit(context) {
        log.debug("pageInit triggered.");
        // Start checking if the sublist is loaded every 500ms.
        var checkSublistLoaded = setInterval(function() {
            var rec = currentRecord.get();
            var lineCount = rec.getLineCount({ sublistId: 'component' });
            // If the sublist is loaded (i.e., it has at least one line), highlight the rows and stop checking.
            if (lineCount > 0) {
                highlightComponentRows();
                clearInterval(checkSublistLoaded);
            }
        }, 500);
    }

    // ... rest of your code ...

    return {
        pageInit: pageInit
    };
});
This approach should ensure that the
highlightComponentRows
function is called as soon as the sublist is loaded, regardless of how long it takes to load. ****** Disclaimer: This response was generated by an artificial intelligence assistant based on previous conversations from multiple Slack channels, and may contain inaccuracies or mistakes. ******
Slack thread 1: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.848714
Slack thread 2: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.841803491
Slack thread 3: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.841772079
Please provide your feedback on this answer.