如何为NetSuite Suitelet子列表行按条件设置颜色高亮
Absolutely! You can absolutely highlight rows in your NetSuite static sublist when the difference between the current date and your audit date exceeds 100 days. Since you're working with SuiteScript 1.0, we'll need to combine server-side code (to populate the sublist) and client-side code (to apply the row styling)—server-side scripts can't directly manipulate UI styles. Here's a step-by-step solution tailored to your existing code:
Step 1: Update Your Server-Side Code
First, make sure your sublist includes the audit date field (even if you hide it) so the client script can access its value. We'll also attach a client script to the form to handle the highlighting logic.
var search = nlapiLoadSearch('customrecord_cseg_properties', 'customsearch52'); var columns = search.getColumns(); var sublist = form.addSubList('customsublist', 'staticlist', 'List of properties'); // Add all search columns to the sublist for(var i = 0; i < columns.length; i++) { var col = columns[i]; var fieldId = 'custcol_' + col.getId(); var field = sublist.addField(fieldId, col.getType(), col.getLabel()); // Optional: Hide the audit date column if you don't want it visible if(col.getId() === 'YOUR_AUDIT_DATE_FIELD_ID') { // Replace with your actual audit date field ID field.setDisplayType('hidden'); } } // Populate the sublist with search results var results = search.runSearch(); var lineNum = 1; results.forEachResult(function(result) { for(var j = 0; j < columns.length; j++) { var col = columns[j]; var fieldId = 'custcol_' + col.getId(); var value = result.getText(col) || result.getValue(col); sublist.setLineItemValue(fieldId, lineNum, value); } lineNum++; return true; }); // Attach your client script to the form (replace with your client script's ID) form.setScript('customscript_client_sublist_highlight');
Step 2: Create the Client-Side Script
This script will run when the page loads, calculate the date difference for each row, and apply highlighting to rows that meet your condition.
function pageInit() { var sublistId = 'customsublist'; var auditDateColId = 'custcol_YOUR_AUDIT_DATE_FIELD_ID'; // Match the field ID from server-side code var rowCount = nlapiGetLineItemCount(sublistId); for(var i = 1; i <= rowCount; i++) { var auditDateStr = nlapiGetLineItemValue(sublistId, auditDateColId, i); if(auditDateStr) { // Parse dates and calculate the day difference var auditDate = new Date(auditDateStr); var currentDate = new Date(); var timeDiff = Math.abs(currentDate.getTime() - auditDate.getTime()); var dayDiff = Math.ceil(timeDiff / (1000 * 3600 * 24)); // Highlight rows where difference exceeds 100 days if(dayDiff > 100) { var rowElement = document.getElementById(sublistId + '_row_' + i); if(rowElement) { // Apply inline style (or use a custom CSS class for cleaner code) rowElement.style.backgroundColor = '#ffcccc'; // Light red background // Optional: Add text color contrast rowElement.style.color = '#333'; } } } } }
Key Notes & Tips
- Field IDs: Replace
YOUR_AUDIT_DATE_FIELD_IDwith the actual internal ID of your audit date field from the saved search. - CSS Alternatives: Instead of inline styles, you can define a custom CSS class (e.g.,
.expired-audit-row { background-color: #ffcccc; }) and add it to the row element usingrowElement.classList.add('expired-audit-row'). Just make sure the CSS is loaded in your NetSuite account (via a custom HTML field or attached CSS file). - Date Parsing: NetSuite typically returns dates in a format that
new Date()can handle, but if you run into parsing issues, you may need to adjust the string format manually. - Script Deployment: Ensure your client script is deployed to the appropriate record type and has the necessary permissions.
内容的提问来源于stack exchange,提问作者Nikhil Gangji

