How to sort Opps in Chart by StageName as per order in Sales Process

First off, it would be great if sorting of a table and chart can be set to the order of picklist values. The most obvious case of this is on the StageName field in Opportunities.

Here’s a workaround snippet for the following use case:
Basic model on the Opportunities object which is then used in a Chart where {{StageName}} is used to split the values. Column Chart called “Opps by Qtr by Stage”.

Before

After

// this snippet has been created in order to sort the series in the chart by the stage order // in the sales process assigned to the Record Type var chartObj = arguments[0], Opportunities = skuid.$M('Opportunities'), StageName, sortedSeries= [], $ = skuid.$; // to get the sort order, we need to find the Stage field metadata in the Opportunities models list of fields $.each(Opportunities.fields, function(f,field){ if (field.id === "StageName"){ StageName = field; return false; } }); // now use this field to create a temporary array to replace the current series // loop through entries in order to then find the corresponding series by the same name // in order to add it to the temporary array $.each(StageName.picklistEntries, function(p,entry){ $.each(chartObj.series, function(s,series){ if (series.name === entry.label){ sortedSeries.push(series); } }); }); // empty original series chartObj.series.length = 0; // insert sorted chart $.each(sortedSeries,function(ss,sseries){ chartObj.series.push(sseries); });

Hi Pat, I’ve tried adding this snippet to a chart that’s using an Opportunity Line Item aggregate model. I get the error

  1. There was a problem rendering a component of type skuidvis__chart: Cannot read property ‘picklistEntries’ of undefined

Here is the javascript, can you see anything wrong with what I’ve written?

// this snippet has been created in order to sort the series in the chart by the stage order
// in the sales process assigned to the Record Type

var chartObj = arguments[0],
    Opportunities = skuid.$M(‘PopularProductsThisFY’),
StageName,
sortedSeries= ,
$ = skuid.$;

// to get the sort order, we need to find the Stage field metadata in the Opportunities models list of fields
$.each(Opportunities.fields, function(f,field){
    if (field.id === “OpportunityStageName”){
        StageName = field;
        return false;
    }
});

// now use this field to create a temporary array to replace the current series
// loop through entries in order to then find the corresponding series by the same name
// in order to add it to the temporary array
$.each(StageName.picklistEntries, function(p,entry){
    $.each(chartObj.series, function(s,series){
        if (series.name === entry.label){
            sortedSeries.push(series);
        }
    });
});

// empty original series
chartObj.series.length = 0;

// insert sorted chart
$.each(sortedSeries,function(ss,sseries){
    chartObj.series.push(sseries);
});

Everything looks in order. You have stepped through the code? This is so long ago that this may need to be adjusted.

hmmmmm I’m not really a developer, so __I’m never really sure what I’m looking at we’ve these things.

Essentially the only difference between my code and your code are the lines highlighted below

Would the code be failing because your model is an Opportunity model, whereas mine is an OpportunityLineItem model?

Here is my aggregate model

Here is my grouping