NetSuite/Suitescript/Workflow:单击按钮后如何从字段打开URL?

NetSuite/Suitescript/Workflow:单击按钮后如何从字段打开URL?,url,hyperlink,netsuite,suitescript,Url,Hyperlink,Netsuite,Suitescript,我有一个工作流,它在记录上添加一个按钮打开链接和一个名为URL的字段,该字段包含指向NetSuite中附件的超链接。我想添加一个工作流操作脚本,用于在其他页面中打开此url。我已将脚本和工作流操作添加到工作流中。我的剧本: function openURL() { var url = nlapiGetFieldValue('custbody_url'); window.open(url); } 单击按钮后,我出现此脚本错误:TypeError:找不到对象[object]中打

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

function openURL() {  

var url = nlapiGetFieldValue('custbody_url');

window.open(url);    

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

如何更改脚本,使其在字段中打开URL

当我在控制台中尝试此功能时,它会工作


谢谢

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

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

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

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

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

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_Before Load。确保将其部署到要在其上运行的记录

下面是与之配套的客户端脚本

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);    
}
将其添加为客户端脚本,但不要进行任何部署。用户事件脚本将为您将其附加到表单。确保此脚本的id为customscript_my_client_script或您在form.setScript中的用户事件脚本中使用的任何脚本id,否则这将无法工作

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


如果你能让@michoel的答案起作用,那可能会更好,因为你将逻辑全部保存在一个脚本中,在我看来,这样可以更容易地管理你的Suitescripts。

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

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_Before Load。确保将其部署到要在其上运行的记录

下面是与之配套的客户端脚本

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);    
}
将其添加为客户端脚本,但不要进行任何部署。用户事件脚本将为您将其附加到表单。确保此脚本的id为customscript_my_client_script或您在form.setScript中的用户事件脚本中使用的任何脚本id,否则这将无法工作

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


如果你能让@michoel的答案起作用,结果可能会更好,因为你将逻辑全部保存在一个脚本中,在我看来,这样可以更轻松地管理你的Suitescripts。

这为我打开了一个空白页。即使我尝试添加:var rec=nlapioadrecord'vendorbill',nlapiGetRecordId;var url=rec.getFieldValue'custbody_url';并尝试打开此页面,它不会执行任何操作
停留在页面上这为我打开了一个空白页面。即使我尝试添加:var rec=nlapioadrecord'vendorbill',nlapiGetRecordId;var url=rec.getFieldValue'custbody_url';并尝试打开此页面,它不做任何操作,并停留在页面上