coldfusioncfmlcfloopcfoutput

cfoutput in cfset tag


I need to use a cfoutput in a cfset tag to get multiple line items to send in xml. I know it's wrong to put a CF tag into another, but I'm looking for the best way to do this. Thanks in advance.

<cfset soapBody = soapBody & "
    <cfoutput query="getitems">                        
    <item id=""#id#"">
        <unitPrice>#getitems.unitprice#</unitPrice>
        <quantity>#getitems.quantity#</quantity>
        <productname>#getitems.productname#</productname>
        <taxAmount>#getitems.taxamount#</taxAmount>
        <unitOfMeasure>#getitems.unitofmeasure#</unitOfMeasure>
        <taxRate>#getitems.taxrate#</taxRate>
        <totalAmount>#getitems.totalamount#</totalAmount>
        <grossNetIndicator>#getitems.grossnetindicator#</grossNetIndicator>
    </item>
    </cfoutput>         
">

Solution

  • You can use <cfsavecontent> tag for this. It is very use full the cases you are trying to create a big piece of string dynamically.

    <cfsavecontent variable="soapBody">
        <cfoutput>#soapBody#</cfoutput>
        <cfoutput query="getitems">                        
        <item id=""#id#"">
            <unitPrice>#getitems.unitprice#</unitPrice>
            <quantity>#getitems.quantity#</quantity>
            <productname>#getitems.productname#</productname>
            <taxAmount>#getitems.taxamount#</taxAmount>
            <unitOfMeasure>#getitems.unitofmeasure#</unitOfMeasure>
            <taxRate>#getitems.taxrate#</taxRate>
            <totalAmount>#getitems.totalamount#</totalAmount>
            <grossNetIndicator>#getitems.grossnetindicator#</grossNetIndicator>
        </item>
        </cfoutput>         
    </cfsavecontent>