Add search query as the data source to a template

We can pass search query as a data source to a template instead of a single record data.
Use the function TemplateRenderer.addQuery(options) for this. This is a part of the N/render module and is available for all server scripts(of the 2.x version).
The parameters require are:

  1. ‘options.id’ of string type which stores the id of search if options.query is not specified
  2. ‘options.templateName’ is the name of the template used
  3. ‘options.query’ is the query and can is required if ‘options.id’ is not specified
    The optional parameters are:
  4. ‘options.pageIndex’ stores the page Index as a number
  5. ‘options.pageSize’ stores page size as a number
    This is helpful when data is to be fetched from a search query. Refer the code for more:

//Add additional code

var search = query.create({
type: query.Type.TRANSACTION
});
var transactionlines = search.join({
fieldId: “transactionlines”
});
search.condition = transactionlines.createCondition({
fieldId: “netamount”,
operator: query.Operator.GREATER,
values: 1
});
search.columns = [
transactionlines.createColumn({
fieldId: “netamount”,
label: “Amount”
}),
];

// get instance of TemplateRenderer – https://system.netsuite.com/app/help/helpcenter.nl?fid=section_4412065265.html
var renderer = render.create();
renderer.templateContent = “<#list page as line>${line[‘0@label’]}:${line[0]}\n”

renderer.addQuery({
templateName: “page”,
query: search,
pageSize: 3, // minimum page size is 5, so result will contain 5 lines instead of 3
pageIndex: 0,
})

// Add additional code

Leave a comment

Your email address will not be published. Required fields are marked *