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:
    global $DBIntranetDB;
    global $tasks;
    if (CCGetUserID() != $tasks->user_id_assign_to->GetValue())
    {
      $from_name = CCDLookUp("emp_name", "employees", "emp_id=".
                  $DBIntranetDB->ToSQL(CCGetUserID(), ccsInteger), $DBIntranetDB);
      $from_email = CCDLookUp("email", "employees", "emp_id=".
                  $DBIntranetDB->ToSQL(CCGetUserID(), ccsInteger), $DBIntranetDB);
      $to_email = CCDLookUp("email", "employees", "emp_id=".
                  $DBIntranetDB->ToSQL($tasks->user_id_assign_to->GetValue(),ccsInteger), $DBIntranetDB);
    
      $headers = "From: ".$from_name."<".$from_email.">;\r\n";
      $headers .= "Content-Type: text/html;";
      $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->GetText();
      mail($to_email, $subject, $message, $headers);
    }

    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.php?task_id=9

Next: Test Email Delivery


On-line, printable versions and updates