3

I am working on a trigger to create a task, but only if there is not an active task already assigned. The following is the logic that I think should work, but Apex does not like the select statement. Any help would be greatly appreciated. Here is the trigger:

trigger RecruiterAppReview on Application__c (after update) {
    /****************
     * When an application status is changed to 'complete and ready for review' and the 
     * application has not been withdrawn, a task needs to be created for the recruiter 
     * to review the application. 
     ****************/ 

    Number numTasks;
    List<Task> followupTasks = new List<Task>();
    // Generate a list of tasks to be generated
    for (Application__c appl : System.Trigger.new) {
        if (appl.Application_Status__c == 'Complete and Ready for Review' && appl.Withdrawal_Reason__c == null) {
            if (appl.Student__c != null && appl.Counselor_Id__c != null) {
                numTasks = [select count(*) from Task where Status not in ('Completed') and Subject = 'Application Review' and WhoId = appl.Student__c and OwnerId = appl.Counselor_Id__c] 
                if (numTasks is null || numTasks > 0) {
                    Task tasks = new Task(
                        WhoId = appl.Student__c,
                        OwnerId = appl.Counselor_Id__c,
                        Description = 'Review Application',
                        Priority = 'High',
                        ReminderDateTime = System.now().addDays(3),
                        Status = 'Not Started',
                        Subject = 'Application Review');

                    followupTasks.add(tasks);
                }
            }
        }
    }
    // insert the entire list
    if (followupTasks.size() > 0) {
        insert followupTasks;
    }
}
6

You can't use * in SOQL. Change that to Id (because you always have an Id) and SOQL should pass if there are no further errors. Also, you don't need to use an aggregate function. This is not SQL and "normal" logic doesn't apply. Any SOQL that uses aggregate functions will return a List<AggregateResult>. Any other SOQL will return List<SObject> where SObject is essentially what you state in the from cause.

Your SOQL could simply be List<Task> tasks = [select Id from Task ...]; Then check if tasks is empty.

In any case, this code has many other syntax errors.

  • I like your suggestion of using the list, but I still can't get by the problem of using appl.Student__c and appl.Counselor_Id__c. What would be my alternative for that? – user2569499 Oct 10 '13 at 13:54
  • 1
    That's an additional problem. When using APEX variables in SOQL, you need to prefix them with a colon ("where field = : variable"). But as I said, there are further errors in the sample code. You should really take it piece by piece. For example, take that SOQL and make sure it works, then go on. – zdropic Oct 10 '13 at 14:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.