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
nlapiLookupFieldis faster than loading the entire record withnlapiLoadRecord. - 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
- Current Approach: Using
nlapiLoadRecordto load records and get field values. - Optimized Approach: Using
nlapiLookupFieldto 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
- Using
nlapiLookupField:
nlapiLookupFieldis used to retrieve thecustrecord_jj_voucher_casefield from thecustomrecord_voucherrecord.- Another
nlapiLookupFieldcall retrieves thenameandaltnamefields from thecustomrecord_jj_voucher_caserecord using the retrievedvouchercaseid.
- 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.