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
naresh.sfdc1.3885733215764783E12naresh.sfdc1.3885733215764783E12 

Save XML File in Attachments

Hi Folks,

i implemented one VF Page for showing Data in XML format.Its showing very perfetly but when i click on Save button on this page,i need to save this Data in Attachments as an XML file.Any ideas....

VF Page:

<apex:page showHeader="false" controller="CLS_xmlGeneration" tabStyle="Reward__c" >
                <apex:outputText value="{!xmldata}" style="width:1000px;"/>
</apex:page>

Class :
public class CLS_xmlGeneration {

    public String xmldata { get; set; }
  
    public ID recid{get;set;}
  
    public Reward__c rew{get;set;}
  

  
    Public CLS_xmlGeneration(){
        recid=ApexPages.CurrentPage().getParameters().get('Id');

        rew =new Reward__c();
        rew = [select id,name,Rward_Amount__c,Time_Based_Mgmnt__c,User__c from Reward__c where id=:recid];

        Dom.Document doc = new Dom.Document();
        XmlStreamWriter w = new XmlStreamWriter();
        w.writeStartDocument(null, '1.0');
       // w.writeStartElement(null, 'quotas', null);
     
              w.writeStartElement(null, 'Reward', null);
         
              w.writeStartElement(null, 'Id', null);
              w.writeCharacters(rew.id);
              w.writeEndElement();
     
              w.writeStartElement(null, 'Name', null);
              w.writeCharacters(rew.name);
              w.writeEndElement();

              w.writeStartElement(null, 'UserId', null);
              w.writeCharacters(rew.User__c);
              w.writeEndElement();

              w.writeStartElement(null, 'TimeId', null);
              w.writeCharacters(rew.Time_Based_Mgmnt__c);
              w.writeEndElement();
     
              w.writeStartElement(null, 'RewardAmount', null);
              w.writeCharacters(string.valueof(rew.Rward_Amount__c));
              w.writeEndElement();
     
              w.writeEndElement();
     
       //  w.writeEndElement();
        w.writeEndDocument();
       xmldata  = w.getXmlString();
          w.close();
       // doc.LoadXml(xmlOutput);
       // system.debug('XML is '+doc.toXmlString());
       // xmldata = doc.toXmlString();

    }
    public pagereference insrtattacmnt(){
         PageReference csvPage = Page.VF_XMLinsert_intoAttachment;
         csvPage.getParameters().put('Id', recid);
         Blob csvBlob = csvPage.getContent();
       
       //    dom.Document doc = new dom.Document();
       //    doc.load(csvPage.getContent().toString());
      
         Attachment attachment = new Attachment();
            attachment.Body = csvBlob ;
            attachment.Name = 'Rewardfile.xml';
            attachment.ParentId = rew.Id;
          insert attachment;

        
      return null;
    }
}
Ashish_SFDCAshish_SFDC
This has been addressed in the link below, 

https://developer.salesforce.com/forums/ForumsMain?id=906F00000009Fq0IAE


Regards,
Ashish