Only Aggregate Expressions Use Field Aliasing – SOQL Error
Out of many other errors we encounter, this is one with the SOQL. Many times, when writing SOQL, we face the error – “Only Aggregate Expressions Use Field Aliasing” and we are stuck that what’s happening, everything seems all right to me.
You must be missing a comma in the SOQL between two fields before FROM keyword.
Aliasing is when you want to give some different name to the column in any SOQL result, but you can only do so in aggregate queries of Salesforce, where you use some aggregate functions like sum(Custom_Field__c), count(Id) or any other similar aggregate function.
Below is a valid SOQL using aggregate function with aliasing. We are trying to give “amountTotal” name to Sum(Amount) column.
[Select Sum(Amount) amountTotal, AccountId from Opportunity group by AccountId];
BUT, because aliasing is not allowed in normal SOQL, that is why below SOQL is considered faulty, and will encounter the error “Only Aggregate Expressions Use Field Aliasing”.
[select id, Name Amount from Opportunity];
You might notice we are missing a comma between Name & Amount field, which is why Salesforce thinks that we are trying to give a different label to Name column in SOQL result but that is not allowed.
If you ever encounter this error in future, look for missing comma between two fields in your SOQL.
Happy coding !!