Marty Zigman

Conversations with Marty Zigman

Certified Administrator • ERP • SuiteCloud

Learn How To Attach and Detach NetSuite Custom Records via SuiteScript

NetSuite Technical



This article is relevant if you would like to attach or detach custom NetSuite child records from their parent records.

Background

The NetSuite platform allows you to create custom records. The header/line or parent/child structure is a common business pattern. From the child record perspective, in relational database nomenclature, the field reference to the parent record is called the foreign key. NetSuite provides a convenient option to describe this child record foreign key pattern on the parent field reference through the ‘Record is Parent’ checkbox.  NetSuite then automatically provides a user interface pattern to allow users to connect the parent/child records together.

We developed a simple record pattern to track our accelerator bundle provisioning efforts to our clients.  The built-in NetSuite user interface required too many clicks — it was time to make it easier. Thus, I wrote a simple SuiteLet-based provisioning application.  During the software development exercise, I found the documentation for attaching and detaching custom records lacked clarity. I searched the NetSuite Professionals forum to no avail. With a little trial and error, I found the JavaScript pattern.

Click the images to get a feel for the application.

SuiteScript 2.0 for Attaching and Detaching NetSuite Custom Records

The following SuiteScript 2.0 JavaScript code function will attach (link) and detach (unlink) NetSuite custom records.

function linkAppToClient(client, app, direction){

	//overload function to work in both attach and detach record operations
	//note attaching or detaching twice does not appear to produce an error; thus no error handling
	
	//object is overloaded to support both attach or detach operations
	operation = 
	{
		//target the child record that needs to be linked to the parent
		//'client' below is the child record ID
		record: {
			 type: 'customrecord_pri_application_accts',
			 id: client},
		
		//target the parent record; to and from is the same operation depending on attach/detach
		//'app' below is the ID of the parent
		to: {
			 type: 'customrecord_pri_application',
			 id: app},
		from: {
			 type: 'customrecord_pri_application',
			 id: app},
		
		//indicate the field that the child record uses to connect to the parent
		attributes: {field : 'custrecord_pri_application_dist_accts'}
		
		
	}
	log.debug({title:'linkAppToClient operation direction:' + direction, details: JSON.stringify(operation)})
	
	//general syntax is record.attach(operation) or record.detach(operation) from the n/record library
	//operation object needs either property to or from depending on direction
	//this function assumes the value for direction is either 'attach' or 'detach' with operation overloaded
	record[direction](operation);
};

Join a Recognized Team of NetSuite Experts

We are committed to holding high standards for innovation and care to our clients. Our team members work in configurations that produce challenge and earned accomplishments. We reward individuals for their contributions.

If you found this article relevant, feel free to sign up for notifications to new articles as I post them. If you desire to belong to a group of high caliber professionals and you are ready to step up, let’s have a conversation.

Marty Zigman LinkedIn

Marty Zigman

Holding three official certifications, Marty is widely recognized as a top NetSuite expert and leads a team of senior professionals at Prolecto Resources, Inc. A former Deloitte & Touche CPA and technology executive with CTO roles, he brings over 35 years of leadership in ERP, CRM, and eCommerce business systems. Contact Marty to engage directly.

Biography • YouTube • LinkedIn • X (Twitter)

Leave a Reply

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