+ Start a Discussion
Timothy SmithTimothy Smith 

Trigger syntax error

Inside the Developer Console, I am receiving the error:
Unexpected Token '<' on Line 2

I believe my syntax defining my List<AggregateResult> is correct.
 
trigger CaseHandlerCountAlert on SOBJECT (after insert) {
    List<AggregateResult> AggregateResultList = [SELECT AccountId, Account.Name name, COUNT(Id) co,  Milestone1_Project__c.id,
                                    Milestone1_Project__c.Implementation_status__c, Milestone1_Project__c.Client_Advisor_Email__c
                                    FROM Case
                                    WHERE CreatedDate = LAST_N_DAYS:5
                                    GROUP BY AccountId, Account.Name
                                    HAVING COUNT(Id)  >= 8
                                    WHERE Id IN :Trigger.New];
                                    

                for(AggregateResult aggr:AggregateResultList){ 
                        system.debug(aggr);

                        Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
                        
                            if(Milestone1_Project__c.Implementation_status__c == 'LIVE - TRANSITION'){    
                                // Set Outgoing Email to Implementation Coordinator
                                //message.toAddresses = new String[] { test@test.com }; 
                            }
                            else if (Milestone1_Project__c.Implementation_status__c == 'Live'){  
                                // Private method *** getAddresses() *** retrieves email address from Customer_Success_Managers Public Group
                                
                                //message.toAddresses = new String[] { "test@test.com" };
                            } 
                        message.setSubject = 'Subject Test Message';
                        message.setPlainTextBody = 'Account name: ' + aggr.get('name') + ' has ' + (Integer)aggr.get('co') + ' cases opened in the last 8 days.';
                        Messaging.SingleEmailMessage[] messages =   new List<Messaging.SingleEmailMessage> {message};
                        Messaging.SendEmailResult[] results = Messaging.sendEmail(messages);
                    System.debug('Account Name: ' + aggr.get('name'));   
                }            
                  
 

private List<String> getAddresses(){
    List<User> UserList =
            [SELECT id, name, email
            FROM User 
            WHERE id 
            IN (SELECT userorgroupid 
                FROM groupmember
                WHERE group.name = 'Customer Success Managers')];

    Set<String> emailString = new Set<String>();

    for(User u: UserList){
        emailstring.add(u.email);
    }   
    return (emailString);
    }    
}

 
RamakantRamakant
Hi,

On your first line of Trigger SOBJECT needs to be replaced by API Name of the object on which you are writing trigger. 

Regards,
Ramakant 
Naveen IlaNaveen Ila
You written the where clause twince in the Query. 

WHERE CreatedDate = LAST_N_DAYS:5 GROUP BY AccountId, Account.Name HAVING COUNT(Id) >= 8 WHERE Id IN :Trigger.New];
 
Deepali KulshresthaDeepali Kulshrestha
Hi Timothy,

You are using two WHERE conditions in your SOQL query
Here is your query:

List<AggregateResult> AggregateResultList = [SELECT AccountId, Account.Name name, COUNT(Id) co,  Milestone1_Project__c.id,
                                    Milestone1_Project__c.Implementation_status__c, Milestone1_Project__c.Client_Advisor_Email__c
                                    FROM Case
                                    WHERE CreatedDate = LAST_N_DAYS:5
                                    GROUP BY AccountId, Account.Name
                                    HAVING COUNT(Id)  >= 8
                                    WHERE Id IN :Trigger.New];

Replace your query with the code:

List<AggregateResult> AggregateResultList = [SELECT AccountId, Account.Name name, COUNT(Id) co,  Milestone1_Project__c.id,
                                    Milestone1_Project__c.Implementation_status__c, Milestone1_Project__c.Client_Advisor_Email__c
                                    FROM Case
                                    WHERE CreatedDate = LAST_N_DAYS:5 AND Id IN :Trigger.New
                                    GROUP BY AccountId, Account.Name
                                    HAVING COUNT(Id)  >= 8];


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com