In a recent project we had to read XML data sent from an external system and update the records in Salesforce. The external system had a REST API that accepted ExternalID as an input and gave us updated values based on the ExternaID. We then used these updated values to update the records in the system. In the process of building our project we discovered that there was very little information on reading XML inside an Apex class.
When integrating with an external system we have to use global standards or a universally recognizable language/ protocol that is recognizable by both systems in integration. Force.com supports global standards of SOAP and REST based API. You can read more about the two on the help topic on API. For data transfer between the two systems we have different options like XML, JSON and even plain text. While using the REST API we can send values via XML, JSON or even plain text in its body. SOAP supports only XML, however, XML is the most popular way of data transfer between systems.
XML is a freeform markup language that allows you to define elements using a schema, and is typically used to communicate between machines in a human readable format. The beauty is that XML can be used to transmit data between any two systems, whether they are cloud systems or on-premises servers. We could get the status of cake being baked in a microwave into the Force.com database in the future or we could connect to any other system like home-away, work.com, oracle or any other system that knows how to read XML and send XML. When I first learned that XML is the language that is human and machine readable I was very very excited, and I thought this was my ticket to program my own terminator. Sadly after 48 hours I learned that project terminator was still a distant dream. Another thing I learned was that reading an XML from Apex is very easy if you know what you are doing.
The following is an example of a XML that we will be receiving from the external server, we are going to read it and insert its value in Account Object.
To get the data from the external system, we must first send a request to the server mentioning what we exactly need. Let us assume that the external system primary key is stored as AccountExternalID__c custom field in Account data.
We will be using the following steps to access data.
- We will first send an XML containing the AccountExternalID__c and send them to the URL of external system.
- We will get another XML in response from the external system that will be parsed in APEX. We will convert this XML into SObject and update the values.
Let us dig into the code
Reading from XML file is similar to reading any file using computer code. We use the following steps for reading any file,
- Find the opening word in the file.
- Find the tokens to identify.
- Repeat step 2 till you reach the end.
The remote server is programmed to read the XML we have send and return us the Accounts we requested in the following format.
In our code we are going to read the file line by line and collect to store it into a List. The flowchart of what we will be doing is as follows:
Now, let us write the code to read the file based on the flowchart above.
The above code will read the XML and store the values in AccountList. You can decided what happens to the AccountList at the end of the function, if we want the AccountList to be processed further or if the accounts are going to be updated.
Reading XML input from a remote server is much easier than you might think it. It opens a wide range of possibilities for functionality that can be achieved by integrating Force.com platform with any system in the world. Learning XML has its own advantage: First of all it is an open standard, hence most systems will support it. It is a cool language, its very straightforward, and when the machines do rise, we will have the language our enemy speaks, which will be handy for intercepting all their messages.
May the Force be with you.