Using nlapiLookupField for Efficient Data Retrieval in SuiteScript

Overview

In SuiteScript, nlapiLookupField is a powerful function that allows you to fetch specific fields from a record without loading the entire record. This can significantly improve performance and reduce the complexity of your code. This post will demonstrate how to use nlapiLookupField to retrieve fields efficiently and provide a practical example.

Why Use nlapiLookupField?

  • Performance: Fetching specific fields using nlapiLookupField is faster than loading the entire record with nlapiLoadRecord.
  • Simplicity: Reduces the lines of code and makes your script cleaner and easier to maintain.

Example Scenario

We have a custom record type customrecord_voucher which references another custom record type customrecord_jj_voucher_case via the field custrecord_jj_voucher_case. Our goal is to fetch specific fields from both records efficiently.

Step-by-Step Guide

  1. Current Approach: Using nlapiLoadRecord to load records and get field values.
  2. Optimized Approach: Using nlapiLookupField to directly fetch required fields.

Original Code (Using nlapiLoadRecord)

GetvoucherCaseId: function (voucherIds) {

  var voucherFields = [];

  try {

    nlapiLogExecution(‘debug’, ‘GetvoucherCaseId-suitescriptJS’);

    voucherIds.forEach(function (id) {

      var voucherRecord = nlapiLoadRecord(‘customrecord_voucher’, id);

      var vouchercaseid = voucherRecord.getFieldValue(‘custrecord_jj_voucher_case’);

      var caseRecord = nlapiLoadRecord(‘customrecord_jj_voucher_case’, vouchercaseid);

      var CaseID = caseRecord.getFieldValue(‘name’);

      var caseName = caseRecord.getFieldValue(‘altname’);

       

      var fields = {

        vouchercaseid: vouchercaseid,

        CaseID: CaseID,

        caseName: caseName,

      };

      voucherFields.push(fields);

    });

  } catch (e) {

    nlapiLogExecution(‘ERROR’, ‘Error loading voucher case ID’, e.toString());

  }

  return voucherFields;

},

GetvoucherCaseId: function (voucherIds) {

  var voucherFields = [];

  try {

    nlapiLogExecution(‘debug’, ‘GetvoucherCaseId-suitescriptJS’);

    voucherIds.forEach(function (id) {

      var voucherLookup = nlapiLookupField(‘customrecord_voucher’, id, [‘custrecord_jj_voucher_case’]);

      var vouchercaseid = voucherLookup.custrecord_jj_voucher_case;

      if (vouchercaseid) {

        var caseLookup = nlapiLookupField(‘customrecord_jj_voucher_case’, vouchercaseid, [‘name’, ‘altname’]);

        var CaseID = caseLookup.name;

        var caseName = caseLookup.altname;

        var fields = {

          vouchercaseid: vouchercaseid,

          CaseID: CaseID,

          caseName: caseName,

        };

        voucherFields.push(fields);

      } else {

        nlapiLogExecution(‘DEBUG’, ‘No Case ID found for voucher ID’, id);

      }

    });

  } catch (e) {

    nlapiLogExecution(‘ERROR’, ‘Error loading voucher case ID’, e.toString());

  }

  return voucherFields;

},

Explanation

  1. Using nlapiLookupField:
  • nlapiLookupField is used to retrieve the custrecord_jj_voucher_case field from the customrecord_voucher record.
  • Another nlapiLookupField call retrieves the name and altname fields from the customrecord_jj_voucher_case record using the retrieved vouchercaseid.
  1. Advantages:
  • Reduced Load: By using nlapiLookupField, we avoid loading entire records, which reduces server load and improves script performance.
  • Cleaner Code: The script is shorter and easier to read, as it directly fetches required fields without unnecessary intermediate steps.

Conclusion

Using nlapiLookupField is a best practice for fetching specific fields from records in SuiteScript. It enhances performance and simplifies the code. Adopting this approach in your SuiteScript projects can lead to more efficient and maintainable scripts.

Leave a comment

Your email address will not be published. Required fields are marked *