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;
}
}