The following sample creates a query for transaction records, joins the query with another query type, and runs the query as a paged query.
/**
* @NApiVersion 2.1
*/
require(['N/query'], query => {
// Create a query definition for transaction records
let myTransactionQuery = query.create({
type: query.Type.TRANSACTION
});
// Join the original query definition based on the employee field. In a transaction
// record, the employee field contains a reference to an employee record. When you
// join based on this field, you are joining the query definition with the employee
// query type, and you can access the fields of the joined employee record in
// your query.
let myEmployeeJoin = myTransactionQuery.autoJoin({
fieldId: 'employee'
});
// Create a condition for the transaction query
let transactionCondition = myTransactionQuery.createCondition({
fieldId: 'isreversal',
operator: query.Operator.IS,
values: true
});
myTransactionQuery.condition = transactionCondition;
// Create a query column
myTransactionQuery.columns = [
myEmployeeJoin.createColumn({
fieldId: 'subsidiary'
})
];
// Sort the query results based on a query column
myTransactionQuery.sort = [
myTransactionQuery.createSort({
column: myTransactionQuery.columns[0],
ascending: false
})
];
// Run the query as a paged query with 10 results per page
let results = myTransactionQuery.runPaged({
pageSize: 10
});
log.debug(results.pageRanges.length);
log.debug(results.count);
// Retrieve the query results using an iterator
let iterator = results.iterator();
iterator.each(function(result) {
let page = result.value;
log.debug(page.pageRange.size);
return true;
})
// Alternatively, retrieve the query results by looping through
// each result
for (let i = 0; i < results.pageRanges.length; i++) {
let page = results.fetch(i);
log.debug(page.pageRange.size);
}
});