Can any give a solid example of how to perform a grouped or summed query, ie "How many sales orders has Jane Smith done this month" in suitescript 2.0. The NetSuite Help Center examples and docs for 2.0 are so scattered and half-baked imho.
Any help would be appreciated. Below is the code im using. which returns an UNKNOWN ERROR. If you change search.Summary.GROUP and search.Summary.COUNT to lowercase (search.Summary.group, etc), then you get one result where the orderCount column actually contains the internal id, not an aggregate count
var results = [],
GROUP = search.Summary.GROUP,
COUNT = search.Summary.COUNT;
var mySalesOrderSearch = search.create({
type: 'salesorder',
columns: [
"trandate",
{
name: 'salesrep',
summary: GROUP
}, {
name: 'internalid',
summary: COUNT
}],
filters: [{
name: 'mainline',
operator: 'is',
values: ['T']
}, {
name: "trandate",
operator: "within",
values: ["thisyear"]
}]
});
mySalesOrderSearch.run().each(function (result) {
var repName = result.getText({
"name": "salesrep",
"summary": GROUP
});
var orderCount = parseInt(result.getValue({
"name": "internalid",
"summary": COUNT
}), 10);
var msg = {
"title": "Order Count by Sales Rep",
"details": repName + " has sold " + orderCount + " orders."
};
results.push(msg);
log.debug(msg);
});
return results;
And here is the result when I change the summary to lowercase (search.Summary.group, etc)
{
"error": false,
"msg": "TESTING_POST_METHOD",
"data": [
{
"title": "Order Count by Sales Rep",
"details": "Victor Beisel has sold 524963 orders."
}
]
}
The 524963 number in the details key is the internal id of a sales order, not the true count. And here is the result when you use the capitalized summary token (search.Summary.GROUP, etc) as stated by the NetSuite docs
{
"error": true,
"msg": {
"type": "error.SuiteScriptError",
"name": "UNEXPECTED_ERROR",
"message": null,
"stack": [
"each(N/searchObject)",
"TESTING_POST_METHOD(/SuiteScripts/XpelAffiliateOrders/XPELSS2_3PO_Index.js:159)",
"<anonymous>(/SuiteScripts/XpelAffiliateOrders/XPELSS2_3PO_Index.js:40)",
"post(/SuiteScripts/XpelAffiliateOrders/XPELSS2_3PO_Index.js:117)"
],
"cause": {
"type": "internal error",
"code": "UNEXPECTED_ERROR",
"details": null,
"userEvent": null,
"stackTrace": [
"each(N/searchObject)",
"TESTING_POST_METHOD(/SuiteScripts/XpelAffiliateOrders/XPELSS2_3PO_Index.js:159)",
"<anonymous>(/SuiteScripts/XpelAffiliateOrders/XPELSS2_3PO_Index.js:40)",
"post(/SuiteScripts/XpelAffiliateOrders/XPELSS2_3PO_Index.js:117)"
],
"notifyOff": false
},
"id": "",
"notifyOff": false
},
"data": null
}