Skip to main content
February 3, 2010
Question

emailing reports

  • February 3, 2010
  • 1 reply
  • 998 views

I have this and all works good excep the report only gets generated once for the last person in the DB with the emails. I want to loop over the output so each person from the email db get a seperate (personal) report. There are 2 people in my test email DB and we both get the same report even though we have different SalesNumbers. Here is basicaly what im doing:

<cfparam name="SupComp" default="">
<cfparam name="Employee" default="">

<cfquery datasource="Emails" name="GetUsers">
  SELECT DISTINCT First, Last, EMail, SalesNumber
  FROM test
</cfquery>

<cfloop query="GetUsers">
<CFQUERY NAME="NatActiv" DATASOURCE="NationalAccounts">
SELECT PGMJ5W, PGMJDE

FROM ActiveBulletin
    WHERE XXOPER = '#SalesNumber#' 
ORDER BY CCUSTN, ProgramType, Item
</CFQUERY>
</cfloop>


<cfdocument name="NationalAccounts" format="pdf" marginbottom=".5" marginleft=".5" marginright=".2" margintop=".4">
-------output from NatActiv Query above-----------

</cfdocument>


<cfquery datasource="Emails" name="GetUsers">
  SELECT First, Last, EMail
  FROM TEST
</cfquery>

<cfmail to="#EMail#"
from="gwsmith@youngsmarket.com"
subject="National Account Bulletin"
type="HTML"
query="GetUsers">
    Attached is your National Accounts Bulletin.

<cfmailparam
file="NationalAccountsBulletin.pdf"
type="application/pdf"
content="#NationalAccounts#"
/>

</cfmail>

Reports have been sent.

anyone elso doing this?

Thanks

George

    This topic has been closed for replies.

    1 reply

    Inspiring
    February 3, 2010

    For your specific question, instead of having your cfmail tag inside a loop, use the query attribute.

    On a more general note, your code is very inefficient.  You should be able to get all the data you need in one query.

    February 8, 2010

    Ok I have changed what I have but still cannot get 2 different reports to get sent. In my Email db there are 2 addresses and we each have a different SalesNumber. When I run my code the report is generated and sent to both of us, but its that same report, im looking for each of us to get there own report.

    <cfparam name="SupComp" default="">
    <cfparam name="Employee" default="">

    <cfquery datasource="HR" name="GetUsers">
      SELECT DISTINCT First, Last, EMail, SalesNumber
      FROM test
    </cfquery>

    <cfloop query="GetUsers">
    <CFQUERY NAME="NatActiv" DATASOURCE="NA">
    SELECT PGMJ5W, PGMJDE, RTRIM(ItemID) AS ItemID, PDES5W AS Item, RTRIM(ProgramType) AS ProgramType, Status, Priority, Descrip,  StartDate, EndDate, RTRIM(CUSN05) AS CUSN05, RTRIM(DSEQ05) AS DSEQ05, RTRIM(MGRDVP) AS MGRDVP, RTRIM(CAD105) AS CCUSTN, RTRIM(XXOPER) AS SLMN20, CASTSDTY, LVALSDTY, CASTSDLY, LVALSDLY, CAD405, PCD105, STTX20, PHON05, SLM4SD, SLM5SD, Driven, Goal, CAD105, GroupCode, PDES5W, COMCDE, WVAR5W, WAPP5W
    FROM ActiveBulletin
        WHERE XXOPER = '#SalesNumber#'
    OR SLM4SD = '#SalesNumber#'
    OR SLM5SD = '#SalesNumber#'

    ORDER BY CCUSTN, ProgramType, Item
    </CFQUERY>
    </cfloop>


    <cfdocument name="NationalAccounts" format="pdf" marginbottom=".5" marginleft=".5" marginright=".2" margintop=".4">
    <head>
    <title>National Accounts Activite Bulletin</title>
    </head>
    <body>

    <cfoutput query="NatActiv" maxrows="1">
    <cfdocumentitem type="header">
    <table width="100%" bgcolor="999999">
    <tr>
      <td style="font-family:Arial, Helvetica, sans-serif; font-size:8px;">Sales Rep:#SLMN20#</td>
      <td style="font-family:Arial, Helvetica, sans-serif; font-size:10px; font-weight:bold" align="center">National Accounts Actitvities Bulletin</td>
      <td style="font-family:Arial, Helvetica, sans-serif; font-size:8px;" align="right">#DateFormat(Now(),'mm/dd/yyyy')#</td>
    </tr>
    </table>
    </cfdocumentitem>
    </cfoutput>

    <cfoutput query="NatActiv" group="CCUSTN">
    <table width="100%" bgcolor="CCCCCC">
    <tr>
      <td style="font-family:Arial, Helvetica, sans-serif; font-size:9px;">#CAD105# #CUSN05#-#DSEQ05#</td>
      <td style="font-family:Arial, Helvetica, sans-serif; font-size:9px;">#CAD405# #PCD105# #STTX20#</td>
      <td style="font-family:Arial, Helvetica, sans-serif; font-size:9px;" align="right"><cfif #TRIM(SLM5SD)# IS NOT "">Key:    #SLM5SD#</cfif></td>
    </tr>
    <tr>
      <td style="font-family:Arial, Helvetica, sans-serif; font-size:9px;">Group: #GroupCode#</td>
      <td style="font-family:Arial, Helvetica, sans-serif; font-size:9px;">Acc. Manager: #MGRDVP#</td>
      <td style="font-family:Arial, Helvetica, sans-serif; font-size:9px;" align="right"><cfif #TRIM(SLM4SD)# IS NOT "">Estate:    #SLM4SD#</cfif></td>
    </tr>
    </table>
    <cfoutput group="ProgramType">
    <table width="100%" bgcolor="C7C7C7">
    <tr>
      <td style="font-family:Arial, Helvetica, sans-serif; font-size:9px; font-weight:bold">#ProgramType#</td>
    </tr>
    </table>
    <cfoutput group="ItemID">
    <table width="100%" bgcolor="F2F2F2">
    <tr>
      <td style="font-family:Arial, Helvetica, sans-serif; font-size:8px; font-weight:bold" width="30%">#TRIM(PDES5W)#</td>
      <td style="font-family:Arial, Helvetica, sans-serif; font-size:8px; font-weight:bold" width="10%">#ItemID#</td>
      <td style="font-family:Arial, Helvetica, sans-serif; font-size:8px; font-weight:bold" width="30%">#TRIM(COMCDE)#</td>
      <td style="font-family:Arial, Helvetica, sans-serif; font-size:8px; font-weight:bold" width="15%">#TRIM(WVAR5W)#</td>
      <td style="font-family:Arial, Helvetica, sans-serif; font-size:8px; font-weight:bold" width="15%">#TRIM(WAPP5W)#</td>
      </tr>
    </table>
    <table cellpadding="0" cellspacing="0" width="100%">
    <tr>
      <td style="font-family:Arial, Helvetica, sans-serif; font-size:8px; color:003399" width="10%" align="center">Status</td>
      <td style="font-family:Arial, Helvetica, sans-serif; font-size:8px; color:003399" width="10%" align="center">Priority</td>
      <td style="font-family:Arial, Helvetica, sans-serif; font-size:8px; color:003399" width="15%" align="center">Type</td>
      <td style="font-family:Arial, Helvetica, sans-serif; font-size:8px; color:003399" width="15%" align="center">Driven</td>
      <td style="font-family:Arial, Helvetica, sans-serif; font-size:8px; color:003399" width="13%" align="center">Start</td>
      <td style="font-family:Arial, Helvetica, sans-serif; font-size:8px; color:003399" width="13%" align="center">End</td>
      <td style="font-family:Arial, Helvetica, sans-serif; font-size:8px; color:003399" width="6%" align="center">Cases TY</td>
      <td style="font-family:Arial, Helvetica, sans-serif; font-size:8px; color:003399" width="6%" align="center">Cases LY</td>
      <td style="font-family:Arial, Helvetica, sans-serif; font-size:8px; color:003399" width="6%" align="center">Dollars TY</td>
      <td style="font-family:Arial, Helvetica, sans-serif; font-size:8px; color:003399" width="6%" align="center">Dollars LY</td>
    </tr>
    <tr bgcolor="D3FED6" valign="middle">
      <td style="font-family:Arial, Helvetica, sans-serif; font-size:8px;" width="10%">   #Status#</td>
      <td style="font-family:Arial, Helvetica, sans-serif; font-size:8px;" width="10%" align="center">#Priority#</td>
      <td style="font-family:Arial, Helvetica, sans-serif; font-size:8px;" width="15%" align="center">#ProgramType#</td>
      <td style="font-family:Arial, Helvetica, sans-serif; font-size:8px;" width="15%" align="center">#Driven#</td>
      <td style="font-family:Arial, Helvetica, sans-serif; font-size:8px;" width="13%" align="center">#StartDate#</td>
      <td style="font-family:Arial, Helvetica, sans-serif; font-size:8px;" width="13%" align="center">#EndDate#</td>
      <td style="font-family:Arial, Helvetica, sans-serif; font-size:8px;" width="6%" align="center">#CASTSDTY#</td>
      <td style="font-family:Arial, Helvetica, sans-serif; font-size:8px;" width="6%" align="center">#DecimalFormat(CASTSDLY)#</td>
      <td style="font-family:Arial, Helvetica, sans-serif; font-size:8px;" width="6%" align="center">#LVALSDTY#</td>
      <td style="font-family:Arial, Helvetica, sans-serif; font-size:8px;" width="6%" align="center">#LVALSDLY#</td>
    </tr>
    <tr bgcolor="D3FED6" valign="middle">
      <td style="font-family:Arial, Helvetica, sans-serif; font-size:8px;" colspan="10">   #Descrip#</td>
    </tr>
    </table>
    </cfoutput>
    </cfoutput>
    <cfdocumentitem type="footer">
    <table width="100%" cellpadding="0" cellspacing="0" align="center">
    <tr>
    <td align="left" width="34%" style="font-family:Arial, Helvetica, sans-serif; font-size:8px;">#DateFormat(Now(),'mm/dd/yyyy')#</td>
    <td align="center" width="33%" style="font-family:Arial, Helvetica, sans-serif; font-size:8px;"></td>
    <td align="right" width="33%" style="font-family:Arial, Helvetica, sans-serif; font-size:8px;">#CFDOCUMENT.currentpagenumber#</td>
    </tr>
    </table>
    </cfdocumentitem>

    </cfoutput>
    </body></html>
    </cfdocument>


    <cfmail to="#EMail#"
    from="gwsmith@youngsmarket.com"
    subject="National Account Bulletin"
    type="HTML"
    query="GetUsers">
        Attached are the PDF reports that have been requested for you.

    <cfmailparam
    file="NationalAccountsBulletin.pdf"
    type="application/pdf"
    content="#NationalAccounts#"/>

    </cfmail>

    Reports have been sent.

    Inspiring
    February 8, 2010

    At the end of the day, you need a query that matches a report to a recipient.  I suggest this approach.

    1 - In your initial query, add  " 'some big constant' as reportfile" to your select clause.

    2 - When you are looping through the results and creating the report, use querysetcell() to update the reportfile column in your query.

    3 - Use the value of reportfile in your cfmailparam tag.