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

danpatino
Tera Expert

I've come across this scenario before.   The approach we took was to build the XML elements as a string before passing them as parameters to the soap message function.   Make sure you use setStringParameterNoEscape(String name, String value) on the XML parameter instead of simply setStringParameter(String name, String value).


Hi Dan thank u so much for the reply, but if possible can give me a sample code by taking the example I have mentioned above.


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>


Thank u so much Dan