jrbeagle
jrbeagle

Reputation: 21

Is there a simple way to identify all dependent records of a record using SuiteScript in NetSuite?

In deleting existing records during automated tests, I often encounter the "This record cannot be deleted because it has dependent records" error. Right now I scrape the dependent records page for the record types and ID's of each dependent record and delete them with nlapiDeleteRecord before proceeding. There must be a better way to identify and delete dependent records.

Upvotes: 2

Views: 3234

Answers (1)

Kane Shaw
Kane Shaw

Reputation: 374

If the record that is returning that error is a Transaction you could use a few different ways to identify them.

You could look through the "Links" line items on the NetSuite record object like so: (note that it does return some weird record type strings ("Bill" when it should be "vendorbill", etc):

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;
}

Or, you could just execute a search to find them. Probably faster, and returns the correct record type for each link:

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);
    }
}

Upvotes: 1

Related Questions