I have two custom objects . How to transfer the records from one custom object to another using xml ?

  • It would help to know if this is this all happening within a single salesforce org, and why you need to use xml to do it.
    – martin
    Oct 22, 2015 at 9:04
  • Yes this will happen in a single org ... we are migrating data from external system to an intermediary object and would like to move the data from this object to final object after doing some modifications Oct 22, 2015 at 9:30

2 Answers 2


If the names of the fields you are using are the same on both objects and you don't necessarily need to use XML, you could use JSON serialize and deserialize to change the object from one type to another in your Apex code:

Account a = new Account(name='The Object\'s Name');

String aString = JSON.serialize(a);

Opportunity o = (Opportunity) JSON.deserialize(aString,Opportunity.class);

String oString = JSON.serialize(o);

If you are planning to use XML, and parse it in Apex using DOM, then you could use following

public void parseData(String xmlString){ //Pass XML String

   /* e.g., xmlString = '<ObjectName>
                        </ObjectName>'; */
   Dom.Document doc = new Dom.Document();

    //Retrieve the root element for this document.
    Dom.XMLNode details = doc.getRootElement();

    String attr1 = details.getChildElement('Attribute1', null).getText();
    String attr2 = details.getChildElement('Attribute2', null).getText();

    // print out specific elements
    System.debug('Attribute1: ' + Attribute1);
    System.debug('Attribute2: ' + Attribute2);

    // Alternatively, loop through the child elements.
    // This prints out all the elements of the address
    for(Dom.XMLNode child : details.getChildElements()) {

Referrences: here and here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .