CF - SQL sending email from query problem

I have a program where it calculates the monthly bonus for employees in each dept for the previous month.  It is then sent in a email to payroll.  It works fine except it sends an email for each employee or record - they want all the employees from one dept in one email - listing the employees instead of one email for each.  Is there a way to get all the records to send in the same email?


Query
SELECT     monthlybonus.bonus_amt AS TotalBonus, monthlybonus.paybonus, monthlybonus.has_sig1, monthlybonus.what_depts, employee_data.email_name, 
                      monthlybonus.monthdate, employee_data.first_name, employee_data.last_name, monthlybonus.employeeID, employee_data.ID, monthlybonus.whosthesig1, 
                      employee_data_1.ID AS Expr1, employee_data_1.email_name AS LCemail_name
FROM         monthlybonus INNER JOIN
                      employee_data ON monthlybonus.employeeID = employee_data.ID INNER JOIN
                      employee_data AS employee_data_1 ON monthlybonus.whosthesig1 = employee_data_1.ID
WHERE     (monthlybonus.whosthesig1 = '#sessionID#') AND (monthlybonus.has_sig1 = 1) AND (monthlybonus.paybonus = 1) AND 
                      (monthlybonus.monthdate = CASE WHEN month(getDate()) = 12 THEN 1 ELSE month(getDate()) - 1 END)

Open in new window


Email form
<CFMAIL query="getSubmitted"
            to="email@company.com"
            from="#getSubmitted.LCemail_name#@company.com"
            spoolenable="no"
            type="html"
            Subject="TEAM PERFORMANCE AWARDS EMAIL">
    <table border="0" cellpadding="0" cellspacing="0" style="width:100%; font-family:Arial, Helvetica, sans-serif;">
      <tr>
        <td><p>Dear Payroll,
          <p>Submission :#DateFormat(Now(), "mmm-dd-yyyy")#
          <p>BONUS AMOUNT = #NumberFormat("#TotalBonus#",'$____.__')# - #what_depts# for the month of #DateFormat(DateAdd('m', -1, MyDateTime),'mmmm')# 
          For #first_name# 	#last_name#<br>

          <p></td>
      </tr>
    </table>
  </CFMAIL>

Open in new window

JohnMac328Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

_agx_Commented:
> <CFMAIL query="getSubmitted"

By default that generates one email per query record.  Try adding the "group" attribute. It should work like <cfoutput query="..." group="...">

Be sure to sort the query results by DEPT first

     
      SELECT ....
      WHERE     (monthlybonus.whosthesig1 = '#sessionID#') ....
      ORDER BY monthlybonus.what_depts    <=== sort by department FIRST


Then "group" by the department column. Inside the email tag, use cfoutput tags to display the individual employees.  Something like this.  

         <CFMAIL to="useATestEmailOnly@company.com" query="getSubmitted" group="what_depts" ....>

           <!--- this should only print once per dept --->
           Dear Payroll,
          <p>Submission :#DateFormat(Now(), "mmm-dd-yyyy")#

          <!--- this should print all employee records within the current department --->    
          <cfoutput>
    <p>BONUS AMOUNT = #NumberFormat("#TotalBonus#",'$____.__')# - #what_depts# for the month of #DateFormat(DateAdd('m', -1, MyDateTime),'mmmm')# 
          For #first_name# 	#last_name#<br>
           </cfoutput>

        </CFMAIL>

Open in new window

0
JohnMac328Author Commented:
Hi agx,

I added the grouping and the order by whats_depts in the query but only one email with one record goes out.

       Subject="TEAM PERFORMANCE AWARDS EMAIL"
            group="what_depts"
0
JohnMac328Author Commented:
I threw in a cfloop to see what would happen and it worked!

 <cfloop query="getSubmitted">

    <table border="0" cellpadding="0" cellspacing="0" style="width:100%; font-family:Arial, Helvetica, sans-serif;">
      <tr>
        <td><p>Dear Payroll,
          <p>Submission :#DateFormat(Now(), "mmm-dd-yyyy")#
          <p>BONUS AMOUNT = #NumberFormat("#TotalBonus#",'$____.__')# - #what_depts# for the month of #DateFormat(DateAdd('m', -1, MyDateTime),'mmmm')#
          For #first_name#       #last_name#<br>

          <p></td>
      </tr>
    </table>
    </cfloop>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

_agx_Commented:
Sorry, I got called away.


>> only one email with one record goes out.

I'm guessing you forgot to add the extra <cfoutput> tags inside the cfmail tag :)


 <CFMAIL to="useATestEmailOnly@company.com" query="getSubmitted" group="what_depts" ....>

           <!--- this should only print once per dept --->
           Dear Payroll,
          <p>Submission :#DateFormat(Now(), "mmm-dd-yyyy")#

          <!--- this should print all employee records within the current department --->    
          <cfoutput>
    <p>BONUS AMOUNT = #NumberFormat("#TotalBonus#",'$____.__')# - #what_depts# for the month of #DateFormat(DateAdd('m', -1, MyDateTime),'mmmm')#
          For #first_name#       #last_name#<br>
           </cfoutput>

        </CFMAIL>
0
_agx_Commented:
EDIT:

I threw in a cfloop to see what would happen and it worked!

Having said that, personally, I'd go with what you came up with instead. I think cfloop is cleaner :)  (I was going to suggest a loop, but was not sure if you wanted one email per dept - or one overall.)

Don't forget to mark your comment as the answer to close out the thread.
0
JohnMac328Author Commented:
I liked your tip also :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ColdFusion Language

From novice to tech pro — start learning today.