function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ShionShion 

Serializing an object to XML via an apex class

Hi all,

 

I am working on a solution where I would like to be able to take a custom object populate it then serialize it to XML to store somewhere - in a field in another object to be exact.

 

This I usually do in .NET using XML serialization and it works great. I then can at any time deserialize it obviously for later use. Almost like a freeze and defreeze mechanism.

 

What I really want to do is create another object that will have a text area field where the xml of the serialized object will go and I can retrieve this at any time out of this object to "defreeze" the child object and deserialize it back into memory.

 

In .NET this is really easy to do. But in APEX is this possible at all? The ideal will be I will create a trigger on object x and then that trigger will then run an apex class that runs some code that does the serialization. And as u can set "before" and "after" events in triggers then I will be able to maintain the two states on an object right???

 

Well that's how I vision it as an experienced .NET programmer new to Apex programming.

 

Any pointers guys? Or if it is even possible?

 

Regards,

 

Shion.

TomSnyderTomSnyder

 

 

I believe what you are trying to do can be done but its not as easy as a .net's serializer.  It would be more like transending .net reflection.

 

 

to serialize in APEX I think need to do something like...

 

determine object type:

 if (obj.GetSObjectType() == Lead.sObjectType) ... 

  

loop through the all the fields, and write them via xmlWriter:

 

Map<String, SObjectField> fields = Lead.SObjectType.getDescribe().fields.getMap();

 

In the above,  i used the Lead object,  not sure if you can make this so its not a strongly typed object,  otherwise you are stuck with list of if/thens for each object to be able to get the describe info.  Your method for serializing and deserializing the fields will be reusable though.

  

to deserialize,  use the xmlreader to parse the xml

   create new object of type, then add the fields.

 

 

 Here's one of my related Ideas you may wish to vote for:

 

http://ideas.salesforce.com/article/show/101514/XSLT_support_in_APEX#skin=le

 

 

 

-Tom

 

 

ShionShion

Tom,

 

You certainly could manually serialize the objects by hand which I think is what you are saying. You can use reflection like you said. I do this already in another implementation I have done but it uses .NET to do the reflection. 

 

If I write a manual serializer and deserializer then the xml help can take any form really I guess it just needs to be reflective of the object it represents and be able to hold state for that object at the time it was created (hold the values for it etc).

 

Providing APEX allows me to write xml in a decent manner then I can build my document up via a control flow (itterating through the fields) like you said .

 

Shion.

TomSnyderTomSnyder

I suggest you take a look at the XMLDOM http://developer.force.com/codeshare/apex/ProjectPage?id=a0630000002ahp5AAA aid in the xml parsing.

 

Also be aware to keep within the  apex governor limits.  so write to only consume what you need to complete the task at hand.  The SObject.SObjectType.getDescribe() is what you will use to get the 'Refection' however as I mention I think this will need to be a concrete object not the generic sObject Type. 

 

 

 

 

_Tom

 

 

 

 

 

 

Daniel BlackhallDaniel Blackhall
The link to idea is dead. Here is the idea: XSLT support in APEX (https://success.salesforce.com/ideaView?id=08730000000Br0dAAC)
I am working on a project that would be 100 times easier with this feature.
sfdc integration 01sfdc integration 01
We can do XML serialization  of any salesforce data like list of contacts \ map of contacts .. I developed an service which is to send List of  contacts in XML formate with out using xmlStreamReader methods
sfdc integration 01sfdc integration 01
without XMLStreamWriter*
sfdc integration 8sfdc integration 8
I have soluction here :   http://www.salesforce2u.blogspot.in/