Netsuite Suitescript 2.0针对不同类型的项目使用不同的形式
我的公司运行几种不同类型的项目,并希望根据所选项目记录中运行的项目类型,以不同的方式查看项目记录 我有一个选择要使用的表单的字段,标题是“自定义表单”(这是一个选择字段),我们的工作人员输入项目类型的字段“CustomEntityJT_fie_pro_projecttype”(也是一个选择字段) 我创建了以下加载前用户事件脚本以尝试实现这一点:Netsuite Suitescript 2.0针对不同类型的项目使用不同的形式,netsuite,Netsuite,我的公司运行几种不同类型的项目,并希望根据所选项目记录中运行的项目类型,以不同的方式查看项目记录 我有一个选择要使用的表单的字段,标题是“自定义表单”(这是一个选择字段),我们的工作人员输入项目类型的字段“CustomEntityJT_fie_pro_projecttype”(也是一个选择字段) 我创建了以下加载前用户事件脚本以尝试实现这一点: /** * @NApiVersion 2.x * @NScriptType UserEventScript * @NModuleScope SameAc
/**
* @NApiVersion 2.x
* @NScriptType UserEventScript
* @NModuleScope SameAccount
*/
define(["N/record"], function(r) {
function beforeLoad(context) {
var currentRecord = context.newRecord;
var projectType = currentRecord.getValue({
fieldId: "custentityjt_fie_pro_projecttype",
});
currentRecord.setValue({
fieldID: 'customform',
value: projectType
})
}
return {
beforeLoad: beforeLoad,
}
})
在编辑模式下加载项目记录时,自定义表单选择不会更改,在视图模式下加载项目记录时,我得到以下结果:
{"type":"error.SuiteScriptError","name":"UNEXPECTED_ERROR","message":null,"stack":["anonymous(N/recordService)","beforeLoad(/SuiteScripts/setForm.js:13)"],"cause":{"type":"internal error","code":"UNEXPECTED_ERROR","details":null,"userEvent":"beforeload","stackTrace":["anonymous(N/recordService)","beforeLoad(/SuiteScripts/setForm.js:13)"],"notifyOff":false},"id":"","notifyOff":false}
一般来说,我对Netsuite和编程非常陌生,所以请保持温和:)您需要使用客户端脚本来更改自定义表单。最好的选择是在两个位置执行,一个是pageInit(),另一个是fieldChanged()。另一个潜在问题是,试图将自定义表单值设置为在字段cusentityjt_fie_project_projecttype的getValue中检索到的值。示例中currentRecord.getValue()返回的值将是在此设置的项目类型的自定义列表值的内部id(返回自定义列表,您将看到列出的内部id值)。这是一个问题,因为在设置自定义表单字段的值时,需要引用要使用的自定义表单的内部Id。如果引用的项目类型的内部ID与自定义表单内部ID匹配,这将是非常值得注意的。我的建议是在代码中创建一个哈希表来存储引用(假设您的项目类型列表不经常更改)。尝试一下(只需确保更新lookup变量中的值即可
/**
*@NApiVersion 2.x
*@NScriptType ClientScript
*/
define([
'N/record'
],
function (
nsRecord
) {
//
// lookup table where the object property represents the internal IDs of the
// custom list values, and the value of each property represents the
// internal id's of the Custom Forms you wish to associate with each list value.
//
var lookup = {
1: 122,
2: 123,
3: 125,
4: 136
};
function fieldChanged(context) {
var field = context.fieldId;
var rec = context.currentRecord;
var projId;
if (field === 'custentityjt_fie_pro_projecttype' && rec.getValue('custentityjt_fie_pro_projecttype')) {
projId = rec.getValue('custentityjt_fie_pro_projecttype');
if (lookup[projId]) {
rec.setValue({
fieldId: 'customform',
value: lookup[projId],
ignoreFieldChange: true,
fireSlavingSync: true
});
}
}
}
function pageInit(context) {
var rec = context.currentRecord;
var mode = context.mode;
var projId;
var formId;
if (mode !== 'create') {
formId = rec.getValue('customform');
projId = rec.getValue('custentityjt_fie_pro_projecttype');
if (lookup[projId] && lookup[projId] !== formId) {
rec.setValue({
fieldId: 'customform',
value: lookup[projId],
ignoreFieldChange: true,
fireSlavingSync: true
});
}
}
}
return {
fieldChanged: fieldChanged,
pageInit: pageInit
};
});
非常感谢您的帮助。我说的这只在编辑模式下工作对吗?在查看模式下查看项目时,有没有办法让它工作?我还应该提到,在编辑模式下,自定义表单选择会正确更改,但实际表单本身不会更改。看起来您通常会单击所选内容,而不是仅仅更改字段的值,以使其实际执行更改。是否可以通过在更改后添加页面重新加载来解决此问题?@jtux您是正确的,这将仅在编辑模式下运行。我还相信,它将仅作为客户端脚本工作,而不是在加载前用户事件脚本上工作。