您希望它在查看或编辑记录时工作吗?它们的脚本略有不同。我假设您希望按钮在查看记录时工作,但我会编写它以便即使在编辑文档时它也能工作。
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 变得更加容易。