Query.autoJoin(options) is a powerful method in the N/query module of SuiteScript 2.x, introduced in NetSuite 2018.2, that allows developers to create join relationships in queries. This method simplifies the process of joining components in a query by leveraging the options.fieldId parameter to define the join relationship.
Purpose: The method establishes join relationships in queries. It’s commonly used after defining an initial query with query.create(options) and allows developers to chain additional joins to retrieve related data.
Usage: Typically used in both client and server-side scripts.
Method Overview
- Returns: The method returns a
query.Componentobject, which represents the newly joined query component. - Supported Script Types: Works in both client scripts and server scripts.
- Governance: No governance limit is enforced for this method.
- Module: Part of the N/query module.
- Parent Object: This method is a part of the
query.Queryobject. - Introduced In: NetSuite 2018.2.
Parameters
- options: A JavaScript object that defines the join parameters.
- fieldId (string): Required. Specifies the field type (column) that joins the parent query component to the new query component.
- Note: Field IDs can be obtained from the Records Catalog. The Records Catalog lists every record type and field available for joining.
Sample Code:
var myTransactionQuery = query.create({
type: query.Type.TRANSACTION
});
var myEntityJoin = myTransactionQuery.autoJoin({
fieldId: 'entity'
});
myTransactionQuery.columns = [
myEntityJoin.createColumn({
fieldId: 'subsidiary'
})
];
myTransactionQuery.sort = [
myTransactionQuery.createSort({
column: myTransactionQuery.columns[0],
ascending: false
})
];
var results = myTransactionQuery.runPaged({
pageSize: 10
});