function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Princes91221Princes91221 

only aggregate expressions use field aliasing (6:39)

Hello, 
Hope you guys are doing well in this tough situation.
I'm trying to send welcome email to customers.
I did setup in Classic Email Templates.

public with sharing class WelcomeEmailProcessor {
    // public WelcomeEmailProcessor() {
        
    // }
    public static void sendMail(List<String> address, String subject, String body) {
        EmailTemplate emailTemplate = [Select Id, Subject, Email Body FROM EmailTemplate where name = 'Dreamhouse welcomes e-mail'];
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setToAddresses(address);
        mail.setSubject(subject);
        mail.setHtmlBody(body);
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }
}


I'm getting an error stasting only aggregate expressions use field aliasing on line number 6.
{
     "owner": "deploy-errors",
    "severity": 8,
    "message": "only aggregate expressions use field aliasing (6:39)",
    "source": "ApexClass",
    "startLineNumber": 6,
    "startColumn": 39,
    "endLineNumber": 6,
    "endColumn": 39
}

Thank you in advance! :-)
Best Answer chosen by Princes91221
SwethaSwetha (Salesforce Developers) 
HI Princes91221 ,

The issue here is with the SOQL. There is a space between Email Body because of which Salesforce thinks that we are trying to give a different label to Email and hence the error

Select Id, SubjectEmail Body FROM EmailTemplate where name = 'Dreamhouse welcomes e-mail'

Try something like
Select Id, Subject,Name, Body FROM EmailTemplate where name = 'Dreamhouse welcomes e-mail'


Related: https://www.sfdcpoint.com/salesforce/only-aggregate-expressions-use-field-aliasing-soql-error/
https://developer.salesforce.com/forums/?id=9062I000000IDrCQAW

Hope this helps you. Please mark this answer as best so that others facing the same issue will find this information useful. Thank you

All Answers

SwethaSwetha (Salesforce Developers) 
HI Princes91221 ,

The issue here is with the SOQL. There is a space between Email Body because of which Salesforce thinks that we are trying to give a different label to Email and hence the error

Select Id, SubjectEmail Body FROM EmailTemplate where name = 'Dreamhouse welcomes e-mail'

Try something like
Select Id, Subject,Name, Body FROM EmailTemplate where name = 'Dreamhouse welcomes e-mail'


Related: https://www.sfdcpoint.com/salesforce/only-aggregate-expressions-use-field-aliasing-soql-error/
https://developer.salesforce.com/forums/?id=9062I000000IDrCQAW

Hope this helps you. Please mark this answer as best so that others facing the same issue will find this information useful. Thank you
This was selected as the best answer
Princes91221Princes91221
Thank you Swetha it worked for me.
I'm glad you helped. :-)