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.– martinCommented 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– Anindya HalderCommented Oct 22, 2015 at 9:30
Add a comment
|
2 Answers
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);
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());
}
}