在自动测试过程中删除现有记录时,我经常遇到 "由于该记录有从属记录,所以不能删除 "的错误。现在,我在依赖记录页面上搜索每个依赖记录的记录类型和ID,并在继续之前用nlapiDeleteRecord删除它们。一定有更好的方法来识别和删除依赖记录。
如果返回该错误的记录是一个事务,你可以使用一些不同的方法来识别它们。
你可以通过NetSuite记录对象上的 "链接 "行项来查找,像这样。(注意,它确实返回一些奇怪的 record type
字符串("Bill "应该是 "vendorbill",等等)。
var recordtype = 'purchaseorder'; // Any valid transaction record type
var recordid = 2709498; // Any valid Transaction Internal ID that has links
var recordlinks = getRecordLinks(recordtype,recordid);
/**
* Returns the an array containing the Record Type and Internal Id of each record linked to the input record
* @param {Object} rec - A NetSuite record object, or record type internal id (salesorder, purchaseorder, etc)
* @param {String} recordid - The Internal ID of the record to load (if a nlobjRecord was not provided)
* @return {Array} - Returns an array of links, or null
*/
function getRecordLinks(rec,recordid) {
if(!(typeof rec == 'object')) rec = nlapiLoadRecord(rec,recordid);
if(rec.getLineItemCount('links') == 0) return null;
var links = [];
for(var i=1,total=rec.getLineItemCount('links');i<=total;i++) {
links.push({type:rec.getLineItemValue('links','type',i),id:rec.getLineItemValue('links','id',i)});
}
return links;
}
或者,你可以直接执行搜索来找到它们。可能更快,并且返回正确的 record type
每一个环节。
var recordid = 2709498; // Any valid Transaction Internal ID that has links
var links = nlapiSearchRecord('transaction',null,[['mainline','IS','T'],'AND',['createdfrom','IS',recordid]]);
if(links != null) {
for(var i=0,total=links.length;i<total;i++) {
var link_recordtype = links[i].getRecordType();
var link_recordid = links[i].getId();
console.log('Link type:'+link_recordtype+' ID:'+link_recordid);
// nlapiDeleteRecord(link_recordtype,link_recordid);
}
}