NetSuite SuiteScript 2.0客户端脚本saveRecord中Swal弹窗无返回值问题
Let's cut to the chase: your current code isn't working because SweetAlert runs asynchronously, but NetSuite’s saveRecord function expects a synchronous yes/no return value to decide whether to proceed with saving. By the time the user clicks "Yes" or "No", saveRecord has already finished running and returned undefined (which NetSuite treats as false, so the save never happens even if the user confirms).
Here's the corrected approach—we'll block the default save immediately, handle the Swal interaction, and manually trigger the save only when the user confirms:
function saveRecord(context) { // Grab the current record from the SuiteScript 2.0 context const currentRecord = context.currentRecord; const confirmationMessage = "Your custom confirmation text here"; // Replace with your actual message // Stop NetSuite's default save flow right away swal({ title: "Are you sure?", text: confirmationMessage, type: "warning", showCancelButton: true, confirmButtonColor: "#DD6B55", confirmButtonText: "Yes", cancelButtonText: "No", closeOnConfirm: false, closeOnCancel: false }, function(isConfirm) { if (isConfirm) { // Manually save the record if user confirms try { currentRecord.save({ enableSourcing: true, ignoreMandatoryFields: false }); swal("Saved!", "Your record has been submitted successfully.", "success"); } catch (error) { swal("Oops!", `Failed to save the record: ${error.message}`, "error"); } } else { swal("Cancelled", "Your record was not submitted.", "info"); } }); // Always return false to block the automatic save return false; }
Key Notes:
- Blocking the default save: Returning
falseimmediately ensures NetSuite doesn't attempt to save before the user interacts with the alert. - Manual save on confirmation: We use SuiteScript 2.0's
currentRecord.save()API to trigger the save only when the user clicks "Yes". This aligns the asynchronous Swal interaction with NetSuite's save workflow. - Error handling: Added a try/catch block to catch any save failures and notify the user clearly.
- SuiteScript 2.0 compliance: Make sure you're using the
contextparameter provided tosaveRecord—it's the proper way to access the current record in SuiteScript 2.0 client scripts.
Just double-check that you've loaded the SweetAlert library correctly in your script deployment (either via a dependency or included in your script's files) and you should be good to go.
内容的提问来源于stack exchange,提问作者DEVload-prog

