Getting and setting field values on NetSuite records are some of the most common actions you will do with SuiteScript. So it is a good idea to get a really good handle on these at the beginning. If you missed an earlier tutorial on getting field values, you can find it here. In this tutorial we will focus on how to set record field values.

Set Field Value

To pick up where we left off in the last tutorial on logging and debugging, here is the latest code we created together:

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
        });
    }
    
    return {
        pageInit: pageInit
    }
    
});

Since most of the setup has been done in previous tutorials, adding the code to update a field is not complicated. Actually the code structure to get a field’s value is almost exactly the same as the code structure to set a field’s value except for one additional piece. For getting a value, all the information you need is the fieldId of the field you’re getting the value from. For setting a field value, you still need the fieldId of the field being set, but you also need the value that you are setting on the field. Here is the basic structure:

currentRecord.setValue({
    fieldId: 'theIdOfTheField',
    value: 'The value being set on the field'
});

Notice that the section starts off with currentRecord.setValue and that there are two peices of information to input: fieldId and value. The first thing you need to do is find the field id of the field being set. Since we are running this script on the customer record, let’s use the “Comments” field. If you navigate to the customer record section in the NetSuite Records Browser and look for the “Comments” field, you’ll see that the internal id is just “comments.” After updating the fieldId in the script, the currentRecord.setValue section should look like this:

currentRecord.setValue({
    fieldId: 'comments',
    value: 'The value being set on the field'
});

You can also change the value to something like value: 'Use this area for any customer comments.'. Putting it all together, the script should 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.'
        });        
    }
    
    return {
        pageInit: pageInit
    }
    
});

Update the Script Record and Test

To test the latest script version, you just need to update the script record in NetSuite with the latest script changes. Here are the steps to update the script:

  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. You should see the alert box appear, and after you click “OK” you should see the “Comments” field change to “Use this area for any customer comments.” If you are not in a Sandbox environment, you need to be careful that you don’t save the customer record since you may lose information that previously existed in the “Comments” field. I would recommend to always use a Sandbox for these code samples.

We hope you enjoyed learning how to set record field values in NetSuite! If this mini tutorial series on SuiteScript has been helpful to you so far, check out the next installment of the series. And don’t forget to subscribe to our mailing list so you can receive our latest developing blogs directly in your inbox each week!