使用事件按钮触发 clientscript 并打开 suitelet
Userevent button to trigger clientscript and open suitelet
我有一个使用用户事件脚本创建的按钮,它出现在交易记录上
这应该会在点击时触发一个客户端脚本,该脚本又应该重定向到一个 suitelet。
到目前为止,我没有收到错误消息,尽管它也没有执行任何操作。
我正在关注 SuiteAnswers id: 93513 的基本结构,因为我试图将用户所在记录的参数传递给 suitelet。
即,如果用户在发票(内部 id-38060)上单击 'add note' 按钮,我希望在 'Parent transaction' 字段下的 Suitelet 表单中设置该值。
我的脚本哪里出错了?
编辑:现在有两种不同的变体。第一个是仅使用用户事件脚本和套件(摆脱客户端脚本),另一个是经过调整的客户端脚本以包含 n/currentRecord 模块
用户事件脚本:
/**
*@NApiVersion 2.x
*@NScriptType UserEventScript
*/
define(["N/url", "N/record", "N/runtime", "N/ui/serverWidget"], function (
url,
record,
runtime,
serverWidget
) {
var exports = {}
/**
* @param {UserEventContext.beforeLoad} context
*/
function beforeLoad(context) {
if (
context.type == context.UserEventType.EDIT ||
context.type == context.UserEventType.VIEW
) {
var record = context.newRecord
var recordId = record.id
var recordType = record.type
var form = context.form
//the internal id of the client script file in the filing cabinet
form.clientScriptFileId = 4910
var suiteletURL = url.resolveScript({
scriptId: "customscript_suitelet_notes",
deploymentId: "customdeploy_suitelet_notes",
returnExternalUrl: true,
params: { recordId: recordId, recordType: recordType },
})
form.addButton({
id: "custpage_add_note",
label: "Add Note",
functionName:"onclick_callforSuitelet"
// "window.open('" +
// suiteletURL +
//"','PopUpWindow','height=500,width=1000,left=120,top=100,resizable=yes,scrollbars=yes,//toolbar=yes,menubar=no,location=no,directories=no, status=yes');",
})
}
}
return {beforeLoad:beforeLoad}
})
客户端脚本:
/**
*@NApiVersion 2.x
*@NScriptType ClientScript
*/
define(["N/record", "N/url",'N/currentRecord'], function (record, url,currentRecord) {
/**
* @param {ClientScriptContext.pageInit} context
* @param {ClientScriptContext.onclick_callforSuitelet} context
*/
function pageInit() {}
function onclick_callforSuitelet() {
var record = currentRecord.get();
var recordId = record.id;
var recordType = record.type;
log.debug("recId", recordId);
log.debug("recType", recordType);
//this is the script id on the script record for the suitelet (not the deployment)
var suiteletURL = url.resolveScript({
scriptId: "customscript_suitelet_notes",
//this is the id from the deployment record for the suitelet
deploymentId: "customdeploy_suitelet_notes",
params: { recordId: recordId, recordType: recordType },
});
document.location = suiteletURL;
log.debug("suiteletURL", suiteletURL);
}
return {
onclick_callforSuitelet: onclick_callforSuitelet,
pageInit: pageInit,
};
});
Suitelet 脚本
/**
*@NApiVersion 2.x
*@NScriptType Suitelet
*/
define(["N/ui/serverWidget", "N/log", "N/record", "N/url"], function (
serverWidget,
log,
record,
url
) {
/**
* @param {SuiteletContext.onRequest} context
*/
function onRequest(context) {
if (context.request.method === "GET") {
// Section One - Forms
var invoice_id = parseInt(context.request.parameters.recordId)
var form = serverWidget.createForm({
id: "notes",
title: "Notes",
})
var customerGroup = form.addFieldGroup({
id: "customerDetails",
label: "Customer Details",
})
customerGroup.isSingleColumn = false
form.addSubmitButton({
label: "Submit",
})
var select = form.addField({
id: "custpage_source",
type: serverWidget.FieldType.SELECT,
label: "Source of Communication",
container: "customerDetails",
})
//the value is the internal id of the option under customisation>lists,records and forms>lists>'source of communication' list
select.addSelectOption({
value: 1,
text: "Phone",
})
select.addSelectOption({
value: 2,
text: "Email",
})
select.addSelectOption({
value: 3,
text: "Website Contact Form",
})
select.addSelectOption({
value: 4,
text: "Other",
})
form.addPageLink({
type: serverWidget.FormPageLinkType.CROSSLINK,
title: "Invoice",
url:
"https://tstdrv2559160.app.netsuite.com/app/accounting/transactions/custinvc.nl?id=" +
invoice_id,
})
var parentTransaction = form.addField({
id: "custpage_parent_transaction",
type: serverWidget.FieldType.SELECT,
label: "Parent Transaction",
container: "customerDetails",
})
parentTransaction.addSelectOption({ value: invoice_id, text: invoice_id })
// form.updateDefaultValues({
// custpage_recordurl:
// "https://tstdrv2559160.app.netsuite.com/app/accounting/transactions/custinvc.nl?id=" +
// recId,
// });
context.response.writePage(form)
function getBaseUrl() {
return url.resolveRecord({
recordType: record.Type.INVOICE,
})
}
// Section Two - Tabs - See "Steps for Adding a Tab to a Form"
// Section Three - Sublist - See "Steps for Adding a Sublist to a Form"
} else {
// Section Four - Output - Used in all sections
var delimiter = /\u0001/
var sourceField = context.request.parameters.custpage_source
var parentField = context.request.parameters.custpage_parent_transaction
var invoiceRecord = record.load({
type: record.Type.INVOICE,
id: parentField,
})
log.debug("parent record", invoiceRecord)
// context.response.write(
// "You have entered:" + "<br/> Name: " + sourceField
// );
var recObj = record.create({
type: "customrecord_user_notes",
})
recObj.setValue({ fieldId: "custrecord_source", value: sourceField })
recObj.setValue({
fieldId: "custrecord_parent_transaction",
value: parentField,
})
var userNote = recObj.save({})
var notesFieldUpdate = record.submitFields({
type: record.Type.INVOICE,
id: parentField,
values: { custbody_notes_check: "CHECK NOTES" },
})
log.debug("notesfield", notesFieldUpdate)
context.response.write("Note Created")
}
}
return {
onRequest: onRequest,
}
})
目前,我还没有完成 suitelet 表单,虽然它工作正常,因为它正在使用用户输入的字段数据创建自定义记录的实例:
我没有定义记录上下文,这就是它什么都不做的原因。我只能在查看控制台日志并看到其中的错误消息后才能看到它
感谢 Sitaram 和 ZSwitaj 的建议
form.addButton({
id: "custpage_add_note",
label: "Add Note",
functionName: "onclick_callforSuitelet()",
});
在函数名称中使用 onclick_callforSuitelet
而不是 onclick_callforSuitelet()
。
据我所知,您无法像您一样在 user-defined 函数中访问客户端脚本中的 context
。 log.debug()
也往往不能很好地与客户端脚本一起工作,使用 console.log()
并打开浏览器控制台往往会导致更好的调试。
如果您将 log.debug()
函数更改为 console.log()
和 运行 脚本(也就是按下您的按钮),请执行 recordId
和 recordType
日志你期待结果吗?
根据 SuiteAnswer 93513 你应该可以使用 currentRecord.get()
来获取你当前的记录。可能无法解决您所有的问题,但希望可以让您重新振作起来!
我有一个使用用户事件脚本创建的按钮,它出现在交易记录上
到目前为止,我没有收到错误消息,尽管它也没有执行任何操作。 我正在关注 SuiteAnswers id: 93513 的基本结构,因为我试图将用户所在记录的参数传递给 suitelet。 即,如果用户在发票(内部 id-38060)上单击 'add note' 按钮,我希望在 'Parent transaction' 字段下的 Suitelet 表单中设置该值。 我的脚本哪里出错了? 编辑:现在有两种不同的变体。第一个是仅使用用户事件脚本和套件(摆脱客户端脚本),另一个是经过调整的客户端脚本以包含 n/currentRecord 模块 用户事件脚本:
/**
*@NApiVersion 2.x
*@NScriptType UserEventScript
*/
define(["N/url", "N/record", "N/runtime", "N/ui/serverWidget"], function (
url,
record,
runtime,
serverWidget
) {
var exports = {}
/**
* @param {UserEventContext.beforeLoad} context
*/
function beforeLoad(context) {
if (
context.type == context.UserEventType.EDIT ||
context.type == context.UserEventType.VIEW
) {
var record = context.newRecord
var recordId = record.id
var recordType = record.type
var form = context.form
//the internal id of the client script file in the filing cabinet
form.clientScriptFileId = 4910
var suiteletURL = url.resolveScript({
scriptId: "customscript_suitelet_notes",
deploymentId: "customdeploy_suitelet_notes",
returnExternalUrl: true,
params: { recordId: recordId, recordType: recordType },
})
form.addButton({
id: "custpage_add_note",
label: "Add Note",
functionName:"onclick_callforSuitelet"
// "window.open('" +
// suiteletURL +
//"','PopUpWindow','height=500,width=1000,left=120,top=100,resizable=yes,scrollbars=yes,//toolbar=yes,menubar=no,location=no,directories=no, status=yes');",
})
}
}
return {beforeLoad:beforeLoad}
})
客户端脚本:
/**
*@NApiVersion 2.x
*@NScriptType ClientScript
*/
define(["N/record", "N/url",'N/currentRecord'], function (record, url,currentRecord) {
/**
* @param {ClientScriptContext.pageInit} context
* @param {ClientScriptContext.onclick_callforSuitelet} context
*/
function pageInit() {}
function onclick_callforSuitelet() {
var record = currentRecord.get();
var recordId = record.id;
var recordType = record.type;
log.debug("recId", recordId);
log.debug("recType", recordType);
//this is the script id on the script record for the suitelet (not the deployment)
var suiteletURL = url.resolveScript({
scriptId: "customscript_suitelet_notes",
//this is the id from the deployment record for the suitelet
deploymentId: "customdeploy_suitelet_notes",
params: { recordId: recordId, recordType: recordType },
});
document.location = suiteletURL;
log.debug("suiteletURL", suiteletURL);
}
return {
onclick_callforSuitelet: onclick_callforSuitelet,
pageInit: pageInit,
};
});
Suitelet 脚本
/**
*@NApiVersion 2.x
*@NScriptType Suitelet
*/
define(["N/ui/serverWidget", "N/log", "N/record", "N/url"], function (
serverWidget,
log,
record,
url
) {
/**
* @param {SuiteletContext.onRequest} context
*/
function onRequest(context) {
if (context.request.method === "GET") {
// Section One - Forms
var invoice_id = parseInt(context.request.parameters.recordId)
var form = serverWidget.createForm({
id: "notes",
title: "Notes",
})
var customerGroup = form.addFieldGroup({
id: "customerDetails",
label: "Customer Details",
})
customerGroup.isSingleColumn = false
form.addSubmitButton({
label: "Submit",
})
var select = form.addField({
id: "custpage_source",
type: serverWidget.FieldType.SELECT,
label: "Source of Communication",
container: "customerDetails",
})
//the value is the internal id of the option under customisation>lists,records and forms>lists>'source of communication' list
select.addSelectOption({
value: 1,
text: "Phone",
})
select.addSelectOption({
value: 2,
text: "Email",
})
select.addSelectOption({
value: 3,
text: "Website Contact Form",
})
select.addSelectOption({
value: 4,
text: "Other",
})
form.addPageLink({
type: serverWidget.FormPageLinkType.CROSSLINK,
title: "Invoice",
url:
"https://tstdrv2559160.app.netsuite.com/app/accounting/transactions/custinvc.nl?id=" +
invoice_id,
})
var parentTransaction = form.addField({
id: "custpage_parent_transaction",
type: serverWidget.FieldType.SELECT,
label: "Parent Transaction",
container: "customerDetails",
})
parentTransaction.addSelectOption({ value: invoice_id, text: invoice_id })
// form.updateDefaultValues({
// custpage_recordurl:
// "https://tstdrv2559160.app.netsuite.com/app/accounting/transactions/custinvc.nl?id=" +
// recId,
// });
context.response.writePage(form)
function getBaseUrl() {
return url.resolveRecord({
recordType: record.Type.INVOICE,
})
}
// Section Two - Tabs - See "Steps for Adding a Tab to a Form"
// Section Three - Sublist - See "Steps for Adding a Sublist to a Form"
} else {
// Section Four - Output - Used in all sections
var delimiter = /\u0001/
var sourceField = context.request.parameters.custpage_source
var parentField = context.request.parameters.custpage_parent_transaction
var invoiceRecord = record.load({
type: record.Type.INVOICE,
id: parentField,
})
log.debug("parent record", invoiceRecord)
// context.response.write(
// "You have entered:" + "<br/> Name: " + sourceField
// );
var recObj = record.create({
type: "customrecord_user_notes",
})
recObj.setValue({ fieldId: "custrecord_source", value: sourceField })
recObj.setValue({
fieldId: "custrecord_parent_transaction",
value: parentField,
})
var userNote = recObj.save({})
var notesFieldUpdate = record.submitFields({
type: record.Type.INVOICE,
id: parentField,
values: { custbody_notes_check: "CHECK NOTES" },
})
log.debug("notesfield", notesFieldUpdate)
context.response.write("Note Created")
}
}
return {
onRequest: onRequest,
}
})
目前,我还没有完成 suitelet 表单,虽然它工作正常,因为它正在使用用户输入的字段数据创建自定义记录的实例:
我没有定义记录上下文,这就是它什么都不做的原因。我只能在查看控制台日志并看到其中的错误消息后才能看到它
感谢 Sitaram 和 ZSwitaj 的建议
form.addButton({
id: "custpage_add_note",
label: "Add Note",
functionName: "onclick_callforSuitelet()",
});
在函数名称中使用 onclick_callforSuitelet
而不是 onclick_callforSuitelet()
。
据我所知,您无法像您一样在 user-defined 函数中访问客户端脚本中的 context
。 log.debug()
也往往不能很好地与客户端脚本一起工作,使用 console.log()
并打开浏览器控制台往往会导致更好的调试。
如果您将 log.debug()
函数更改为 console.log()
和 运行 脚本(也就是按下您的按钮),请执行 recordId
和 recordType
日志你期待结果吗?
根据 SuiteAnswer 93513 你应该可以使用 currentRecord.get()
来获取你当前的记录。可能无法解决您所有的问题,但希望可以让您重新振作起来!