是否有一种简单的方法可以使用NetSuite中的SuiteScript识别记录的所有相关记录?

时间:2017-09-13 21:09:24

标签: netsuite suitescript suitecloud

在自动化测试期间删除现有记录时,我经常遇到"此记录无法删除,因为它具有依赖记录"错误。现在,我在依赖记录页面上搜索每个依赖记录的记录类型和ID,并在继续之前用nlapiDeleteRecord删除它们。必须有更好的方法来识别和删除相关记录。

1 个答案:

答案 0 :(得分:0)

如果返回该错误的记录是“事务”,则可以使用几种不同的方式来识别它们。

您可以像下面这样浏览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);
    }
}