7

I wrote the following trigger to generate a task when an application is set to a certain status. It is creating the task I expect, but it appears to be generating an additional task. Any help will be greatly appreciated.

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.  
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) {
            integer t1 = [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 (t1 == 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;
}

}

  • Can you elaborate on the second task being created? Is it a duplicate? Are you sure there is no other workflow or trigger which is being activated when an Application is activated? You can also try adding a debug statement to see the contents of the followupTasks list just before you insert it. – Richard N Oct 18 '13 at 15:47
  • Yes, I would be interested to know if there is another trigger on the object. Or some workflows that have field updates. If thats the case, you would need to use a static variable to ensure the trigger only fires once per context – Chris Duncombe Oct 18 '13 at 15:58
  • Is it the same exact task being added, a duplicate? ie, your trigger is running and adding the task twice? – Chris Duncombe Oct 18 '13 at 16:21
5

Your trigger needs to operate on the premise that there's a condition of a previous value and a new value. Since this is an "AfterUpdate" trigger, everything you're looking for should be in "trigger.old" and "trigger.new". That your query looks for "count" is a bit confusing to me other than that you're trying to eliminate Application__c's which have no tasks? If so, perhaps you may be querying on the wrong object?

Your tasks are related to Application_c, thus the issue may be that you're not querying Application_c for related tasks. That having been said, I'd expect your trigger to look something more like the following:

for (Application__c appl : trigger.old) {
// use trigger.old because you only want updated Application__c's, so all the Id's will be in Trigger.old

   if (trigger.NewMap.get(appl.Id).Application_Status__c == 'Complete and Ready for Review' && trigger.NewMap.get(appl.Id).Withdrawal_Reason__c == null && (appl.Student__c != null && appl.Counselor_Id__c != null)) {

     ApplicIDs.add(appl.Id); // this gives you a list of the Ids you need to add tasks for

   }

}

Now you can create a for loop for each ApplicID to create a follow-up task for it. Just use the Id to "get" the values from trigger.newmap. You can also query to see if any tasks already exist for these Id's.

This is one solution of many that would be possible.

  • Does this ensure that there isn't already a task on each Application__c? – Chris Duncombe Oct 18 '13 at 16:08
  • You may have missed my edit. – crmprogdev Oct 18 '13 at 16:09
  • Gotcha. I did not see the edit. Might want to throw a snippet in for that – Chris Duncombe Oct 18 '13 at 16:12

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.