1

We have a need where we need to parse an xml schema and dynamically create a custom object with fields at run time. Have any of you guys done this and if so can you share code snippets? If the custom object is a problem, is it possible to create a custom apex class with class variables at run time? Please let me know..

5
  • 2
    Is the XML schema constantly changing? You cant create the class ahead of time? – Phil Rymek Oct 30 '13 at 18:25
  • Also wondering the same thing. What's the use case? – Chris Duncombe Oct 30 '13 at 18:39
  • Have you looked at the Metadata and Tooling APIs? – Phil B Oct 30 '13 at 18:46
  • @PhilB The xml schema would change regularly and so we want to dynamically create the custom object or apex class. Here is the usecase. Salesforce is going to call a REST api for a call out and we want to establish a data contract where any schema changes on the external application would cause salesforce to change at compile time instead of run time issues. So the question is it possible to delete class variables from an apex class and create at run time or delete all the fields of custom object and readd them? – Buyan Thyagarajan Oct 30 '13 at 21:08
  • @SF_Ninja The xml schema would change regularly and so we want to dynamically create the custom object or apex class. Here is the usecase. Salesforce is going to call a REST api for a call out and we want to establish a data contract where any schema changes on the external application would cause salesforce to change at compile time instead of run time issues. So the question is it possible to delete class variables from an apex class and create at run time or delete all the fields of custom object and readd them? – – Buyan Thyagarajan Oct 30 '13 at 21:11
2

You can do this with a reflector[1], but unfortunately, i don't believe you can do this in pure native apex. Even the apex <--> metadata api bridge uses javascript, and requires a browser (to the best of my knowledge). However, an external application (i call it a Reflector) living externally, say on heroku, can access both the metadata and subject api's as well as making your rest callouts. The metaforce and rest force ruby gems make it a pretty trivial bit of work to do this. Essentially, you'd be making your rest call, deserializing the xml or json to a ruby object, then using subject describe calls to validate the ruby object's schema against the subject schema, and reconciling the schema as necessary via the metadata api; after which you can then of course upsert your data.

I'm sure other, inferior :) languages like java have similar salesforce integrations and this general algorithm will suffice regardless of language choice.

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.