【问题标题】:NetSuite/Suitescript/Workflow: How do I open a URL from a field after clicking button?NetSuite/Suitescript/Workflow:单击按钮后如何从字段中打开 URL?
【发布时间】:2017-11-27 13:34:58
【问题描述】:

我有一个工作流程,它添加了一个“打开链接”按钮和一个名为“URL”的记录字段,其中包含指向 NetSuite 中附件的超链接。我想添加一个在不同页面中打开此 url 的工作流操作脚本。我已将脚本和工作流操作添加到工作流中。我的脚本:

function openURL() {  

var url = nlapiGetFieldValue('custbody_url');

window.open(url);    

} 

单击按钮后出现此脚本错误:“TypeError:找不到在对象 [object Object] 中打开的函数。

如何更改我的脚本以便在字段中打开 URL?

(这个功能我在控制台试一下就可以了)

谢谢!

【问题讨论】:

    标签: url hyperlink netsuite suitescript


    【解决方案1】:

    您希望它在查看或编辑记录时工作吗?它们的脚本略有不同。我假设您希望按钮在查看记录时工作,但我会编写它以便即使在编辑文档时它也能工作。

    Netsuite 设置方式的难点在于它需要两个脚本,一个用户事件脚本和一个客户端脚本。 @michoel 建议的方式也可能有效……不过,我之前从未亲自插入脚本。 也许我今天某个时候会试试看。

    这是一个你可以使用的用户事件(虽然我自己还没有测试过,所以你应该在部署给每个人之前通过测试运行它)。

    function userEvent_beforeLoad(type, form, request)
    {
        /*
        Add the specified client script to the document that is being shown
        It looks it up by id, so you'll want to make sure the id is correct
        */
        form.setScript("customscript_my_client_script");
    
        /*
        Add a button to the page which calls the openURL() method from a client script
        */
        form.addButton("custpage_open_url", "Open URL", "openURL()");    
    }
    

    将其用作用户事件脚本的 Suitescript 文件。将脚本页面中的 Before Load 函数设置为 userEvent_beforeLoad。确保将其部署到您希望它运行的记录。

    这是与之配套的客户端脚本。

    function openURL()
    {
        /*
        nlapiGetFieldValue() gets the url client side in a changeable field, which nlapiLookupField (which looks it up server side) can't do
        if your url is hidden/unchanging or you only care about view mode, you can just get rid of the below and use nlapiLookupField() instead
        */
        var url = nlapiGetFieldValue('custbody_url');
    
        /*
        nlapiGetFieldValue() doesn't work in view mode (it returns null), so we need to use nlapiLookupField() instead
        if you only care about edit mode, you don't need to use nlapiLookupField so you can ignore this
        */
        if(url == null)
        {
            var myType = nlapiGetRecordType();
            var myId = nlapiGetRecordId();
            url = nlapiLookupField(myType, myId,'custbody_url');
        }
    
        //opening up the url
        window.open(url);    
    }
    

    将其添加为客户端脚本,但不要进行任何部署(用户事件脚本会为您将其附加到表单中)。确保此脚本具有 customscript_my_client_script 的 ID(或您在 form.setScript() 中的用户事件脚本中使用的任何脚本 ID),否则这将不起作用。

    要记住的另一件事是,每条记录只能使用 form.setScript() 附加一个脚本(我认为?),因此您可能希望为用户事件脚本和客户端脚本命名与表单相关的内容你正在部署它。使用 form.setScript 相当于在自定义表单菜单中设置脚本值。

    如果您可以让@michoel 的答案正常工作,那最终可能会更好,因为您将逻辑全部保存在一个脚本中(从我的角度来看)这使得管理您的 Suitescripts 变得更加容易。

    【讨论】:

      【解决方案2】:

      您遇到的问题是工作流操作脚本在服务器端执行,因此您无法执行客户端操作,例如打开新选项卡。我建议使用可以将客户端代码“注入”到按钮 onclick 函数中的用户事件脚本。

      function beforeLoad(type, form) {
        var script = "window.open(nlapiGetFieldValue('custbody_url'))";
        form.addButton('custpage_custom_button', 'Open URL', script);
      }
      

      【讨论】:

      • 这为我打开了一个空白页面。即使我尝试添加:var rec = nlapiLoadRecord('vendorbill', nlapiGetRecordId()); var url = rec.getFieldValue('custbody_url'); 并尝试打开此页面,它也不执行任何操作并停留在页面上
      猜你喜欢
      • 2022-10-02
      • 1970-01-01
      • 2015-12-18
      • 2014-07-28
      • 2018-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-18
      相关资源
      最近更新 更多