When working with Suitelets in NetSuite, a common approach to pass data from a record to a Suitelet page is by sending it through URL parameters. However, there are situations where you might not want to expose data in the URL or you simply want a cleaner approach.
Luckily, there’s a way to access record data directly in the Suitelet popup without explicitly passing it as parameters. This can be done using the window.opener object in client-side JavaScript, which allows the Suitelet to communicate with the parent record page.
How It Works
- Inject a hidden HTML field with script logic
- We add an INLINEHTML field to the Suitelet form that automatically runs JavaScript when the Suitelet loads. This script reads field values from the parent record using
window.opener.nlapiGetFieldValue(). - Pass the values to the Suitelet backend
- The script dynamically builds a hidden form with the extracted values and submits it to the Suitelet itself, making the data available on the server-side request.
- Send processed data back to the record
- Once the Suitelet finishes processing, it can push the results back into the parent record using
window.opener.nlapiSetFieldValue().
form.addField({
id: 'custpage_data_injector',
label: 'Data Injector',
type: serverWidget.FieldType.INLINEHTML
}).defaultValue = `
<script>
document.addEventListener('DOMContentLoaded', function () {
try {
var invJson = window.opener.nlapiGetFieldValue('custbody_jj_pieces_inventory_snapshot') || '[]';
var existingJson = window.opener.nlapiGetFieldValue('custbody_jj_pieces_tracking_json') || '[]';
var form = document.createElement('form');
form.method = 'POST';
form.action = window.location.href;
form.innerHTML += "<input type='hidden' name='custpage_inventorydata_json' value='" + encodeURIComponent(invJson) + "' />";
form.innerHTML += "<input type='hidden' name='custpage_existingdata_json' value='" + encodeURIComponent(existingJson) + "' />";
document.body.appendChild(form);
form.submit();
} catch (e) {
alert('Error loading inventory data from parent window.');
console.error(e);
}
});
</script>`;
And when you’re ready to send data back to the record:
context.response.write(`
<script>
window.opener.nlapiSetFieldValue(
'custbody_jj_pieces_tracking_json',
'${JSON.stringify(result).replace(/'/g, "'")}'
);
window.close();
</script>`);