function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
@jpmaria@jpmaria 

how to represent & in messaging:attachment in vf page email template attachment?

for classic email template using VF page - & can be displayed in messaging:htmlEmailBody using & but in same template messaging:attachment does not support & or & . for a csv attachment when placing & in messaging:attachment  - it shows error, and when placing &  it comes as '&' itself
Best Answer chosen by @jpmaria
@jpmaria@jpmaria

with what Mayank Tolambia @ Salesforce Technical Support has suggested <apex:outputText value="&" escape="false"/> works well for getting & without amp; in csv also

, for the sample code I've provided before, the following modification gives & as expected in csv attachment
 

<messaging:emailTemplate subject="Sample Subject" recipientType="Contact" relatedToType="Opportunity">
<messaging:htmlEmailBody >
<html>
            <body>
           Sample Content with &amp; that works as expected. 
            Thanks,<br/>
            {!$User.firstName}&nbsp;{!$User.LastName}<br/>
            {!$User.Email}<br/>
            {!$User.Phone}<br/>
                      
            </body>
        </html>
</messaging:htmlEmailBody>
<messaging:attachment filename="Sample.csv"><apex:outputText value=" Column Header with &" escape="false"/>
</messaging:attachment>
</messaging:emailTemplate>

All Answers

SwethaSwetha (Salesforce Developers) 
HI ,
Can you share a reproducible code snippet so that I can try this behaviour in my org to suggest? Thanks

Checked:https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_messaging_attachment.htm
@jpmaria@jpmaria

<messaging:emailTemplate subject="Sample Subject" recipientType="Contact" relatedToType="Opportunity">
<messaging:htmlEmailBody >
<html>
            <body>
           Sample Content with &amp; that works as expected. 
            Thanks,<br/>
            {!$User.firstName}&nbsp;{!$User.LastName}<br/>
            {!$User.Email}<br/>
            {!$User.Phone}<br/>
                      
            </body>
        </html>
</messaging:htmlEmailBody>
<messaging:attachment filename="Sample.csv">Column Header with &amp;
</messaging:attachment>
</messaging:emailTemplate>

thanks Swetha

Kindly try this code, while testing via Send Test and verify merge fields, or sending from Compose Email to a contact, the '&amp;' inside messaging:htmlEmailBody comes as '&' but '&amp;' inside messaging:attachment comes as '&amp;' itself

this behavior can be observed by downloading the csv that is sent as an attachment with the mail
 

@jpmaria@jpmaria

that is, in mail, '&amp;' is coming as &, but in the attached csv file, '&amp;' is coming as '&amp;'

what can be done to bring '&" in csv file attachment
 

@jpmaria@jpmaria

with what Mayank Tolambia @ Salesforce Technical Support has suggested <apex:outputText value="&" escape="false"/> works well for getting & without amp; in csv also

, for the sample code I've provided before, the following modification gives & as expected in csv attachment
 

<messaging:emailTemplate subject="Sample Subject" recipientType="Contact" relatedToType="Opportunity">
<messaging:htmlEmailBody >
<html>
            <body>
           Sample Content with &amp; that works as expected. 
            Thanks,<br/>
            {!$User.firstName}&nbsp;{!$User.LastName}<br/>
            {!$User.Email}<br/>
            {!$User.Phone}<br/>
                      
            </body>
        </html>
</messaging:htmlEmailBody>
<messaging:attachment filename="Sample.csv"><apex:outputText value=" Column Header with &" escape="false"/>
</messaging:attachment>
</messaging:emailTemplate>

This was selected as the best answer