如何通过SuiteScript从客户记录创建供应商并关联至Other Relationship字段?
NetSuite AfterSubmit脚本实现客户同步创建同名供应商并关联关系字段
核心逻辑
在客户记录创建完成的AfterSubmit事件里,读取客户名称生成同名供应商,再将新供应商关联到客户的「Other Relationship」字段。
具体实现代码(SuiteScript 2.x)
/** * @NApiVersion 2.x * @NScriptType UserEventScript */ define(['N/record', 'N/log'], function(record, log) { function afterSubmit(context) { // 仅在创建客户时执行,避免修改客户时重复生成供应商 if (context.type !== context.UserEventType.CREATE) { return; } var customerRecord = context.newRecord; var customerName = customerRecord.getValue({ fieldId: 'companyname' }); var customerId = customerRecord.id; if (!customerName) { log.error('错误', '客户名称为空,无法创建供应商'); return; } try { // 1. 创建同名供应商记录 var vendorRecord = record.create({ type: record.Type.VENDOR }); vendorRecord.setValue({ fieldId: 'companyname', value: customerName }); // 按需补充供应商必填字段,比如电话、地址等 // vendorRecord.setValue({ fieldId: 'phone', value: customerRecord.getValue({ fieldId: 'phone' }) }); var vendorId = vendorRecord.save({ enableSourcing: true, ignoreMandatoryFields: false }); log.debug('供应商创建成功', '供应商ID:' + vendorId); // 2. 关联供应商到客户的「Other Relationship」字段 var customerToUpdate = record.load({ type: record.Type.CUSTOMER, id: customerId }); // 替换为你系统中「Other Relationship」字段的实际ID(标准/自定义字段ID) customerToUpdate.setValue({ fieldId: 'custentity_other_relationship', value: vendorId }); customerToUpdate.save({ enableSourcing: true, ignoreMandatoryFields: false }); log.debug('客户关联成功', '客户ID:' + customerId + '已关联供应商ID:' + vendorId); } catch (e) { log.error('执行失败', e.message + '\n' + e.stack); } } return { afterSubmit: afterSubmit }; });
关键注意点
- 字段ID替换:代码里的
custentity_other_relationship要换成你系统中「Other Relationship」字段的真实ID,可通过NetSuite自定义字段列表或浏览器开发者工具查看。 - 权限配置:脚本必须拥有「创建供应商」和「编辑客户」的权限,否则会触发权限报错。
- 必填字段处理:如果供应商有其他必填项(如税号、地址),需要在创建时补充对应值,否则保存供应商会失败。
- 重复执行规避:通过
context.type限制仅在创建客户时执行,防止修改客户记录时重复生成供应商。
内容的提问来源于stack exchange,提问作者4N335
相关产品推荐
相关产品推荐

