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
tony_ctony_c 

Custom XML Rest API changing the root xml element.

Hello All-

I'm trying to write a custom Rest API that a 3rd party vendor will use to update our org.   The problem I'm having is that the XML data being passed doesn't have a root element of "request" and unfortunately I cannot have the 3rd party requester change their request XML payload.

My question is, is there a way to change the name of the root element? 

If not, could anyone point me to another resource that I could use to accomplish this?

Thank you so much for any help you could provide

Best Answer chosen by tony_c
daniel_hdaniel_h
Looking at the documentation here: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_rest_methods.htm

I think you can get the entire xml into requestBodyProperty and then work with it there. "If the Apex method has no parameters, Apex REST copies the HTTP request body into the RestRequest.requestBodyproperty. If the method has parameters, then Apex REST attempts to deserialize the data into those parameters and the data won't be deserialized into the RestRequest.requestBody property."

All Answers

daniel_hdaniel_h
Why do you need to change the root element to request? Are you writing the REST service in Salesforce that will receive the request, can't you accept any xml and parse it?
tony_ctony_c

Thanks so much for response-  Sorry if I wasn't that clear.

"Why do you need to change the root element to request? Are you writing the REST service in Salesforce that will receive the request, "
Yes I am attempting to write the REST service in Salesforce that would receive XML data from a vendor.  I am using the @RestResource annotation and it will only allow xml with a root element name of "request".  I was hoping there was a way around this.

"can't you accept any xml and parse it"
I'm not sure. This would be Ideal.  Could you point me in the right direction in going about doing that? 

daniel_hdaniel_h
Looking at the documentation here: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_rest_methods.htm

I think you can get the entire xml into requestBodyProperty and then work with it there. "If the Apex method has no parameters, Apex REST copies the HTTP request body into the RestRequest.requestBodyproperty. If the method has parameters, then Apex REST attempts to deserialize the data into those parameters and the data won't be deserialized into the RestRequest.requestBody property."
This was selected as the best answer
tony_ctony_c
Thank you so much.