I inherited a program that I've had to modify and I am stuck on adding conditions to. I have a very long <cfset> statement that is made up of a table and several divs.
<td style="border-bottom: thin solid black"> <div align="left" class="HeadingTwo"> #DollarFormat(Session.WorkOrderReceipt.amount)# #Session.WorkOrderReceipt.Instrument# #Session.WorkOrderReceipt.CheckNumber# </div> </td>
This div shows the dollar amount on the receipt and if it's paid with a check then it shows that and then the check number, but since we've added credit/debit cards it would need to show that information as well. So basically, if #Session.WorkOrderReceipt.instrument eq check# then #Session.WorkOrderReceipt.CheckNumber# would be correct, but if #Session.WorkOrderReceipt.Instrument eq creditDebitCard# then #Session.WorkOrderReceipt.creditDebitNumber# would be correct. There is another div that would basically be the same with the exception of the dollar amount.
I'm not sure how to handle this, and any suggestions would be greatly appreciated.
I'm not sure that you can use a conditional within a string. Never tried it.
However, if you get rid of the CFSET and place your code within <CFSAVECONTENT variable="myVar"> your content </CFSAVECONTENT>, it should work fine with conditionals (if/else or switch/case).
^ _ ^
I hate to say it, but a rewrite would be the best solution. The good news is that you already have the body code written, and it should just be a simple case of removing the CFSET and replace it with the CFSAVECONTENT. Yeah, it's kind of a pain, but better than completely throwing what you have away and starting from scratch. 🙂
^ _ ^
A simple way to do this is use an inline IF, replace the #Session.WorkOrderReceipt.CheckNumber# with the following
#iif(Session.WorkOrderReceipt.Instrument eq "check", de(Session.WorkOrderReceipt.CheckNumber), de(Session.WorkOrderReceipt.creditDebitNumber))#
This assumes that if Instrument is not CHECK then it must be CreditCard. If you expect more values then I would either create a function to return the ID or include a script before the output that sets a variable, like below.
vInstrumentId = "Unknown";
vInstrumentId = session.WorkOrderReceipt.CheckNumber;
vInstrumentId = session.WorkOrderReceipt.creditDebitNumber;
<td style="border-bottom: thin solid black">
<div align="left" class="HeadingTwo">
#DollarFormat(session.WorkOrderReceipt.amount)# #session.WorkOrderReceipt.Instrument# #vInstrumentId#
1) The code #Session.WorkOrderReceipt.Instrument eq creditDebitCard# makes me wonder whether you actually meant #Session.WorkOrderReceipt.Instrument eq 'creditDebitCard'#;
2) In any case, you could improve your code as follows, using the DRY (Don't Repeat Yourself) principle.
<cfset amountInDollars=dollarFormat(Session.WorkOrderReceipt.amount)> <cfswitch expression="#Session.WorkOrderReceipt.instrument#"> <cfcase value="check" > <cfset instrument="check"> <cfset instrumentNumber=Session.WorkOrderReceipt.CheckNumber> <cfbreak> </cfcase> <cfcase value="creditDebitCard" > <cfset instrument="creditDebitCard"> <cfset instrumentNumber=Session.WorkOrderReceipt.creditDebitNumber> <cfbreak> </cfcase> <cfdefaultcase> <!--- The code for the catch-all default payment method (if there is one) ---> </cfdefaultcase> </cfswitch> ... ... <table> ... <td style="border-bottom: thin solid black"> <div align="left" class="HeadingTwo"> #amountInDollars# #instrument# #instrumentNumber# </div> </td> ... </table>