
Tips when using lookupFields()
Here are a couple of tips when working with search.lookupFields()
in SuiteScript 2.1.
Assign variables automatically
It's common to retrieve one or more fields from a record using search.lookupFields
. Often I see this done in two or more steps, and usually these steps can be combined.
I'll often see code like this:
const results = search.lookupFields({
type: search.Type.CUSTOMER,
id: 12345,
columns: [
'companyname'
]
});
const companyName = results['companyname'];
We can combine the retrieval of the data and the assigning of the variable in the same step like this:
const companyName = search.lookupFields({
type: search.Type.CUSTOMER,
id: 12345,
columns: [
'companyname'
]
})['companyname'];
With object destructuring, we can even retrieve and assign multiple values in the same line of code.
const { companyname, email, phone } = search.lookupFields({
type: search.Type.CUSTOMER,
id: 12345,
columns: [ 'companyname', 'email', 'phone' ]
});
If we want to assign one of these values to a different variable name, we can assign that variable here as well. For example, if we want to use the variable customer
instead of companyname
then we can do this.
const { companyname: customer, email, phone } = search.lookupFields({
type: search.Type.CUSTOMER,
id: 12345,
columns: [ 'companyname', 'email', 'phone' ]
});
Access Connected Data
You can access joined data when using search.lookupFields()
. For example, if you have an invoice ID in your script, you can access data from the Sales Order which created that invoice in a single call using dot notation on the createdfrom
field.
const salesOrderNumber = search.lookupFields({
type: search.Type.INVOICE,
id: invoiceId,
columns: [ 'createdfrom.tranid' ]
})['createdfrom.tranid'];