I need to build a connection from Salesforce to "post" to a rest api. The recipient has given me XDS schema files of the data to be sent over.

My Current Plan

  1. Build a sObject to be the object to send.
  2. Match the sObjects fields to the schema defined in the xds file.
  3. Write the XML using the DOM
  4. Post the XML

I feel like this method is kinda laborious is there a better way?

  • You don't need a SObject for this. As long as you know what parameters you need to post, and from where your data will be sourced for those parameters, you just need to build the request body, and post it to the endpoint. – Jayant Das Feb 21 '19 at 15:57
  • My reasoning with the sObject is that some of our data needs to be in certain formats before sending. So my thoughts where to use the Sobject as a staging ground to normalize the data in config – Ryan Sherry Feb 21 '19 at 15:59
  • 1
    I would still recommend not to have one. You can easily do the transformation right in your apex class. If you introduce a SObject, you are introducing some overhead as well. You will need to make lots of considerations e.g., you will additionally need to take care of removing data from it as it counts towards your storage. Who should have visibility to that object, etc. etc. – Jayant Das Feb 21 '19 at 16:03
  • yeah that makes sense! thank you for your input – Ryan Sherry Feb 21 '19 at 16:17

When XML is bit tricky, then I just design it myself using String manipulation.

String myXML = '<Body> +
                        '<Name>' + account.Name + '</Name>' +
                        '<Age>' + account.Age + '</Age>' +
                        '<Id>' + account.Id + '</Id>' +
                        '<DOB>' + account.DOB + '</DOB>' +

This makes code readable as well.

| improve this answer | |
  • as apposed to doing something like this salesforce.stackexchange.com/questions/102090/… ? – Ryan Sherry Feb 21 '19 at 16:15
  • I am Definitely considering your solution in comparison haha I have 30 fields to do this for. – Ryan Sherry Feb 21 '19 at 16:17
  • 1
    Yups both are creating XML, this way is faster, readable, simpler. I can't think of a reason to not use this. If someone disagrees i would like to know why. – Pranay Jaiswal Feb 21 '19 at 16:19
  • Sounds good haha thanks again – Ryan Sherry Feb 21 '19 at 16:23

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.