SuiteScript > 添加按钮,然后转换记录 > 出现错误

问题描述 投票:0回答:1

我目前正在寻找 NetSuite SuiteScripts 的备忘单(如果有)。当我按下按钮时,我的代码出现错误。

我的代码功能是向一条记录添加一个按钮,然后将该记录转换为另一条记录。不幸的是,我在控制台中收到此错误Uncaught ReferenceError:transformToCreditNote未定义

/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 * @NModuleScope SameAccount
 */

define(['N/ui/serverWidget', 'N/record', 'N/url'], function(serverWidget, record, url) {
    function beforeLoad(context) {
        if (context.type === context.UserEventType.VIEW) {
            var form = context.form;

            // Create a button to transform to Credit Memo
            form.addButton({
                id: 'custpage_transform_to_credit_note',
                label: 'Transform to Credit Note',
                functionName: 'transformToCreditNote'
            });

            // Define the function to transform and redirect
            function transformToCreditNote() {
                var currentRecord = record.load({
                    type: record.Type.INVOICE,
                    id: context.newRecord.id
                });

                // Transform the Sales Invoice to a Credit Memo
                var creditMemo = currentRecord.transform({
                    fromType: record.Type.INVOICE,
                    toType: record.Type.CREDIT_MEMO
                });

                // Save the Credit Memo
                var creditMemoId = creditMemo.save();

                // Redirect to the newly created Credit Memo
                var creditMemoURL = url.resolveRecord({
                    recordType: record.Type.CREDIT_MEMO,
                    recordId: creditMemoId,
                    isEditMode: true,
                });

                // Redirect to the Credit Memo
                window.location.href = creditMemoURL;
            }
        }
    }

    return {
        beforeLoad: beforeLoad
    };
});

我正在尝试向记录添加一个按钮,然后单击时它将转换记录并将用户重定向到该记录。

suitescript2.0
1个回答
0
投票

这里我将从ECMA脚本的角度解释一下为什么会发生错误。

您调用

form.addButton()
方法并向其传递字符串,而不是函数。并且
addButton()
方法的范围是由定义它的位置定义的,而不是由调用它的位置定义的。

.addButton()
运行时,它只看到
'transformToCreditNote'
字符串,但在其作用域中没有具有这样名称的函数(因为它仅存在于
function beforeLoad
声明的作用域中),这就是为什么你得到ReferenceError 错误。


这是您需要执行的操作来摆脱 Uncaught ReferenceError:transformToCreditNote is not Defined 错误。

您需要在客户端脚本中定义点击处理函数,并将其作为属性设置在

form
对象上。据我所知,这是必须的(抱歉,无法提供任何文档参考)。

所以你需要有2个脚本:

  • 用户事件一,它将添加按钮并在
    form
    上设置客户端脚本的路径。
  • 客户端,它将定义处理程序

这是您的 UserEvent 脚本,将其部署在您希望在其页面上有按钮的记录上(记下客户端脚本的路径,您将其放置在 SuiteScripts 目录(以及子目录,如果需要)中):

/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 * @NModuleScope SameAccount
 */

define([], function() {
    'use strict';
    function beforeLoad(context) {
        if (context.type === context.UserEventType.VIEW) {
            var form = context.form;
            form.clientScriptModulePath = '/SuiteScripts/my-client-script.js';
            // Create a button to transform to Credit Memo
            form.addButton({
                id: 'custpage_transform_to_credit_note',
                label: 'Transform to Credit Note',
                functionName: 'transformToCreditNote'
            });
        }
    }

    return {
        beforeLoad: beforeLoad
    };
});

这是客户端脚本:

/**
 * @NApiVersion 2.x
 * @NScriptType ClientScript
 * @NModuleScope SameAccount
 */

define(['N/record', 'N/url'], function(record, url) {
    'use strict';
    return {
      //You must have this
      pageInit: function() {},
      transformToCreditNote: function() {
          var currentRecord = record.load({
              type: record.Type.INVOICE,
              id: context.newRecord.id
          });

          // Transform the Sales Invoice to a Credit Memo
          var creditMemo = currentRecord.transform({
              fromType: record.Type.INVOICE,
              toType: record.Type.CREDIT_MEMO
          });

          // Save the Credit Memo
          var creditMemoId = creditMemo.save();

          // Redirect to the newly created Credit Memo
          var creditMemoURL = url.resolveRecord({
              recordType: record.Type.CREDIT_MEMO,
              recordId: creditMemoId,
              isEditMode: true,
          });

          // Redirect to the Credit Memo
          window.location.href = creditMemoURL;
      }
    };
});

不能保证脚本的正确性,但是提到的错误应该消失了

© www.soinside.com 2019 - 2024. All rights reserved.