Handling a SOAP Object with Array

Sven Brose
Tera Guru

Hello,

I need to fill an array in a SOAP-Request:

 

<IctAdditionalInfos>
<!--Zero or more repetitions:-->
<item>
<Guid>INC12345</Guid>
<ParentGuid>irbert566</ParentGuid>
<AddInfoAttribute>SAPMessageImpact</AddInfoAttribute>
<AddInfoValue>50</AddInfoValue>
</item>
<item>
<AddInfoAttribute>SAPProcessType</AddInfoAttribute>
<AddInfoValue>YMIN</AddInfoValue>
</item>
<item>
<AddInfoAttribute>SAPCategory</AddInfoAttribute>
<AddInfoValue>YAS</AddInfoValue>
</item>
</IctAdditionalInfos>


Simple strings can be set via
s.setStringParameterNoEscape('item.AddInfoAttribute', 'SAPProcessType');
s.setStringParameterNoEscape('item.AddInfoValue', 'YMIN');

But how is to achieve this in an array?

Thanks in advance!
Sven
2 ACCEPTED SOLUTIONS

ChrisBurks
Mega Sage

Hi @Sven Brose ,

 

Variables don't only have to be placed within the xml elements. The elements themselves can be within variables too. For instance:

<IctAdditionalInfos>
    <!--Zero or more repetitions:-->
    <item>
        <Guid>INC12345</Guid>
        <ParentGuid>irbert566</ParentGuid>
        <AddInfoAttribute>SAPMessageImpact</AddInfoAttribute>
        <AddInfoValue>50</AddInfoValue>
    </item>

    ${items}

</IctAdditionalInfos>

 

With that now you can build your xml from an array into the needed xml:

The following is an idea since I don't know how your data is coming in. I am assuming it's an array of objects containing the necessary values.

var whereverDataComesFrom = [
    {
        "AddInfoAttribute": "SAPProcessType",
        "AddInfoValue": "YMIN"
    },
    {
        "AddInfoAttribute": "SAPCategory",
        "AddInfoValue": "YAS"
    }
]

function buildXml(item){
    var xml = [
        "<item>",
        "<AddInfoAttribute>",
        item.AddInfoAttribute,
        "</AddInfoAttribute>",
        "<AddInfoValue>",
        item.AddInfoValue,
        "</AddInfoValue>",
        "</item>"
    ]
    //convert to string without commas
    return xml.join("");
}

//combine all your items into a string of xml
var itemsArr = whereverDataComesFrom.map(buildXml).join("");


...
s.setStringParameterNoEscape('items', itemsArr);

Again that is one idea of how to do this. There probably could be a better example if you posted or described what the data structure is that holds the values.

Hope this helps.

View solution in original post

Sven Brose
Tera Guru

Hi Chris,
I've testet it, but the array will not be set in the parameter.

var itemsArr = 

'<item><AddInfoAttribute>SAPProcessType</AddInfoAttribute><AddInfoValue>YMIN</AddInfoValue></item><item><AddInfoAttribute>SAPCategory</AddInfoAttribute><AddInfoValue>YAS</AddInfoValue></item>'
s.setStringParameterNoEscape('IctAdditionalInfos', itemsArr); -->is not working
s.setStringParameterNoEscape('items', itemsArr); --> not working

This is working, but only with on Attribute/Value-Pair:
s.setStringParameterNoEscape('item.AddInfoAttribute', 'testAttribute');
s.setStringParameterNoEscape('item.AddInfoValue', 'testvalue');


Result is:


<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:sap-com:document:sap:soap:functions:mc-style">
   <soapenv:Header/>
   <soapenv:Body>
      <urn:ProcessIncident>
         <IctAdditionalInfos>
			   <!--Zero or more repetitions:-->
            <item>
               <Guid></Guid>
               <ParentGuid></ParentGuid>
               <AddInfoAttribute></AddInfoAttribute>
               <AddInfoValue></AddInfoValue>
            </item>
         </IctAdditionalInfos>
      <IctAttachments>
           <!-- <item>
               <AttachmentGuid></AttachmentGuid>
               <Filename></Filename>
               <MimeType></MimeType>
               <Data>cid:375667742933</Data>
               <Timestamp></Timestamp>
               <PersonId></PersonId>
               <Url></Url>
               <Language></Language>
               <Delete></Delete>
            </item>-->
         </IctAttachments>
         <IctHead>
            <IncidentGuid>680FC3B7975FED10310F74871153AF9F</IncidentGuid>
            <RequesterGuid>SERVICENOWRZS0SAPSOLUTIONMANAGER</RequesterGuid>
            <ProviderGuid>BF8D284C68335032E10000000A0ACCA6</ProviderGuid>
            <AgentId></AgentId>
            <ReporterId></ReporterId>
            <ShortDescription>Checkmk: WARN - T4 - Enterprise Alert</ShortDescription>
            <Priority></Priority>
            <Language>DE</Language>
            <RequestedBegin></RequestedBegin>
            <RequestedEnd></RequestedEnd>
         </IctHead>
         <IctId>INC0023258</IctId>
         <IctPersons>
         </IctPersons>
         <IctSapNotes>
         </IctSapNotes>
         <IctSolutions>
         </IctSolutions>
         <IctStatements>
            <!--Zero or more repetitions:-->
            <item>
               <TextType>YU99</TextType>
               <Texts>
                  <!--Zero or more repetitions:-->
                  <item><![CDATA[Host:     T4
Service:  Enterprise Alert
Output:   For testing purposes only - this check simulates a real state change and will change it´s state every hour.
]]></item>
               </Texts>
               <Timestamp></Timestamp>
               <PersonId></PersonId>
               <Language></Language>
            </item>
         </IctStatements>
         <IctTimestamp></IctTimestamp>
         <IctUrls>
         </IctUrls>
      </urn:ProcessIncident>
   </soapenv:Body>
</soapenv:Envelope>

Still any Ideas?

View solution in original post

5 REPLIES 5

Hi Chris,

sorry, my mistake. I've forget to set the variable in the SOAP Message Function.
It works like a charm! 

Thank you very much!