Salesforce XML Generation Step by Step Guide

Salesforce uses Extensible Markup Language (XML) to define document encoding in both machine and human-readable format. The goal for using XML language is to give emphasis on usability, simplicity and generality. Salesforce uses the highest version of Extensible Markup Language, which support the use of any Unicode character in character data, attributes, comments and instructions processing.

Salesforce is a Customer Relationship Management software built to bring your company’s data and information in one place. It is more than just a customer interaction tracker. It has inbuilt capabilities to track all your company’s sales and marketing activities. There is no CRM solution that generates more leads as does Sales force Customer Relationship Management.

You already know that WSDL-2-Apex is great when it works. The problem, however, is that at times API vendors do not support REST. Because the WSDL-2-Apex rarely works, the best option is to build a good old fashion Extensible Markup Language (XML) from scratch. This post provides you three methods for Salesforce XML generation.

Salesforce XML Generation Guide

Salesforce XML Generation Method One

Using Force.com Migration tool to generate package.xml file:

Force.com Migration tool is a perfect solution for generating a complete package.xml file for an organization. The Migration tool has all metadata components for all items subscription. It can also list all the items for component type where required.

Step one – Figuring out the available components: 

Use the describe Metadata function. Calling the function gives you a list of all the available metadata components. The function further returns the names to use in your package.xml file. It will also return information on the storage location of the metadata component types. The ANT library can also be agreat benefit because it contains the describe Metadata target. This will return the same results in text form. You can also decide to pass in an API version.

Step two – retrieving folders for the in-folder metadata:

If the metadata is stored in folders, identify the folder to get a list of the metadata available in the org. To know the folder, use the listMetadata function. If you want to list all of the folders first, add a suffix to the foldered –metadata type and then call the function. You can also use the Salesforce ANT library listMetadata target to return the results in text form.

Step three – Listing the components for each metadata types: 

This step is for all components that are not foldered and are returned in step one. You need to use the listMetadata function to get all the instances of each component in the target org. If the metadata is foldered, make a list call for each. This step is not suitable for those components that support wildcards.

Step four – Building out your package.xml file: 

Make sure you execute the first three steps sequentially appropriately. Once you do that correctly, you should have all that you need to build your package.xml file in Force.com Migration tool. Use the results achieved from stage one through stage three to build your package.xml file.

Step five – retrieving your data: 

Your package.xml file is important for this stage. Once you are done building the package.xml, use it to do your data retrievals.

Point to note:

  •  If you are generating your package.xml file with any client orgs, once thing you are likely to run into is the maximum retrieval limits. It is easy to get around this though. Simply have an intermediate process to help chunk your package into small mini-retrieves.
  •  The package.xml used in Force.com IDE is similar to package.xml used in the Migration tool. In fact, you can refresh one of your projects with everything in it. You can then grab your package, parse it to what you want and then use it to perform a retrieve.

Salesforce XML Generation Method Two

Using String to Generate Salesforce XML:

The second best way to generate Salesforce XML file is using the string option. This option uses the ‘String’value to generate the Extensible Markup Language. To do this, follow the steps outlined below:

Step one: 

Supply the sting API_KEY on the first line of your file. Use ‘String maxRecordValue’ to indicate the number of record values you intend to have in your file.

Step two: 

Define your keyword value plus your state value.

Step three: 

The last step is to define your file request using the string value.

Point to note: 

  •  The code may not look elegant and it may seem bloated as you write. However, it is a very simple code to write. This means that you can clearly see and understand the Extensible Markup Language (XML) file with a lot of ease.
  • Because it is simple, clear and easy to understand, you could say it is somewhat self-documenting.

Salesforce XML Generation Method Three 

Using the Document Object Model (DOM) classes:

The third method you can use to generate Salesforce XML is the Document Object Model classes. It take a form, which looks similar the string model. The difference is in the way the XML file is generated. The entire file is written using the Document Object Model classes and only includes the ‘String request’ option once.

Step one: 

The first thing you need to do is to include the API_KEY on the top section of your xml file. It should be on the first line. Determine the maximum record value and the keyword value. Make sure that the state value is not empty.

Step two: 

You need to define a new Document Object Model by defining the XML Node and setting the namespace.

Step three: 

This is the state for defining the header section of your XML using the DOM classes. Include the envelop and the API_KEY in the header section. It is easy to do this. Simply use the DON.XMLNODE header and the DOM.XMLNODE API_KEY options.

Step four: 

Determine your requirements in the body section.

Point to note: 

Using the Document Object Model (DOM) classes is more structured than the string model. Once you are done, you feel like you have done a lot of work than you would had you opted for the string model to generate your XML file.

Now that you’ve read the Salesforce XML generation guide, you should should have a better idea of the language.

Amanda McDonnald
Amanda is the Lead Author & Editor of Rainforce Blog. Amanda established the Rainforce blog to create a source for news and discussion about some of the issues, challenges, news, and ideas relating to Salesforce usage.
Amanda McDonnald on sabtwitterAmanda McDonnald on sabgoogle