Skip to main content
Added some commentary. Changed the formatting
Source Link
superfell
  • 8k
  • 3
  • 37
  • 46

What happens if you alias the field, does that work?

AggregateResult[] groupedResults = [
  SELECT Account.Type type, count(Id) oppCount
  FROM Opportunity 
  GROUP BY Account.Type];

// snip

System.Debug(ar.Get('type'));

Notice that if you are doing an aggregation (GROUP BY) you can create an alias for both the summarized field and for other fields. This is not documented, but is the only way to get this to work. In your GROUP BY field you need to use the full name of the field, but when getting info from the AggregateResults you use the alias. Confusing!

What happens if you alias the field, does that work?

AggregateResult[] groupedResults = [
  SELECT Account.Type type, count(Id) oppCount
  FROM Opportunity 
  GROUP BY Account.Type];

// snip

System.Debug(ar.Get('type'));

Notice that if you are doing an aggregation (GROUP BY) you can create an alias for both the summarized field and for other fields. This is not documented, but is the only way to get this to work. In your GROUP BY field you need to use the full name of the field, but when getting info from the AggregateResults you use the alias. Confusing!

What happens if you alias the field, does that work?

AggregateResult[] groupedResults = [
  SELECT Account.Type type, count(Id) oppCount
  FROM Opportunity 
  GROUP BY Account.Type];

// snip

System.Debug(ar.Get('type'));

Notice that if you are doing an aggregation (GROUP BY) you can create an alias for both the summarized field and for other fields. This is not documented, but is the only way to get this to work. In your GROUP BY field you need to use the full name of the field, but when getting info from the AggregateResults you use the alias.

Added some commentary. Changed the formatting
Source Link

What happens if you alias the field, does that work?

AggregateResult[] groupedResults = [SELECT[
  SELECT Account.Type type, count(Id) oppCount
  FROM Opportunity 
  GROUP BY Account.Type];

// snip

System.Debug(ar.Get('type'));

Notice that if you are doing an aggregation (GROUP BY) you can create an alias for both the summarized field and for other fields. This is not documented, but is the only way to get this to work. In your GROUP BY field you need to use the full name of the field, but when getting info from the AggregateResults you use the alias. Confusing!

What happens if you alias the field, does that work?

AggregateResult[] groupedResults = [SELECT Account.Type type, count(Id) FROM Opportunity GROUP BY Account.Type];

// snip

System.Debug(ar.Get('type'));

What happens if you alias the field, does that work?

AggregateResult[] groupedResults = [
  SELECT Account.Type type, count(Id) oppCount
  FROM Opportunity 
  GROUP BY Account.Type];

// snip

System.Debug(ar.Get('type'));

Notice that if you are doing an aggregation (GROUP BY) you can create an alias for both the summarized field and for other fields. This is not documented, but is the only way to get this to work. In your GROUP BY field you need to use the full name of the field, but when getting info from the AggregateResults you use the alias. Confusing!

Source Link
Matt Lacey
  • 25.8k
  • 7
  • 68
  • 153

What happens if you alias the field, does that work?

AggregateResult[] groupedResults = [SELECT Account.Type type, count(Id) FROM Opportunity GROUP BY Account.Type];

// snip

System.Debug(ar.Get('type'));