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
Vijaya Kumar RegantiVijaya Kumar Reganti 

XML File Download through a button click

Hi Friends,


I have created preparing the XML content in my Apex class and getting into a string and used "contentType" along with <apex:outputText> on the VF page to download it as an .xml file.It is working fine.


But what I want to do is, I need to create multiple XML files ,need to download into different files and then attached to to the attachments to a particular record.


Please suggest a way to do this. 

Create a PageReference to the page that generates the XML, obtain its content, then save it as an attachment.

Attachment[] atts = new Attachment[0];
PageReference ref = Page.xmlExport;  // (1)
ref.getParameters().put('id', recordId);  // (2)
atts.add(new Attachment(ParentId=record.Id, name='', contenttype='text/xml', body=ref.getContent())); // (3)
insert atts; (4)

For multiple attachments, just repeat the steps for (1) assigning the page reference, (2) adding any parameters, (3) creating the attachment in memory for each page you need to obtain the contents of, and finally (4) inserting all attachments as a list.
Swamy P R NSwamy P R N
Hi buddys,

I have a VF page like the below one.Now when i am going to click on "OK" Button,i need to download that XML file and  i need to insert that XML file in related Attachments.Any idea...

<apex:page showHeader="false" controller="CLS_xmlGeneration" tabStyle="Reward__c">
    <apex:form >
        <apex:pageBlock mode="edit">
            <apex:pageBlockSection collapsible="false">
                <apex:outputText value="{!xmldata}" style="width:1000px;"></apex:outputText>
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton value="Back" onclick=";"/>
                <apex:commandButton value="OK" onclick="'/{!recid}?export=1&enc=UTF-8&xf=csv');"/>

<?xml version="1.0"?><Reward><Id>a04i000000AhEc8AAF</Id><Name>R-0001</Name><UserId>005i0000002hsAbAAI</UserId><TimeId>a01i000000AoSiWAAV</TimeId><RewardAmount>500</RewardAmount></Reward>