Here’s how you can use search.lookupFields in SuiteScript 2.0 to retrieve the created date of a Sales Order associated with a given Invoice.
define([‘N/search’], function(search) {
function getSalesOrderCreatedDate(invoiceId) {
try {
// Lookup the ‘createdfrom’ field of the Invoice
var invoiceData = search.lookupFields({
type: search.Type.INVOICE,
id: invoiceId,
columns: [‘createdfrom’]
});
var salesOrderId = invoiceData.createdfrom[0] ? invoiceData.createdfrom[0].value : null;
if (!salesOrderId) {
return ‘No Sales Order linked to this Invoice.’;
}
// Lookup the created date of the Sales Order
var salesOrderData = search.lookupFields({
type: search.Type.SALES_ORDER,
id: salesOrderId,
columns: [‘datecreated’]
});
return salesOrderData.datecreated || ‘Created Date not found.’;
} catch (error) {
return ‘Error retrieving data: ‘ + error.message;
}
}
return {
getSalesOrderCreatedDate: getSalesOrderCreatedDate
};
});
Explanation:
- Step 1: Retrieves the
createdfromfield from the Invoice, which contains the associated Sales Order ID. - Step 2: Uses the retrieved Sales Order ID to look up the
datecreatedfield from the Sales Order. - Step 3: Returns the Sales Order’s created date.