Learn how to delete child records when they are removed

Introduction

In NetSuite you can relate records to each other using the “Record is Parent” checkbox on custom fields.  When you check this box, it creates a Parent/Child relationship.  If the child record is a custom record, you can configure the record to show a “Remove” link in view mode when the child record is shown on the Parent.  Clicking the “Remove” link will remove the Parent/Child relationship.  The problem here is that the Child record is not deleted when you click “Remove”.   It becomes an orphaned record in the system.  This article will go into more detail about how this works and offer a user event script to delete the orphaned record.

Step 1: Create Parent Record

Go to Customization->List, Records, & Fields->Record Types->New.  Create the record to match the picture.

 

 

Step 2: Create Child Record

Go to Customization->List, Records, & Fields->Record Types->New.  Create the record to match the picture below.  You will notice there are three check boxes next to one another: “Show Remove Link”, “Allow Child Editing”, and “Allow Delete”.  You must check “Show Remove Link” first to be able to check “Allow Child Record Editing” and once “Allow Child Record Editing” is checked, you are able to check “Allow Delete”.  Clicking “Show Remove Link” will add a link on the Child record called “Remove”. Clicking “Remove” will detach (not delete) the record from the Parent.    “Allow Child Record Editing” will allow the user to add and edit child records on the parent record.  “Allow Delete” will allow the user to delete child records when the parent record is in edit mode and detach (not delete) child records when the parent record is in view mode.  You can click the field labels to get more detailed information.  Click the first two check boxes (“Show Remove Link” and “Allow Child Record Editing”) to show the remove link and to be able to easily add child records from the parent.

 

 

You should create one custom field for this record “Link to Parent”.  Make sure to check the box “Record is Parent”. When you do this you will be able to add and edit Child records by going to the Custom tab on the Parent record.

 

 

Step 3: Add Parent and Child records

Add a parent record with a child record and Save.  After saving, you will see the “Remove” link.

 

 

 

The problem with the “Remove” link is that it only detaches the record from the parent.  A user could easily think that the “Remove” link is deleting the record, but it is not.  Over time clicking this link will lead to many orphaned records in the system.  There are probably some use cases where it makes sense to keep an orphaned record but I think those are few and far between.  Let’s write a user event script to delete the child record when the “Remove” link is clicked.

Step 4: Create User Event Script to Delete Child record


 

/**
*@NApiVersion 2.1
*@NScriptType UserEventScript
*/

define(['N/record'],
    (record) => {
        afterSubmit = (context) => {
            let newRecord = context.newRecord;
            let parent = newRecord.getValue('custrecord_link_to_parent');
            // if parent is empty, delete the record
            if (!parent) {
                let recordId = record.delete({
                    type: 'customrecord_child_record',
                    id: newRecord.id
                });
                log.debug('Record deleted', recordId);
            }
        }

        return {
            afterSubmit: afterSubmit
        };
    });

Add and deploy the script. You should deploy the script on the Child record.  Reference my article “Quick Guide to Adding and Deploying a Script in NetSuite” if needed.

Although there is no way to know the user clicked the “Remove” link in script, if the “Link To Parent” is empty you know that the record is orphaned from its parent.

Conclusion

This simple script will delete child records when they are orphaned from clicking the “Remove” link.  Keeping the system clean by removing orphaned records is a good practice.

If you need help with customizing NetSuite, please contact Suite Tooth Consulting here to set up a free consultation.

If you liked this article, please sign up for my newsletter to get these delivered to your inbox here.

Follow on
Jaime Requena Chief Executive Officer

Jaime Requena is a seasoned NetSuite Consultant and Solutions Architect, known for delivering WHITE GLOVE service to businesses. With 15+ years of experience and 3x certifications in ERP, Developer, and Admin, Jaime specializes in highly customized NetSuite accounts, transforming operations for 200+ satisfied customers all across the globe.

Get Connected

How can we help?


    Stay in the loop with Suite Tooth Consulting!

    We aim to bring unmatched expertise and professionalism to your NetSuite initiatives. Let’s talk about how our NetSuite consultancy can make a difference!

    Global Client Satisfaction

    Client Testimonials

    It’s Been 4+ Years Now And We Have Worked With Hundreds Of Clients, Building Our Way To The Top, One Happy Client After Another! Their Voices Of Satisfaction Serve As A Testament To Our Success –