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
Sfdc_beginnerSfdc_beginner 

A Custom apex class to process a xml and then create records in salesforce

Hi,

 

I have a requirement where i need to write a custom apex class to process a xml and then create records in salesforce.

 

One more thing, is there any way we can process a xml located in a local drive and then create records in Salesforce.

 

Please share few examples.

 

Thanks

SFDC Beginner

Best Answer chosen by Admin (Salesforce Developers) 
zachbarkleyzachbarkley

If you do not have a folder/drive available to the web on your local machince, you would have to have your local machine upload this as a document.

 

Then i would create a trigger, so as the file is saved, say in X folder, have it process then, or use a batch job to process all unprocessed files periodically yes.

All Answers

zachbarkleyzachbarkley

Hi SFDC Beginner,

 

You would create a Visualforce page that would read a file that you upload. It's hard to explain exactly what you need as the XML file will have difference nodes. Use the XmlStreamReader to read your file and there are some examples in this discussion

 

Here is an exmaple how to get your XML File into a string so you can read it.

 

VF Page

 

<apex:page id="VFPage" controller="MyCustomController">
  <apex:form id="VFForm">
    <apex:inputFile value="{!MyFileVal}" id="MyFile" />
    <br/>
    <apex:commandButton value="Process File" action="{!ProcessFile}"/>
  </apex:form> 
</apex:page>

 APEX Class

 

public class MyCustomController {
   public Blob MyFileVal{get;set;}
   public MyCustomController () {
	
   }
   public PageReference ProcessFile() {
       
       if(MyFileVal==null){
          ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL, 'No File Chosen'));
          return null;
        }
        
        String XMLFileString = MyFileVal.ToString();

        // NOW PROCESS YOUR XML FILE Using XMLFileString


return null; } }

 

Sfdc_beginnerSfdc_beginner

Thanks 

Thanks in advance!

 

Thanks

SFDC Beginner

zachbarkleyzachbarkley

If you do not have a folder/drive available to the web on your local machince, you would have to have your local machine upload this as a document.

 

Then i would create a trigger, so as the file is saved, say in X folder, have it process then, or use a batch job to process all unprocessed files periodically yes.

This was selected as the best answer
Sfdc_beginnerSfdc_beginner

Hi There,

 

One more question, please share me a few examples of how to read an uploaded XML file , parse it and creating records using an Apex class.

 

Thanks in advance

SFDC Beginner