Week 3 • Module 6

N/currentRecord Module

Client-side record manipulation for real-time UI updates

🌐 Client-Side vs Server-Side

The key difference between N/record and N/currentRecord:

N/record N/currentRecord
Server-side execution Client-side (browser) execution
Consumes governance units No governance consumption
User sees nothing until save Updates appear in real-time
Used in User Event, Scheduled, Map/Reduce Used in Client Scripts only

💡 Why It Matters

Client Scripts provide instant feedback. When a user changes a field, you can immediately validate, calculate, or update other fields without waiting for a server round-trip.

🔧 Getting the Current Record

In Client Scripts, you access the record the user is viewing via currentRecord.get():

define(['N/currentRecord'],
    function(currentRecord) {
        
        function pageInit(context) {
            // Access through context in entry points
            const rec = context.currentRecord;
            const customerId = rec.getValue({ fieldId: 'entity' });
        }
        
        function customButtonHandler() {
            // Outside entry points, use currentRecord.get()
            const rec = currentRecord.get();
            const memo = rec.getValue({ fieldId: 'memo' });
        }
        
        return {
            pageInit: pageInit
        };
    }
);

⚠️ Two Ways to Access

Inside entry points (pageInit, fieldChanged, etc.), use context.currentRecord. In custom functions called by buttons or other handlers, use currentRecord.get().

📝 Reading and Writing Fields

The same methods as N/record, but changes appear immediately in the UI:

function fieldChanged(context) {
    const rec = context.currentRecord;
    const fieldId = context.fieldId;
    
    if (fieldId === 'quantity') {
        const qty = rec.getValue({ fieldId: 'quantity' });
        const rate = rec.getValue({ fieldId: 'rate' });
        
        // Calculate and set the amount - user sees it instantly
        rec.setValue({
            fieldId: 'amount',
            value: qty * rate
        });
    }
}

📊 Sublist Operations

Working with sublists in Client Scripts requires the dynamic pattern:

Selecting a Line

// Select line 0 (first line) of the item sublist
rec.selectLine({
    sublistId: 'item',
    line: 0
});

Setting Current Sublist Value

// After selecting a line, modify it
rec.setCurrentSublistValue({
    sublistId: 'item',
    fieldId: 'quantity',
    value: 5
});

Committing the Line

// Save changes to the selected line
rec.commitLine({
    sublistId: 'item'
});

✅ Dynamic Mode Pattern

Client-side sublists always work in dynamic mode: Select → Modify → Commit. This mirrors how users interact with sublists in the UI.

🧪 Practical Example: Memo Field Validation

This example from the training replaces "bad" with a smiley face when the user exits the memo field:

/**
 * @NApiVersion 2.1
 * @NScriptType ClientScript
 */
define(['N/currentRecord'],
    function(currentRecord) {
        
        function validateField(context) {
            const rec = context.currentRecord;
            const fieldId = context.fieldId;
            
            if (fieldId === 'memo') {
                let memoValue = rec.getValue({ fieldId: 'memo' });
                
                if (memoValue && memoValue.includes('bad')) {
                    memoValue = memoValue.replace('bad', ':)');
                    rec.setValue({
                        fieldId: 'memo',
                        value: memoValue
                    });
                }
            }
            
            return true; // Allow field change
        }
        
        return {
            validateField: validateField
        };
    }
);

📚 Entry Point: validateField

validateField runs when the user exits a field. Return true to accept the change, false to reject it and keep focus on the field.

🧪 Testing in the Console

You can test N/currentRecord directly in the browser console while viewing a record:

// Open browser DevTools (F12) while on a record
// Paste this to load the module:
require(['N/currentRecord'], function(currentRecord) {
    const rec = currentRecord.get();
    console.log('Record Type:', rec.type);
    console.log('Record ID:', rec.id);
    console.log('Entity:', rec.getValue({ fieldId: 'entity' }));
});

💡 Development Tip

Use console testing to explore field IDs, test getValue/setValue, and prototype logic before writing full scripts.

📖 Finding This in the Docs

To look up N/currentRecord methods:

  1. Go to docs.oracle.com → SuiteScript 2.x Modules
  2. Click on N/currentRecord Module
  3. Review the currentRecord.get() method and CurrentRecord object members

Key pages to bookmark:

🎯 Key Takeaways