【同步锁/专题问答】如何在NS的脚本操作中锁定数据?
问题
你一般在NS里实现同步锁 怎么实现的呀?
现在遇到的问题是 map reduce 和workflow action 调用同一个方法 操作同一个单据 出现问题
Record has been changed
解答
写数据冲突这种事情,尽量让NS自己去处理。换句话说,Map/Reduce处理数据的时候,只要保证了出错了的记录能够在下一次能被继续执行,且Map/Reduce脚本不终止,那这个错误是无所谓的,(事实上写数据冲突也是时间偶然性的),所以:
1、保证M/R在执行错误时,程序不会被终止,比如用try catch来保障,当然这里你得捕获到正确的错误来进行处理;
2、保证被处理的记录针对该脚本逻辑的状态不发生变化,以让M/R下一次运行时能跑到该条记录;
案例
var searchResults = nlapiLoadSearch('customer', 'customsearch'); // Load the saved search
for (var x = 0; x < searchResults.length; x++) // Loop through each record
{
try
{
var record = nlapiLoadRecord('customer', searchResults[x].internalid ); // Try to load the locked record
} catch (error) {
if(error.code == 'RCRD_LOCKED_BY_WF') // Check what kind of error is thrown, in this case its locked record by a workflow
{
continue; // If true, then skipped this record and continue on the next record
}
}
}