CodeCharge Studio

Add Code in the After Insert Event to Send Emails

  1. Select the tasks form by selecting it in the Project Explorer, or clicking anywhere within the form's caption.
  2. In the Properties window click on the Events tab.
  3. Select the After Insert event.
  4. Click on the [+] button, then select Add Code...

  5. Once you are in the Code view, replace the generated comment:
    <!---  write your own code here --->

    with the code below:

    <CF_CCToSQL Value="#flduser_id_assign_to#" Type="#ccsInteger#">
    <CF_CCDLookUp Field="email" Table="employees" Where="emp_id=#CCToSQL#" Connection="IntranetDB">
    <CFSET mai1_To=CCDLookUp>
    <CF_CCToSQL Value="#Session.UserID#" Type="#ccsInteger#">
    <CF_CCDLookUp Field="email" Table="employees" Where="emp_id=#CCToSQL#" Connection="IntranetDB">
    <CFSET mai1_From=CCDLookUp>
    <CF_CCDLookUp Field="max(task_id)" Table="tasks" Where="user_id_assign_by=#CCToSQL#" Connection="IntranetDB">
    <CFMAIL TO="#mai1_To#" FROM="#mai1_From#" SUBJECT="New task for you" TYPE="HTML" >
      The following task was submitted:<br><br>
      Task ID:#CCDLookUp#
      <br><br>#fldtask_desc#
    </CFMAIL>

    The following is the explanation of the above code:

    <CF_CCToSQL Value="#flduser_id_assign_to#" Type="#ccsInteger#">
    <CF_CCDLookUp Field="email" Table="employees" Where="emp_id=#CCToSQL#" Connection="IntranetDB">
    <CFSET mai1_To= CCDLookUp>

    Sets the To email address to the email of the person that is assigned to the task. The CCDLookUp custom tag is used here to retrieve the appropriate email address.

    <CF_CCToSQL Value="#Session.UserID#" Type="#ccsInteger#">
    <CF_CCDLookUp Field= "email" Table= "employees" Where= "emp_id=#CCToSQL#" Connection=quot;IntranetDB">
    <CFSET mai1_From= CCDLookUp>

    Sets the From email address to the value of the email field in the employees table where emp_id matches the current user. The CCDLookUp function is used to retrieve a database value, while Session.UserID contains the id of the currently logged in user.

    <CF_CCDLookUp Field="max(task_id)" Table="tasks" Where="user_id_assign_by=#CCToSQL#" Connection= "IntranetDB">

    Retrieves the current Task Id. The last inserted task id can be obtained using different methods with different databases. Unfortunately, MS Access doesn't support the retrieval of the last inserted record, therefore you will need to use the CCDLookUp function to retrieve the largest task id submitted by the current user (assuming that task ids are created incrementally).

    <CFMAIL TO="#mai1_To#" FROM="#mai1_From#" SUBJECT="New task for you" TYPE="HTML" >
      The following task was submitted:<br><br>
      Task ID:#CCDLookUp#
      <br><br>#fldtask_desc#
    </CFMAIL>

    The CFMail tag sends out an email address using the variables set within the tag. The body of the tag makes up the body of the email address that will be sent.


Next: Use the "After Update" Event to Send Emails


On-line, printable versions and updates