cancel
Showing results for 
Search instead for 
Did you mean: 

Message Mapping - only 1st item in collection is read

saket_chiluveru
Explorer
0 Kudos

Hello,

A noob question.

I have an input XML that has a collection on items and I would like to extract specific values from this collection and push it to the output. I have seen some examples, but I am unable to extract from multiple items.

This is what I intend to do. Extract as

  • extract <partnerId> for partnerRoleCode = WE and map it to an o/p <shipToNumber>
  • extract <partnerId> for partnerRoleCode = AG and map it to an o/p <soldToNumber>

See i/p XML below.

     <SAPCpiOutboundPartnerRole>
        <orderId>0000490025</orderId>
        <entryNumber>-1</entryNumber>
        <partnerId>11111</partnerId>
        <documentAddressId>1</documentAddressId>
        <partnerRoleCode>WE</partnerRoleCode>
        <integrationKey>-1|0000490025</integrationKey>
      </SAPCpiOutboundPartnerRole>
      <SAPCpiOutboundPartnerRole>
        <orderId>0000490025</orderId>
        <entryNumber>-1</entryNumber>
        <partnerId>44444</partnerId>
        <documentAddressId/>
        <partnerRoleCode>AG</partnerRoleCode>
        <integrationKey>-1|0000490025</integrationKey>
      </SAPCpiOutboundPartnerRole>
     

The issue I am facing is that only the first <SAPCpiOutboundPartnerRole> seems to getting read in the groovy script.

def void extractShipToSoldTo(String[] is,String[] ps, Output shipToNumber, Output soldToNumber, MappingContext context) {
        //String value1 = context.getHeader(is[0]);
        //String value2 = context.getProperty(ps[0]);
        /*String s = "";
        for (int i = 0; i < is.length; i++){
            s = s + is[i].toString();
        }*/
        
        
        if (is[0]=='WE'){
            shipToNumber.addValue('SHIPTO' + ps[0]);    
        }
        if (is[0]=='AG'){
            soldToNumber.addValue('SOLDTO' + ps[0]);
        }
}

This produces only either the soldToNumber or the shipToNumber when I test with the i/p XML as above. I have tried printing the values, but no luck.

What am I doing wrong ?

I think Message Mapping should be able to handle this simple case.

How do I solve this problem using Mapping.

Accepted Solutions (0)

Answers (1)

Answers (1)

bhalchandraswcg
Contributor
0 Kudos

Hi Saket,

Context of partnerId and partnerRoleCode must be outer node of SAPCpiOutboundPartnerRole so that function extractShipToSoldTo will receive all the Partner IDs and Partner Role Codes together. Then, extractShipToSoldTo function can loop over Partner Role Codes and map them as required.

def void extractShipToSoldTo(String[] partnerIds, String[] partnerRoleCodes, Output shipToNumber, Output soldToNumber, MappingContext context) {
	partnerRoleCodes.eachWithIndex { partnerRoleCode, index ->
		switch (partnerRoleCode) {
			case 'WE':
				shipToNumber.addValue("SHIPTO${partnerIds[index]}");
				break
			case 'AG':
				soldToNumber.addValue("SOLDTO${partnerIds[index]}");
				break
		}
	}
}

Hope this helps,

Bala