You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NetSuite SuiteScript 2.0客户端脚本saveRecord中Swal弹窗无返回值问题

Fixing SweetAlert Return Value Issue in NetSuite SuiteScript 2.0 Client Script's saveRecord Function

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 false immediately 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 context parameter provided to saveRecord—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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 21:47:30