CodeCharge Studio

Use the After Update Event to Send Emails

You previously added the necessary code that sends email notification to the assignee upon recording a new task in the system. We shall now implement similar functionality in After Update Event to notify the assignee when an existing task is updated and reassigned to someone. <
  1. Click on the tasks form in the Project Explorer.
  2. In the Properties window select the Events tab.
  3. Add the following Custom Code in After Update event:
    $DBIntranetDB = clsDBIntranetDB->new();
    
    if (CCGetSession("UserID") != $tasks->{user_id_assign_to}->GetValue())
    {
      $from_name = CCDlookUp("emp_name", "employees", "emp_id=" . 
                  $DBIntranetDB->ToSQL(CCGetSession("UserID"), $ccsInteger), $DBIntranetDB);
      $from_email = CCDlookUp("email", "employees", "emp_id=" . 
                  $DBIntranetDB->ToSQL(CCGetSession("UserID"), $ccsInteger), $DBIntranetDB);
      $to_email= CCDlookUp("email", "employees", "emp_id=" . 
                  $DBIntranetDB->ToSQL($tasks->{user_id_assign_to}->GetValue(), $ccsInteger), $DBIntranetDB); 
      $subject = "A task was assigned to you";
      $message = "The following task was assigned to you:<br><br>" . 
                  "Task ID: " .CCGetFromGet("task_id","") ."<br><br>" .
                  $tasks->{task_desc}->GetValue();
    
      if (open(SENDMAIL, "| sendmail $from_name")) 
      {
        print SENDMAIL "From: $from_email\nTo: $to_email\nSubject: $subject\n\n$message\n.\n";  
        close(SENDMAIL);
      }
    }

    The main differences between the above code and that which was used in the After Insert event are as follows:

    1. An if condition was added to send an email only if a user assigns a task to someone other than himself/herself
    2. task_id is retrieved from the URL using the CCGetFromGet function. We can use this method because tasks can be updated only if the user arrived at the current page via a URL that contains the task id to be updated. Such a URL would look like this:
      http://localhost/TaskManager/tasks_maint.cgi?task_id=9

Next: Test Email Delivery


On-line, printable versions and updates