Send multiple values for same xml tag in SOAP

dinesh91n
Tera Expert

Hi guys, i have a requirement to send multiple value under a same xml tag based on the values selected by the user, example like the one given below

<complexType>  

<exampleElement>Data 1</exampleElement>  

<exampleElement>Data 2</exampleElement>  

<exampleElement>Data 3</exampleElement>  

</complexType>

based on the user values selection <exampleElement> may be 3 times or 5 times or even null.

I tried looking into the following community link SOAP Message Complex Types: How do we send these using SOAPMessage()? but i wasn't able to get my solution.

I even tried looking into john blog Inserting XML into ServiceNow's SOAPMessage Object-John Andersen, but his method states to change the tag elements which is not possible in my case.

So if anyone has any suggestions or solutions please do share.

1 ACCEPTED SOLUTION

Here is an example script that calls the outbound SOAP message function:



//unrelated values that may also be needed for the same function


var parmOneVar = "Example 1";


var parmTwo = "Example 2";



var elementArray = ['Data 1','Data 2','Data 3']; //example of array containing unknown number of elements


var xmlString = '';



var sm = new sn_ws.SOAPMessageV2('YourSoapMessage', 'SOAPMessageFunction');


sm.setStringParameter('StaticParm1', parmOne);


sm.setStringParameter('StaticParm2', parmTwo);



for (var i = 0; i < elementArray.length; i++) {


  //build XML elements


  xmlString += '<exampleElement>' + elementArray[i] + '</exampleElement>';


}



sm.setStringParameterNoEscape('complexType', xmlString);



var response = sm.execute();


var responseBody = response.getBody();


var status = response.getStatusCode();



The idea is you would replace all of your <exampleElement> tags and values with the complexType parameter.



<complexType>


        ${complexType}


</complexType>


View solution in original post

10 REPLIES 10

Hi dan, is this method possible in Eureka?


Eureka has an older SOAP API.   I've adjusted the script to work off of the older API but it does not have an xml escape function.



//unrelated values that may also be needed for the same function


var parmOne = "Example 1";


var parmTwo = "Example 2";



var elementArray = ['Data 1','Data 2','Data 3']; //example of array containing unknown number of elements


var xmlString = '';



var sm = new SOAPMessage('YourSoapMessage', 'SOAPMessageFunction');


sm.setParameter('StaticParm1', parmOne);


sm.setParameter('StaticParm2', parmTwo);



for (var i = 0; i < elementArray.length; i++) {


  //build XML elements


  xmlString += '<exampleElement>' + elementArray[i] + '</exampleElement>'


}



sm.setParameter('complexType', xmlString);


sm.post();




If that does not work, you could build the SOAP message from scratch.   This highly depends on what your WSDL looks like but here is a small snippet using your example.   I also must say, I've never actually used this approach.   This is all in theory based on the wiki article here.



var envelope = new SOAPEnvelope();


var complexType = envelope.createBodyElement("complexType");



for (var i = 0; i < elementArray.length; i++) {


  //build XML elements


  envelope.createElement(complexType,"exampleElement",elementArray[i])


}



var req = new SOAPRequest("http://endpoint.com");


req.setSoapAction("http://endpoint.com/action_header");


req.post(envelope);


Hi Dan, once again thank you for the timely response, so there is no escape method in older SOAP api but will this method work s.setXMLParameter('complexType',xmlstring).


Also in fuji version we have a function getRequestBody(), so using this we can preview the request xml after executing the soap message. Likewise is it possible to preview the xml in eureka? This information would help me a lot.


idk why this is marked as the correct answer. it does not work. i'm trying to do the same type of thing some client wants values from a list collector broken out in individual tags rather than comma separated like the designs and this doesn't achieve that. did you actually try this dan?


I didn't try the Eureka approach because I didn't have a Eureka sandbox.



There is an issue with the solution marked correct.   It is with the 'parmOneVar' variable.   Later it's used as just parmOne.   I probably overlooked when I refactored the code to post an example (I usually have a gross mess while testing).