简体   繁体   English

使用自定义模块创建 NetSuite 脚本时出现“执行定义回调时所有 SuiteScript API 模块不可用”

[英]Getting “All SuiteScript API Modules are unavailable while executing your define callback” When Creating NetSuite Script With Custom Module

When trying to create a new script record within NetSuite, I get the error "Fail to evaluate script: All SuiteScript API Modules are unavailable while executing your define callback".尝试在 NetSuite 中创建新脚本记录时,我收到错误“无法评估脚本:执行定义回调时所有 SuiteScript API 模块不可用”。 I can't find any real information on what may cause this, and I can't see anything in my custom module that looks suspicious.我找不到任何可能导致此问题的真实信息,并且在我的自定义模块中看不到任何看起来可疑的东西。 I can't post the code here as the module is nearly 2,000 lines in length and has some proprietary code in it.我无法在此处发布代码,因为该模块的长度接近 2,000 行,并且其中包含一些专有代码。 As it was with another custom module I built that had issues at the "Upload Script File" stage, if I remove reference to the module in the script the process continues, and then I can go back to the script and return the module reference, wherein everything seems to work correctly afterward.正如我构建的另一个在“上传脚本文件”阶段出现问题的自定义模块一样,如果我在脚本中删除对模块的引用,过程将继续,然后我可以 go 返回脚本并返回模块引用,之后一切似乎都正常工作。

The only information I found that seemed useful was that the error could be caused by referencing a module outside of the define callback, but that isn't the case.我发现似乎有用的唯一信息是错误可能是由在定义回调之外引用模块引起的,但事实并非如此。 The module has two large objects constructed within, and they're returned from the callback.该模块内部构造了两个大对象,它们从回调中返回。 The only other thing I can think of is that this module calls the other custom module, but I haven't seen anything that says I can't do that.我能想到的唯一另一件事是这个模块调用了另一个自定义模块,但我没有看到任何说我不能这样做的东西。

So, overall, what should I look for to resolve this error?所以,总的来说,我应该寻找什么来解决这个错误? I really cn't seem to find anything useful or that applies to this situation.我真的似乎找不到任何有用的东西或适用于这种情况的东西。

EDIT编辑

Ok, so I believe that I discovered the cause is due to the calling of a search function outside of an object/function being returned for the callback.好的,所以我相信我发现原因是由于在为回调返回的对象/函数之外调用了搜索 function。 Here's a simplified version of what's happening, since a lot of fields and values are managed:这是正在发生的事情的简化版本,因为许多字段和值都被管理:

/** 
 * custom.module.js
 * @NApiVersion 2.x
 * @NModuleScope Public 
 */


define(['N/search'],
/**
 * @param {search} search
 */
function(search) {

    var fields = new Array("a","b","c","d","e");
    var lValues = search.lookupFields({
            type : "customrecord_ng_cs_settings"
        ,   id : "1"
        ,   columns : fields
    });
    var _values = {
            a :     lValues.a
        ,   b :     lValues.b
        ,   c :     lValues.c
        ,   d :     lValues.d
        ,   e :     lValues.e
    };
    var _funcs = {
            func_a : function() {
                // do stuff
            }
        ,   func_b : function() {
                // do stuff
            }
        ,   func_c : function() {
                // do stuff
            }
    };

    return {
            value : _values
        ,   func : _funcs
    };

});

I need to maintain this kind of structure as not everything that gets returned in _values is actually a search/lookup result.我需要维护这种结构,因为并非在 _values 中返回的所有内容实际上都是搜索/查找结果。 Am I going to be forced to encapsulate the construction of this object within a function?我是否将被迫将 object 的构造封装在 function 中? And would that cause the lookup to occur every time a value is needed?这会导致每次需要值时都进行查找吗? This is a conversion from a 1.0 script, and this gets loaded and set only once at the beginning so the data is all there the entire time without having to be repeatedly fetched.这是从 1.0 脚本转换而来的,它在开始时只加载和设置一次,因此数据一直都在那里,无需重复获取。

So, I see the following options:所以,我看到以下选项:

  1. Convert the callback output to a function and doing something like将回调 output 转换为 function 并执行类似的操作
    the following at the start of every script:每个脚本开头的以下内容:
    var _values = customModule.values();
  1. Find some way to rework the code so that any script using the module can still access values in the following way:找到某种方法来重新编写代码,以便使用该模块的任何脚本仍然可以通过以下方式访问值:
    var _a = customModule.values.a;

I'd very much prefer option #2.我非常喜欢选项#2。 Is it possible?可能吗?

You cannot run any SuiteScript module code outside of an entry point.您不能在入口点之外运行任何 SuiteScript 模块代码。 You will need to encapsulate your data retrieval in a function, then invoke that function at the beginning of your entry point.您需要将数据检索封装在 function 中,然后在入口点的开头调用该 function。

If you want to avoid multiple fetches, you can leverage memoization in your function, or perhaps N/cache or N/session to store the data.如果你想避免多次获取,你可以在你的 function 中利用 memoization,或者N/cacheN/session来存储数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM