虽然我之前对现有 SuiteScript 代码进行过一些维护工作,但我对 NetSuite 还很陌生。目前,我正在开发一个 NetSuite 用户事件脚本,该脚本使用 Handlebars.js 从客户记录数据生成 JSON 有效负载。但是,我不断遇到脚本记录“Handlebars is not Defined”的错误。尽管尝试了不同的方法来加载和初始化 Handlebars,问题仍然存在。
环境: NetSuite API 版本:2.0 脚本类型:用户事件脚本 脚本触发器:提交客户记录创建和编辑后 我尝试过的: 正在加载 Handlebars.js:
我将handlebars.min.js 文件上传到NetSuite 中的SuiteScripts/Libraries 文件夹中。 我使用 file.load() 函数加载库,然后尝试使用 Function() 执行它。
尝试初始化车把:
var handlebarsFile = file.load({ id: 'SuiteScripts/Libraries/handlebars.min.js' });
var handlebarsLib = handlebarsFile.getContents();
var Handlebars = Function(handlebarsLib + 'return Handlebars;')();
错误处理:
尽管如此,我仍然在脚本执行日志中看到错误“Handlebars is not Define”。
脚本详细信息: 这是我的脚本:
/**
* @NApiVersion 2.x
* @NScriptType UserEventScript
* @NModuleScope SameAccount
*/
define(['N/record', 'N/log', 'N/file', 'N/https'], function(record, log, file, https) {
function afterSubmit(context) {
log.debug('Script Triggered', 'After Submit script triggered for customer record.');
if (context.type !== context.UserEventType.CREATE && context.type !== context.UserEventType.EDIT) return;
try {
// Load the Handlebars.js library
var handlebarsFile = file.load({
id: 'SuiteScripts/Libraries/handlebars.min.js'
});
var handlebarsLib = handlebarsFile.getContents();
// Execute the Handlebars library code in the current scope
var Handlebars = Function(handlebarsLib + 'return Handlebars;')();
log.debug('Handlebars Loaded', 'Successfully loaded Handlebars.');
// Load the customer record
var customer = record.load({
type: record.Type.CUSTOMER,
id: context.newRecord.id
});
log.debug('Customer Record Loaded', 'Customer ID: ' + customer.id);
// Extract customer data
var data = {
email: customer.getValue('email') || '',
firstname: customer.getValue('firstname') || '',
lastname: customer.getValue('lastname') || '',
username: customer.getValue('email') || '',
phone: customer.getValue('phone') || '',
role: customer.getText('pricelevel') || '',
password: customer.getValue('custentity_pm_int_cust_pass') || '',
billing: {},
shipping: {}
};
// Extract address book data
var addressCount = customer.getLineCount('addressbook');
for (var i = 0; i < addressCount; i++) {
var isBilling = customer.getSublistValue({
sublistId: 'addressbook',
fieldId: 'defaultbilling',
line: i
});
var isShipping = customer.getSublistValue({
sublistId: 'addressbook',
fieldId: 'defaultshipping',
line: i
});
var address = {
company: customer.getValue('companyname') || '',
firstname: customer.getValue('firstname') || '',
lastname: customer.getValue('lastname') || '',
addr1: customer.getSublistValue({
sublistId: 'addressbook',
fieldId: 'addr1',
line: i
}) || '',
addr2: customer.getSublistValue({
sublistId: 'addressbook',
fieldId: 'addr2',
line: i
}) || '',
city: customer.getSublistValue({
sublistId: 'addressbook',
fieldId: 'city',
line: i
}) || '',
state: customer.getSublistValue({
sublistId: 'addressbook',
fieldId: 'state',
line: i
}) || '',
zip: customer.getSublistValue({
sublistId: 'addressbook',
fieldId: 'zip',
line: i
}) || '',
country: customer.getSublistValue({
sublistId: 'addressbook',
fieldId: 'country',
line: i
}) || '',
phone: customer.getValue('phone') || ''
};
if (isBilling) {
data.billing = address;
log.debug('Billing Address Found', JSON.stringify(address));
}
if (isShipping) {
data.shipping = address;
log.debug('Shipping Address Found', JSON.stringify(address));
}
}
// Handlebars template as a string
var templateSource =
'{' +
'"data":{' +
'"email": "{{record.email}}",' +
'"first_name": "{{record.firstname}}",' +
'"last_name": "{{record.lastname}}",' +
'{{#if record.pricelevel}}' +
'"role": [' +
'"{{wooCommerceRole record.pricelevel.name}}",' +
'{{#if record.custentity_pm_int_ws_credit_hold}} "credit_hold", {{/if}}' +
'{{#if_eq record.terms.internalid "4"}} "cod" {{else}} "terms" {{/if_eq}}' +
'],' +
'{{/if}}' +
'{{#if record.custentity_pm_int_update_ws_pass}}' +
'"password": "{{record.custentity_pm_int_cust_pass}}",' +
'{{/if}}' +
'"username": "{{record.email}}",' +
'"phone": "{{record.phone}}"' +
'{{#if (gt record.addressbook.length 0)}}' +
'{{#each record.addressbook}}' +
'{{#if defaultbilling}}' +
',"billing": {' +
'"company": "{{../record.companyname}}",' +
'"first_name": "{{../record.firstname}}",' +
'"last_name": "{{../record.lastname}}",' +
'"address_1": "{{addr1}}",' +
'"address_2": "{{addr2}}",' +
'"city": "{{city}}",' +
'"state": "{{state}}",' +
'"postcode": "{{zip}}",' +
'"country": "{{country}}",' +
'"phone": "{{../record.phone}}"' +
'}' +
'{{/if}}' +
'{{/each}}' +
'{{#each record.addressbook}}' +
'{{#if defaultshipping}}' +
',"shipping": {' +
'"company": "{{../record.companyname}}",' +
'"first_name": "{{../record.firstname}}",' +
'"last_name": "{{../record.lastname}}",' +
'"address_1": "{{addr1}}",' +
'"address_2": "{{addr2}}",' +
'"city": "{{city}}",' +
'"state": "{{state}}",' +
'"postcode": "{{zip}}",' +
'"country": "{{country}}",' +
'"phone": "{{../record.phone}}"' +
'}' +
'{{/if}}' +
'{{/each}}' +
'{{/if}}' +
'}' +
'}';
// Compile the Handlebars template with the data
var hbTemplate = Handlebars.compile(templateSource);
var jsonData = hbTemplate({
record: data
});
log.debug('Generated JSON', jsonData);
// Send the JSON to the middleware endpoint
var response = https.post({
url: 'https://middleware.exampleserver.io/customer-sync',
body: jsonData,
headers: {
'Content-Type': 'application/json'
}
});
log.debug('Response from API', response.body);
} catch (e) {
log.error('Error in afterSubmit', e.message);
}
}
return {
afterSubmit: afterSubmit
};
});
原始车把模板: 这是我正在尝试使用的原始车把模板:
{
"data": {
"email": "{{record.email}}",
"first_name": "{{record.firstname}}",
"last_name": "{{record.lastname}}",
{
{
#if record.pricelevel
}
}
"role": [
"{{wooCommerceRole record.pricelevel.name}}",
{
{
#if record.custentity_pm_int_ws_credit_hold
}
}
"credit_hold",
{
{
/if}} {
{
#if_eq record.terms.internalid "4"
}
}
"cod" {
{
else
}
}
"terms" {
{
/if_eq}}
], {
{
/if}} {
{
#if record.custentity_pm_int_update_ws_pass
}
}
"password": "{{record.custentity_pm_int_cust_pass}}", {
{
/if}}
"username": "{{record.email}}",
"phone": "{{record.phone}}" {
{
#ifgreaterthan record.addressbook.length 0
}
} {
{
#each record.addressbook
}
} {
{
#if defaultbilling
}
}, "billing": {
"company": "{{{../record.companyname}}}",
"first_name": "{{../record.firstname}}",
"last_name": "{{../record.lastname}}",
"address_1": "{{addressbookaddress.addr1}}",
"address_2": "{{addressbookaddress.addr2}}",
"city": "{{addressbookaddress.city}}",
"state": "{{addressbookaddress.state}}",
"postcode": " {{addressbookaddress.zip}}",
"country": "{{addressbookaddress.country.internalid}}",
"phone": " {{{../record.phone}}}"
}
{
{
/if}} {
{
/each}}
{
{
#each record.addressbook
}
} {
{
#if defaultshipping
}
}, "shipping": {
"first_name": "{{../record.firstname}}",
"last_name": "{{../record.lastname}}",
"company": "{{{../record.companyname}}}",
"address_1": "{{addressbookaddress.addr1}}",
"address_2": "{{addressbookaddress.addr2}}",
"city": "{{addressbookaddress.city}}",
"state": "{{addressbookaddress.state}}",
"postcode": " {{addressbookaddress.zip}}",
"country": "{{addressbookaddress.country.internalid}}",
"phone": " {{{../record.phone}}}"
} {
{
/if}} {
{
/each}} {
{
/ifgreaterthan}}
}
}
我需要什么帮助:
加载 Handlebars.js 的正确方法:是否有更可靠的方法在 NetSuite SuiteScript 环境中加载和初始化 Handlebars?
替代方案:如果 Handlebars 在 SuiteScript 中不可行,您建议使用哪些替代方案来从模板动态生成 JSON?
也许我的处理方式是错误的,所以我非常感谢您提供的任何指导或建议。
您需要定义您的车把库...'N/https','./Libraries/handlbars.min']...然后在函数...https, Handlebars) {.. .