how to get all child records of a customer using suitescript

If the user wants to get all the child records of a Customer via script then we can use this method.

Using SuiteScript 1.0:

function getChildren() {‌

    var filters = new Array();

    // 352 is the internal id of the parent customer
    filters[0] = new nlobjSearchFilter('internalid', 'parentcustomer', 'anyof', '352');

    var columns = new Array();

    columns[0] = new nlobjSearchColumn('internalid');

    var results = nlapiSearchRecord('customer', null, filters, columns);

    //results would be an array of the internal ids of the child customers
    for (var i = 0; i < results.length; i++) {‌
        nlapiLogExecution('DEBUG', 'Child #' + i, results[i].getValue('internalid'));
    }

}
Using SuiteScript 2.0:
define(['N/search','N/currentRecord','N/log'],function(search,currentRecord,log){‌
    var filters = new Array();

    // 352 is the internal id of the parent customer
    filters[0] = search.createFilter({‌
    name: 'internalid',
	join: 'parentcustomer',
    operator: search.Operator.IS,
    values: 352
});

    var columns = new Array();

    columns[0] = search.createColumn({‌
    name: 'internalid'
});;

    var mySearch = search.create({‌
		type:search.Type.CUSTOMER,
		filters:filters,
		columns:columns
})

var results = mySearch.run();

    //results would be an array of the internal ids of the child customers
results.each(function(row){‌


	var internalID = row.getValue({‌name:'internalid'})
        log.debug({‌
        title: 'Debug Entry',
        details: 'Child Record ID: ' + internalID
        });

	return true
})
})

Leave a comment

Your email address will not be published. Required fields are marked *