1

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

2
  • 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

1

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');
system.debug(a);

String aString = JSON.serialize(a);
system.debug(aString);

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

String oString = JSON.serialize(o);
system.debug(oString);
0

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>
                          <Attribute1>Value1</Attribute1>
                          <Attribute2>Value2</Attribute2>
                        </ObjectName>'; */
   Dom.Document doc = new Dom.Document();
   doc.load(xmlString);

    //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()) {
       System.debug(child.getText());
    }
}

Referrences: here and here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.