Hi
I am trying to get the first row returned from a saved search in SuiteScript 2.0.
I can see references to getRange as a function online, though I can't see how to use that correctly in the following script:
/**
*@NApiVersion 2.x
*@NScriptType ClientScript
*/
define(["N/search"], function (search) {
/**
* @param {ClientScriptContext.pageInit} context
*/
function pageInit(context) {
var customerSearchObj = search.create({
type: "customer",
filters: [],
columns: [
search.createColumn({
name: "internalid",
summary: "GROUP",
label: "Internal ID",
}),
search.createColumn({
name: "entityid",
summary: "MAX",
label: "ID",
}),
search.createColumn({
name: "altname",
summary: "GROUP",
label: "Name",
}),
search.createColumn({
name: "subsidiarynohierarchy",
summary: "GROUP",
label: "Primary Subsidiary (no hierarchy)",
}),
search.createColumn({
name: "formuladate",
summary: "MAX",
formula: "TO_DATE(TRUNC({datecreated}))",
label: "Date Created",
}),
search.createColumn({
name: "formulanumeric",
summary: "GROUP",
formula: "TO_NUMBER(REGEXP_REPLACE({entityid}, 'ACC',''))",
sort: search.Sort.DESC,
label: "Customer ID",
}),
],
});
var searchResultCount = customerSearchObj.runPaged().count;
var rs=customerSearchObj.run();
var results = rs.getRange({
start: 0,
end: 10
});
var rowJson = JSON.stringify(results);
//var resultSet=customerSearchObj.run();
log.debug("customerSearchObj result count", searchResultCount);
for(var i=0; i<results.length;i++){
log.debug("highest number", results[i]);
}
return true
}
return {
pageInit: pageInit,
};
});
This is what the saved search that is being returned looks like
The blue highlighted row is the one I am after
I have tried multiple variations on this script including one where I was able to get the data in the fifth column with:
customerSearchObj.run().each(function (result) {
// var range= resultSet.getRange(0,1);
var customerId = result.getValue(result.columns[5]);
log.debug("customer ID", customerId);
// log.debug("range", range);
// .run().each has a limit of 4,000 results
return true;
});
How do I narrow this down so that I have the results of the 5th column, but only the first row?
EDIT: I have spent the last 3 days going through all of Suiteanswers trying to see where I am going wrong since my code looked the same as the samples...
I think I might know the reason:
Do the methods such as getValue or columns not work for resultSet or result objects if the columns are summarised?