Suitelet – callback to original page
I recently had a requirement sent over to me whereby a user needed to select something in a pop-up window, which was then sent back to the original transaction page. Once back at the transaction page, new lines needed to be added to the transaction with the selected data entered.
In Suitescript, the first part can be easily achieved by adding a button via a User Event script, which has a function to open a Suitelet window. The difficulty is working out how to post back to the original page and perform an action
window.opener.require(['N/currentRecord'], function(currentRecord) {
var arrItems = JSON.parse(localStorage.getItem("arrItems"));
for (x in arrItems) {
var objRecord = currentRecord.get();
objRecord.selectNewLine({
sublistId: 'item'
});
objRecord.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'item',
value: arrItems[x].intid,
forceSyncSourcing: true
});
objRecord.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'quantity',
value: arrItems[x].qty,
forceSyncSourcing: true
});
objRecord.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'rate',
value: arrItems[x].price,
forceSyncSourcing: true
});
objRecord.commitLine({
sublistId: 'item'
});
}
});
The example above takes some selected information from the Suitelet (stored in the browser localStorage) and enters it as lines on the sublist in the original window. The code is entered as a client side function either inline inside the suitelet, or as a client script linked to the form – the saveRecord function would execute this as the Suitelet window was submitted
As the function is passed an array through the localStorage variable, we can take this array and add/set/commit each line on the transaction sublist.