Skip to main content
Participating Frequently
March 10, 2011
Question

<cfif> or <cfswitch> no idea

  • March 10, 2011
  • 1 reply
  • 610 views
I do not know which or how to do this. Have lengthy <cfquery> for MS SQL. Have <cfmail> tag to direct results of query to appropriate recipients.

Here's query:

<cfquery datasource="#Application.DSN2#" name="allinone">
SELECT
        USER_PIN,
        USER_LNM + ', ' + USER_FNM AS FullName,
        TEAM_ABBR,
        BRNCH_ABBR,
        POSN_ABBR,
        USER_EMAIL,
        (
            SELECT E1.USER_EMAIL
            FROM USER_VIEW_3 AS E1
            WHERE E1.TEAM_ABBR = USER_VIEW_3.TEAM_ABBR
            AND E1.POSN_ABBR = 'TL'
        ) AS TL_EMAIL,
        (
            SELECT E3.POSN_ABBR
            FROM USER_VIEW_3 AS E3
            WHERE E3.TEAM_ABBR = USER_VIEW_3.TEAM_ABBR
            AND E3.POSN_ABBR = 'TL'
        ) AS TL_POSITION,
        (
            SELECT E2.USER_EMAIL
            FROM USER_VIEW_3 AS E2
            WHERE E2.BRNCH_ABBR = USER_VIEW_3.BRNCH_ABBR
            AND E2.POSN_ABBR = 'BC'
        ) AS BC_EMAIL,       
        (
            SELECT E4.POSN_ABBR
            FROM USER_VIEW_3 AS E4
            WHERE E4.BRNCH_ABBR = USER_VIEW_3.BRNCH_ABBR
            AND E4.POSN_ABBR = 'BC'
        ) AS BC_POSITION,
        (
        SELECT E5.POSN_ABBR
            FROM USER_VIEW_3 AS E5
            WHERE E5.POSN_ABBR = 'DD'
        ) AS DD_POSITION,
        (
        SELECT E6.USER_EMAIL
            FROM USER_VIEW_3 AS E6
            WHERE E6.POSN_ABBR = 'DD'
        ) AS DD_EMAIL
    FROM USER_VIEW_3
    WHERE USER_PIN = '#Form.EMPPIN#'
</cfquery>

And here's <cfmail>

<cfif cgi.SERVER_NAME is not "localhost">             
           
      <cfmail to="#allinone.TL_EMAIL#"
                cc="#allinone.BC_EMAIL#"
          from="#session.user.email#"
          subject="Request for Additional Work Hours" 
                       type="html">
              <html><body>
      <font size="+2">#EmpName# submitted a request for 
                      #NoOfHours1# hours of #TypeHrs1# for 
                      #startdate1#.
              <br /><br />
       Please click the link below to view pending requests.
              <br /><br />
  <a href="http://xxxxxxxxxxxxxxxx.com/Reply.cfm">Pending Requests</a> </font></body></html>
        </cfmail>       
  </cfif>

How do I start? The session user information is captured. The session user could be level 1; 2; 3; 4; 5; or, 6. If the session user is level 1 then for email
to = level 4
cc = level 5
For level 2
to = level 6
no cc
For level 3
to = level 6
no cc
For level 4
to = level 5
no cc
For level 5
to = level 6
no cc

Any suggestions? Thank you - JS
    This topic has been closed for replies.

    1 reply

    Inspiring
    March 10, 2011

    It is not clear from your description where the email addresses are supposed to come from based on the level.


    Note that this code is not tested.

    You could do something like:

    <!--- default values --->
    <cfset emailTo="">
    <cfset emailCc="">

    <cfswitch expression="#session.level#">

        <cfcase value="1">
            <cfset emailTo="#queryname.emailToField#">
            <cfset emailCc="#queryname.emailCCField#">
        </cfcase>

        <cfcase value="2">
            <cfset emailTo="#queryname.emailToField#">
            <cfset emailCc="#queryname.emailCCField#">
        </cfcase>

        <!--- etc. --->

    </cfswitch>

    <cfmail to="#emailTo#" cc="#emailCc#"><!--- mail body here ---></cfmail>


    You might also consider moving this logic into a CFC with a function that takes the session and empPin and returns the to and cc addresses.  If the logic is in a CFC it should be easier to maintain your code if and when the business logic changes.

    Alternately you could wrap this logic in SQL server stored procedure or user defined function that will return the appropriate email addresses.