The saveRecord client script function is beneficial when you want to prevent a user from saving a record if certain conditions are true. For instance, if you want to prevent a Purchase Order from being saved if the quantity of a particular item is under a certain amount, you could give a message and stop the record from being saved until the quantity is changed. For our walk-through sample, we will keep working with the Customer record. Here is where we left off with the Customer record client script.

define(['N/record','N/search','N/ui/dialog', 'N/log'], function (record, search, 
dialog, log) {
    /**
    *@NApiVersion 2.0
    *@NScriptType ClientScript
    */
    
    function pageInit(context) {
        var currentRecord = context.currentRecord;
        
        var firstName = currentRecord.getValue({
            fieldId: 'firstname'
        });
        log.debug({
            title: 'Customer First Name', 
            details: firstName
        });
        
        dialog.alert({
            title: 'Announcement',
            message: 'You are viewing ' + firstName
        });

        currentRecord.setValue({
            fieldId: 'comments',
            value: 'Use this area for any customer comments.'
        });        
    }

    function fieldChanged(context) {
        var currentRecord = context.currentRecord;

        if(context.fieldId == 'title'){
            var jobTitle = currentRecord.getValue({
                fieldId: 'title'
            });

            dialog.alert({
                title: 'Job Title Change',
                message: 'Job Title has changed to ' + jobTitle
            });
        }
    }
    
    return {
        pageInit: pageInit,
        fieldChanged: fieldChanged
    }
    
});

Save Record Function

The saveRecord function is triggered right after you click “Submit” after editing a record. The function runs before the record actually gets saved. This is the main structure for the saveRecord function.

function saveRecord(context) {
    var currentRecord = context.currentRecord;

}

I went ahead and added the var currentRecord = context.currentRecord; line since we will need to get a field value from the record in the function.

An element that is needed in the function is a return. It needs to either return true; or return false;

  • if return true; then the script allows the record to save.
  • if return false; then the script does not allow the record to save.

Also, just like the pageInit and fieldChange functions, the saveRecord functions need to be added to the return section at the bottom of the script. After adding it, that section should look like this:

return {
    pageInit: pageInit,
    fieldChanged: fieldChanged,
    saveRecord: saveRecord
}

Require Customer Comments

For the sake of this tutorial, we will prevent the Customer record from being saved if there are no comments. This isn’t the most practical way to use the saveRecord function since you could just make “comments” field mandatory, but this is an easy way to demonstrate how the function works.

The first thing we need to do is get the value of the “Comments” field. By now you should understand how to get a field value. When you look at the NetSuite Record Browser, you will notice the the “Comments” field id is comments. Here is the updated saveRecord function.

function saveRecord(context) {
    var currentRecord = context.currentRecord;

    var comments = currentRecord.getValue({
        fieldId: 'comments'
    });

}

Next we can add a condition “IF” statement to find out if the “Comments” field is empty of not. You can do either if (comments == null) or if (!comments) since they both say, “If there are no comments, then…”. I’ll use the shorter version. If the comments are empty, we want to give a message that says “Please enter comments for this customer” and return false;(prevent the record from being saved), but if the comments are not empty, we want to return true; (allow the record to be saved). Here’s what that looks like.

function saveRecord(context) {
    var currentRecord = context.currentRecord;

    var comments = currentRecord.getValue({
        fieldId: 'comments'
    });

    if (!comments){
        dialog.alert({
            title: 'Comments Needed',
            message: 'Please enter comments for this customer'
        });
        return false;
    }else {
        return true;
    }
}

Adding the saveRecord function to the rest of the script will look like this.

define(['N/record','N/search','N/ui/dialog', 'N/log'], function (record, search, dialog, log) {
   /**
   *@NApiVersion 2.0
   *@NScriptType ClientScript
   */
   
   function pageInit(context) {
       var currentRecord = context.currentRecord;
       
       var firstName = currentRecord.getValue({
           fieldId: 'firstname'
       });
       log.debug({
           title: 'Customer First Name', 
           details: firstName
       });
       
       dialog.alert({
           title: 'Announcement',
           message: 'You are viewing ' + firstName
       });

       currentRecord.setValue({
           fieldId: 'comments',
           value: 'Use this area for any customer comments.'
       });        
   }

   function fieldChanged(context) {
       var currentRecord = context.currentRecord;

       if(context.fieldId == 'title'){
           var jobTitle = currentRecord.getValue({
               fieldId: 'title'
           });

           dialog.alert({
               title: 'Job Title Change',
               message: 'Job Title has changed to ' + jobTitle
           });
       }
   }

   function saveRecord(context) {
       var currentRecord = context.currentRecord;

       var comments = currentRecord.getValue({
           fieldId: 'comments'
       });

       if (!comments){
           dialog.alert({
               title: 'Comments Needed',
               message: 'Please enter comments for this customer'
           });
           return false;
       }else {
           return true;
       }
   }
   
   return {
       pageInit: pageInit,
       fieldChanged: fieldChanged,
       saveRecord: saveRecord
   }
   
});

Update the Script Record and Test

Now you can update your script record and test. Here is reminder for how to update the script record.

  1. View the script record (don’t click on edit).
  2. Go to the “Scripts” tab
  3. Click “edit” next to the “Script File”
  4. Copy your new code and paste it in the window that pops up to replace the old code.
  5. Then save

Now you can navigate to a customer record and click on edit. When you save the customer without any comments, it should give you an alert and prevent you from saving. If you have comments, the record should save right away.

Add Sublists to a Suitelet (Build a Suitelet, Part 3)

With the 'ui/serverWidget' module, we can add custom sublists to our Suitelet page. In this tutorial, we will cover the basics of adding Suitelet sublists, as well as some helpful tips to help you along the way. If you're new to this mini-series about creating...

Build a Suitelet with the ‘ui/serverWidget’ Module Part 2

If you're new to this mini-series on how to create a Suitelet, check out our first part of the series here! How to Make Suitelets Look Like NetSuite Suitelets are an incredibly powerful tool. Not only are they great for making completely custom pages in NetSuite (like...

Build a Suitelet with the ‘ui/serverWidget’ Module Part 1

Introduction to Suitelets Suitelets are, in simple terms, a kind of page in NetSuite we can use any way we want. We can use SuiteScript modules to imitate the NetSuite user interface, or we can even use custom HTML to display our own unique information and UI....

Debugging Scripts with the SuiteScript “N/log” Module

About Logging Debugging your scripts is perhaps the most important skill as a developer, next to writing actual code. Debugging is a key to open many closed doors, and an eye to see through many thick walls. Several months ago, we made a blog about the basics of...

Using the ‘N/search’ SuiteScript Module

Searches in SuiteScript 2.0 The Search Module is an important key to coding effectively in NetSuite. Although there are numerous methods that this module supports (which the help documentation defines in detail), we will just address the three most commonly used...
0
Would love your thoughts, please comment.x
()
x