Client Script Save Record Function

NetSuite for Developers

The client sctipt saveRecord 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, in our previous post in this SuiteScript tutorial series, 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 the “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 that 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 or 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. Save

Conclusion

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.

You can access the next blog in this series here. And don’t forget to subscribe to our mailing list so you can receive our latest developing blogs directly in your inbox each week!