如何在nlapi搜索中用关联查询匹配多个子列表值
关于NetSuite nlapi多子列表值搜索的问题
NetSuite的nlapiSearchRecord支持通过join关联子列表或其他记录的值进行搜索。例如有如下结构的FOO记录:
FOO: { type: 'rectype', field1: '111', sublists: { bar: [ {value: 1}, {value: 2} ] } }
若已建立到bar子列表的关联,单值匹配的搜索可以正常返回结果:
nlapiSearchRecord('rectype', null, [ ['field', 'equalto', 'bar'], 'and', ['bar.value', 'equalto', '1'] ], [ new nlobjSearchColumn('anotherfield') ] );
但当尝试同时指定多个子列表值(比如要求bar.value同时等于1和2)时,直接叠加and条件的查询始终无结果:
nlapiSearchRecord('rectype', null, [ ['field', 'equalto', 'bar'], 'and', ['bar.value', 'equalto', '1'], 'and', ['bar.value', 'equalto', '2'] ], [ new nlobjSearchColumn('anotherfield') ] );
具体场景示例:BOM Revision搜索
实际需求是查找包含特定组件及对应BOM数量的bomrevision记录,尝试过多种过滤写法均无结果:
nlapiSearchRecord('bomrevision', null, [ ['isinactive', 'equalto', 'F'], 'and', ['component.item', 'is', '4942'], 'and', ['component.item', 'is', '4936'], // 以下为尝试过的过滤表达式 // [ // 'and', // ['component.bomquantity', 'equalto', '38'], // ], // 'and', // [ // ['component.item', 'anyof', '4936'], // 'and', // ['component.bomquantity', 'equalto', '38'], // ], ], [ new nlobjSearchColumn('name') ] );
解决方案
直接叠加同join字段的and条件,会要求同一条子列表记录同时满足所有条件,这显然不符合需求。正确的做法是将每个子列表条件封装为独立的过滤组,通过and连接这些组,让每个组独立匹配一条子列表记录:
通用场景修正写法
nlapiSearchRecord('rectype', null, [ ['field', 'equalto', 'bar'], 'and', // 匹配value=1的子列表条目 [['bar.value', 'equalto', '1']], 'and', // 匹配value=2的子列表条目 [['bar.value', 'equalto', '2']] ], [ new nlobjSearchColumn('anotherfield') ] );
BOM Revision场景修正写法
若需要同时匹配组件4942、以及组件4936且数量为38的BOM记录,过滤条件应写为:
nlapiSearchRecord('bomrevision', null, [ ['isinactive', 'equalto', 'F'], 'and', // 匹配包含组件4942的子列表记录 [['component.item', 'is', '4942']], 'and', // 匹配包含组件4936且数量为38的子列表记录 [ ['component.item', 'is', '4936'], 'and', ['component.bomquantity', 'equalto', '38'] ] ], [ new nlobjSearchColumn('name') ] );
原理说明
每个用[]包裹的过滤组会独立关联一次子列表,相当于要求父记录至少存在一条满足该组条件的子列表条目。多个组通过and连接,就实现了父记录同时拥有满足不同条件的子列表条目。
内容的提问来源于stack exchange,提问作者d.k
相关产品推荐
相关产品推荐

