Suitescript 2.0 添加按钮

Suitescript 2.0 addButton

我有一个带有子列表按钮的 suitelet,我正在尝试获取按钮以在自定义模块上执行功能。我无法让它工作。我收到错误 "Cannot call method "receive" of undefined" 有什么想法吗?

添加按钮的代码片段

define(['N/error', 'N/record', 'N/search', 'N/ui/serverWidget','./lib1'],


function(error, record, search, ui, lib1) {
//... some code here
searchSublist.addButton({
   id: 'custpage_recievepayment', 
   label: 'Receive Payment',
   functionName: "lib1.receive()"});
}

自定义模块的片段

define(['N/redirect'],
  function(redirect){
 function receive(){

      var deal = '497774';
      var url = redirect.toSuitelet({
       scriptId: 'customscript_deal_entry_2_0',
       deploymentId: 'customdeploy1',
       returnExternalUrl: false,
       params: {
        prevdeal: url
       }
      })
 }
 });

按钮点击处理程序在 2.0 中是一个问题(至少在我看来)。您收到此错误是因为 lib1 未在客户端定义。

我必须做的是创建一个客户端脚本,将我的点击处理程序导出到全局对象以使其可访问。类似于:

// Client script
define(['my/custom/module'], function (myModule) {
    window.receive = myModule.receive;
});

// Suitelet
define(...
    // some code...
    searchSublist.addButton({
        id: 'custpage_recievepayment', 
        label: 'Receive Payment',
        functionName: "receive"
    });
// other code...

导出到全局并不理想,通常也不是好的做法space,但我不知道在单击按钮时在模块中引用函数的任何其他方法。

我能够使用客户端脚本或将其导出到建议的全局对象。关键是将我的自定义模块修改为 return 我想在按钮上使用的功能,并使用 form.clientScriptFileId

调用自定义模块文件

//suitelet
define(['N/error', 'N/record', 'N/search', 'N/ui/serverWidget'],
function(error, record, search, ui) {
   
 // other code here
  form.clientScriptFileId = 78627;//this is the file cabinet internal id of my custom module
  var searchSublist = form.addSublist({
   id: 'custpage_deals',
   type: ui.SublistType.LIST,
   label: 'Deals'
  })
  searchSublist.addButton({
   id: 'custpage_receivepayment', 
   label: 'Receive Payment',
   functionName: "receive()"
    });
  
  //custom module
  
  define(['N/url','N/error'],

function(url, error) {
 return{
       receive: function(){
        
     //function code here
 }             

})

在多次尝试后无法正常工作后,我向 Netsuite 提交了一个缺陷(缺陷 390444),他们刚刚告诉我,这已经得到修复和测试,将在下一个主要版本中发布。

感谢您分享此信息。我终于通过使用 "file cabinet internal id" 而不是 "client script" 内部 ID 使其工作,正如该线程中有人提到的那样。

我还通过 chrom 开发工具注意到 "scriptContext" 没有像我预期的那样自动传递给客户端脚本函数。所以我不得不通过以下方式在客户端脚本中手动传递我需要的数据:

function beforeLoad(scriptContext) {
    var form = scriptContext.form;
    form.clientScriptFileId = 33595032;
    form.addButton({
        id: 'custpage_reprocessinvoice',
        label: 'Reprocess Invoice',
        functionName: 'reprocess(' + scriptContext.newRecord.id + ')'
    });
}

希望这可以帮助某人并节省 him/her 一点时间。

对于 suitescript 2.0,您需要定义实际包含您的函数的文件。

/**
 *@NApiVersion 2.x
 *@NScriptType UserEventScript
 */
define([],
  function() {
    function beforeLoad(context) {
      if(context.type == "view") {
        context.form.clientScriptFileId = 19181;
        context.form.addButton({
          id: 'custpage_dropshippo',
          label: 'Generate Dropship PO',
          functionName: 'generateDropshipPurchaseOrder'
        });
      }
    }
    return {
      beforeLoad: beforeLoad,
    };
  }
);

在该示例中,值19181是以下文件的文件柜ID(不需要部署但需要脚本记录):

/**
 *@NApiVersion 2.x
 *@NScriptType ClientScript
 */
define([],
  function() {
    function pageInit() {
    }
    function generateDropshipPurchaseOrder() {
      console.log("foo");
    }
    return {
      pageInit: pageInit,
      generateDropshipPurchaseOrder: generateDropshipPurchaseOrder
    };
});