title: Performing a Sum or Grouped Query in SuiteScript 2.0
body: <p>Can any give a solid example of how to perform a grouped or summed query, ie "<strong>How many sales orders has Jane Smith done this month</strong>" in suitescript 2.0. The NetSuite Help Center examples and docs for 2.0 are so scattered and half-baked imho.</p>
  
  <p>Any help would be appreciated. Below is the code im using. which returns an <strong>UNKNOWN ERROR</strong>. If you change search.Summary.GROUP and search.Summary.COUNT to lowercase (search.Summary.group, etc), then you get one result where the <strong>orderCount</strong> column actually contains the internal id, not an aggregate count</p>
  
  <pre><code>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;
  </code></pre>
  
  <p>And here is the result when I change the summary to lowercase (search.Summary.<strong>group</strong>, etc)</p>
  
  <pre><code>{
      "error": false,
      "msg": "TESTING_POST_METHOD",
      "data": [
          {
              "title": "Order Count by Sales Rep",
              "details": "Victor Beisel has sold 524963 orders."
          }
      ]
  }
  </code></pre>
  
  <p>The <strong>524963</strong> 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.<strong>GROUP</strong>, etc) as stated by the NetSuite docs</p>
  
  <pre><code>{
      "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)",
              "&lt;anonymous&gt;(/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)",
                  "&lt;anonymous&gt;(/SuiteScripts/XpelAffiliateOrders/XPELSS2_3PO_Index.js:40)",
                  "post(/SuiteScripts/XpelAffiliateOrders/XPELSS2_3PO_Index.js:117)"
              ],
              "notifyOff": false
          },
          "id": "",
          "notifyOff": false
      },
      "data": null
  }
  </code></pre>