1

Situation

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?

4
  • 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
    Commented Feb 21, 2019 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 Commented Feb 21, 2019 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
    Commented Feb 21, 2019 at 16:03
  • yeah that makes sense! thank you for your input Commented Feb 21, 2019 at 16:17

1 Answer 1

1

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>' +
                '</Body>';

This makes code readable as well.

4
  • as apposed to doing something like this salesforce.stackexchange.com/questions/102090/… ? Commented Feb 21, 2019 at 16:15
  • I am Definitely considering your solution in comparison haha I have 30 fields to do this for. Commented Feb 21, 2019 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. Commented Feb 21, 2019 at 16:19
  • Sounds good haha thanks again Commented Feb 21, 2019 at 16:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .