Start of Content Area

This graphic is explained in the accompanying text MapExampleTwoTablesToNestedTables  Locate the document in its SAP Library structure

Problem

This example is similar to the example MapExampleTwoTablesToFlatStructure: The source structure consists of two records, which contain the name and the telephone number for a person. This example is a little more complicated, since the target structure is not flat, and all the telephone numbers for a person are to be stored within a Person record.

Solution

This graphic is explained in the accompanying text

·        The target structure must have the same number of Person records as there are in the source structure. This following assignment is therefore valid:
/ns:MapExampleNestedTablesMessage/Person = /ns:MapExampleTwoTablesMessage/Persons/Person.

·        You can use a simple assignment to copy the values for Id and Name to the target structure.

·        To copy the telephone numbers across, you need to have the corresponding telephone number for each ID. You therefore proceed as outlined in the example MapExampleTwoTablesToFlatStructure with an advanced user-defined function getPhonesById:

This graphic is explained in the accompanying text

The function proceeds as follows:

...

       1.      The function needs PersonId and Number in the Telephones context so that all the IDs and telephone numbers within this context can be copied.

       2.      The basic idea is to save a list of all telephone numbers for an ID in a container object so they can be read again later. To do this, the function accesses a map phones. If this map does not already exist, the function creates a new HashMap. The function then either creates a list (the first time) or uses an existing list for each PersonId (b[i]) and adds the corresponding telephone numbers (c[i]) to it. The map containing the list of telephone numbers is passed to the container object phones, so that it is available each time the function is called.

       3.      Finally, the function uses the ResultList Object to return the list of telephone numbers for the input argument Id (a[0]).

Advanced User-Defined Function getPhonesById

public void getPhonesById(String[] a, String[] b, String[] c, ResultList result, Container container){

Map map = (Map) container.getParameter("phones");
if (map == null){
  map = new HashMap();
  for (int i = 0; i<b.length; i++){ // if the payload is valid to xsd, b and c have equal length
    List list = (List) map.get(b[i]);
    if (list == null){
      list = new ArrayList();
      map.put(b[i], list);
    }
    list.add(c[i]);
  }
  container.setParameter("phones", map);
}

List list = (List)map.get(a[0]);
for (int i = 0; i< list.size(); i++) result.addValue((String) list.get(i));

}

 

 

 

 

 

End of Content Area