The powerful thing about NetSuite SuiteScripting is that you can interact with records throughout a script in the same way you interact with them in the user interface. You can load a record and save it. You can get field values and set field values. In this tutorial, I will walk you through how to get a value from a field and use it in the alert box that we created in the last tutorial, NetSuite Development 10: SuiteScript 2.0 NetSuite Modules.

If you are just jumping into this training series, I would recommend starting with the first tutorial, NetSuite Development 1: Understanding Script Types, and working through to this one so that you can get the full foundation for your journey into NetSuite SuiteScripting.

Here is the code from where we left off in the last tutorial.

define(['N/record','N/search','N/ui/dialog'], function (record, search, dialog) {
    /**
    *@NApiVersion 2.0
    *@NScriptType ClientScript
    */
    
    function pageInit(context) {
        dialog.alert({
            title: 'Announcement',
            message: 'My first SuiteScript!!' 
        });
    }
    
    return {
        pageInit: pageInit
    }
    
});

Get the Current Record

Since we need to get a field value from the record we need to get the record object first. To get the record object we have to add one simple line of code that allows us to do things (like getting a field value) with the record. It is context.currentRecord. You will want to create a variable that stores the record object so that you can use it throughout the rest of your script. That looks like var currentRecord = context.currentRecord;. Once you add that to your code, it should look like this.

define(['N/record','N/search','N/ui/dialog'], function (record, search, dialog) {
    /**
    *@NApiVersion 2.0
    *@NScriptType ClientScript
    */
    
    function pageInit(context) {
        var currentRecord = context.currentRecord;
        
        dialog.alert({
            title: 'Announcement',
            message: 'My first SuiteScript!!' 
        });
    }
    
    return {
        pageInit: pageInit
    }
    
});

Get A Field Value

Since the record we have been working with is the customer record, let’s get the customer’s first name. The first thing we need to do when getting the value of a field is to find the internal id of that field. Typically I do one of two things to find that. (1) Navigate to the customer record and click on the name of the “name” field. A field information box should appear, and the internal id should be in the top right corner of the box. If you don’t see the internal id, you will need to turn it on in your preferences by hovering over the home icon and clicking “Set Preferences.” Then go to the “General” tab and click on the “Show Internal IDs” checkbox. (2) You can also find the internal id by going to the NetSuite Records Browser and looking for the Customer record. Here’s a direct link to that. You’ll notice that the internal id for the “First Name” field is firstname.

The function to get the field value is getValue, and it needs one piece of information: the field’s internal id. To get the field value from the current customer record, you would start with the record object that we created, currentRecord and append getValue to the end of it like this: currentRecord.getValue. Then you add the field’s internal id to that like this.

currentRecord.getValue({
    fieldId: 'firstname'
});

Notice that we are telling it what field to get the value from by adding fieldId: 'firstname' inside ({ }). Let’s create a variable called “firstName” to hold the customer’s first name so we can use it later in the script. Creating that variable will look like this:

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

Adding that to our script, it should look like this:

define(['N/record','N/search','N/ui/dialog'], function (record, search, dialog) {
    /**
    *@NApiVersion 2.0
    *@NScriptType ClientScript
    */
    
    function pageInit(context) {
        var currentRecord = context.currentRecord;
        
        var firstName = currentRecord.getValue({
            fieldId: 'firstname'
        });
        
        dialog.alert({
            title: 'Announcement',
            message: 'My first SuiteScript!!' 
        });
    }
    
    return {
        pageInit: pageInit
    }
    
});

Add firstName To The Alert

Now we can add the firstName variable that we created to the alert box. You can have the message text say something like this: “You are viewing Bob” To do that you will need to modify the dialog.alert section like this.

dialog.alert({
    title: 'Announcement',
    message: 'You are viewing ' + firstName
});

We changed the text to “You are viewing,” and since we are adding in the firstName variable, we just need to do + firstName. Now your script should look like this:

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

Now you can navigate to the script record in NetSuite and update the code to the latest version.

  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

Then you can edit a customer record to see how the updated message now includes the customer’s first name!

Next scripting blog: NetSuite Development 12: SuiteScript Logging and Debugging

Have you missed any previous tutorials?

 

    0
    Would love your thoughts, please comment.x
    ()
    x