从父记录子列表创建的子记录无法触发User Event脚本的替代方案咨询
替代方案:从父单子列表创建子记录时修改字段值(绕过锁定Bundle的User Event限制)
以下是针对该场景的可行替代方案,可根据业务需求选择适配方式:
1. 父记录User Event脚本(实时修改)
利用父记录的afterSubmit事件,在子记录随父单保存创建完成后,直接加载子记录并修改目标字段。适合子记录自动随父记录创建的场景,能实现实时更新。
示例代码(以销售订单创建发票为例):
function afterSubmit(context) { const allowedTypes = [context.UserEventType.CREATE, context.UserEventType.EDIT]; if (!allowedTypes.includes(context.type)) return; const parentRecord = context.newRecord; const sublistId = 'item'; // 替换为实际子列表ID const lineCount = parentRecord.getLineCount({ sublistId }); const childRecordType = record.Type.INVOICE; // 替换为子记录类型 for (let i = 0; i < lineCount; i++) { // 获取已创建的子记录ID(替换为实际关联字段ID) const childId = parentRecord.getSublistValue({ sublistId, fieldId: 'createdfrom', line: i }); if (!childId) continue; // 加载子记录并修改字段 const childRecord = record.load({ type: childRecordType, id: childId, isDynamic: true }); // 替换为目标字段ID和对应值 childRecord.setValue({ fieldId: 'custbody_custom_field', value: '目标修改值' }); // 可选:添加标记字段避免重复执行修改逻辑 const hasBeenUpdated = childRecord.getValue({ fieldId: 'custbody_updated_flag' }); if (!hasBeenUpdated) { childRecord.setValue({ fieldId: 'custbody_updated_flag', value: true }); childRecord.save({ enableSourcing: false, ignoreMandatoryFields: true }); } } }
2. 子记录定时脚本(批量延迟处理)
创建定时脚本定期扫描近期创建且未修改目标字段的子记录,批量更新字段。适合不需要实时修改、子记录批量生成的场景。
示例代码:
function scheduledScriptExecution(context) { const childRecordType = record.Type.INVOICE; // 替换为子记录类型 const search = search.create({ type: childRecordType, filters: [ ['createddate', 'within', 'last', '2hours'], // 根据业务调整时间范围 'AND', ['custbody_custom_field', 'isempty', ''], // 过滤未修改的记录 'AND', ['createdfrom', 'noneof', '@NONE@'] // 过滤从父单创建的记录 ], columns: ['internalid'] }); const results = search.run().getRange({ start: 0, end: 1000 }); for (const result of results) { const childId = result.getValue('internalid'); const childRecord = record.load({ type: childRecordType, id: childId }); childRecord.setValue({ fieldId: 'custbody_custom_field', value: '目标修改值' }); childRecord.save(); } }
3. 子记录Client Script(前端加载时修改)
如果子记录创建后会被用户手动打开,可在子记录的pageInit事件中判断是否为从父单创建的新记录,实时修改目标字段。仅适用于用户需要打开子记录的场景。
示例代码:
function pageInit(context) { const currentRecord = context.currentRecord; const isNew = currentRecord.isNew; const createdFrom = currentRecord.getValue({ fieldId: 'createdfrom' }); const targetFieldValue = currentRecord.getValue({ fieldId: 'custbody_custom_field' }); // 仅处理从父单创建、且目标字段未设置的新记录 if (isNew && createdFrom && !targetFieldValue) { currentRecord.setValue({ fieldId: 'custbody_custom_field', value: '目标修改值' }); } }
4. 父记录Client Script(拦截创建动作)
针对用户手动在父单子列表点击创建子记录的场景,拦截原生创建动作,改用record.create方法手动创建子记录,同时直接设置目标字段(如需触发子记录User Event,此方式也可实现)。
示例代码:
function pageInit(context) { // 等待页面加载完成后绑定按钮事件 setTimeout(() => { // 替换为实际创建子记录的按钮ID(需通过浏览器开发者工具获取) const createBtn = document.getElementById('btn_create_child'); if (!createBtn) return; createBtn.addEventListener('click', (e) => { e.preventDefault(); const currentRecord = context.currentRecord; const sublistId = 'item'; // 替换为实际子列表ID const currentLine = currentRecord.getCurrentSublistIndex({ sublistId }); // 获取子列表必要字段值 const itemId = currentRecord.getSublistValue({ sublistId, fieldId: 'item', line: currentLine }); // 手动创建子记录 const childRecord = record.create({ type: record.Type.INVOICE }); childRecord.setValue({ fieldId: 'createdfrom', value: currentRecord.id }); childRecord.setValue({ fieldId: 'item', value: itemId }); // 设置目标字段 childRecord.setValue({ fieldId: 'custbody_custom_field', value: '目标修改值' }); const childId = childRecord.save(); // 跳转到新创建的子记录页面 window.location.href = `/app/common/transaction/transaction.nl?id=${childId}`; }); }, 1000); }
内容的提问来源于stack exchange,提问作者Anu Ashish
相关产品推荐
相关产品推荐

