will post one that doesn't shortly
# suitescript
e
will post one that doesn't shortly
This does not work:
Copy code
/**
 * Retrieves the Sales Rep with the largest (by Total)
 * Sales Order in each Period
 *
 * @author Eric T Grubaugh <eric@stoic.software>
 */
require(["N/search"], function (s) {
    s.create({
        type: s.Type.SALES_ORDER,
        filters: [
            ["mainline", <http://s.Operator.IS|s.Operator.IS>, true], "and",
["salesrep", s.Operator.NONEOF, "@NONE@"]
        ],
        columns: [
            {name: "postingperiod", summary: s.Summary.GROUP},
            s.createColumn({
                name: "salesrep",
                summary: s.Summary.MAX
            }).setWhenOrderedBy({name:"totalamount", join: "x"})
        ]
    }).run().getRange({start: 0, end: 10}).forEach(printOrder);
    
    function printOrder(result) {
        console.log(
            result.getValue({name:"salesrep", summary:s.Summary.MAX}) +
            " had the largest order in " +
            result.getText({name:"postingperiod", summary:s.Summary.GROUP})
        );
        return true;
    }
});
It gets the right transactions/periods, but then just has the same Sales Rep every time. The Sales Rep it shows is not even set on the associated transaction, so it's really strange
It seems like it's actually returning the Maximum Sales Rep from each Period no matter what (i.e. the "last" Sales Rep, alphabetically, regardless of whether they're actually associated to the largest Transaction that period)
And oh btw an identical search in the UI works perfectly
aaand just for good measure, this also works just fine:
Copy code
/**
 * Retrieves the Contact on the most recent Case filed
 * by each Customer
 *
 * @author Eric T Grubaugh <eric@stoic.software>
 */
require(["N/search"], function (s) {
    s.create({
        type: s.Type.SUPPORT_CASE,
        filters: [["contact", s.Operator.ISNOTEMPTY, ""]],
        columns: [
            {name: "company", summary: s.Summary.GROUP},
            s.createColumn({
                name: "contact",
                summary: s.Summary.MAX
            }).setWhenOrderedBy({name:"createddate", join: "x"})
        ]
    }).run().getRange({start: 0, end: 10}).forEach(printOrder);
    
    function printOrder(result) {
        console.log(
            result.getValue({name:"contact", summary:s.Summary.MAX}) +
            " is the Contact on " +
            result.getValue({name:"company", summary:s.Summary.GROUP}) +
            "'s most recent Case."
        );
        return true;
    }
});
So it's not totally broken, just partially
lol I know I'm talking to myself in here, but I figured it out in case anyone finds this. My sales reps seemed inaccurate because the Rep it was showing was inactive. Added a filter to remove inactive Sales Reps and it worked.