Skip to Content
Author's profile photo Sunil Chandra

Message Mapping : Play around with target structure

Ask any PI developer about favorite mapping, the answer for sure would be Message Mapping. Still sometimes our favorite message mapping seem to be helpless due to its fixed target structure definition and we are forced to use java/xsl mappings or do something at adapter level for the desired output. For example you can visit some of these threads – Pass value to the node, Removing ns0 from namespace, XSLT for dynamic target field names, Add namespace in message mapping, JAVA/XSLT mapping to create SOAP envelope

Perhaps there may be many ways to manage all the above requirements but sometimes it becomes like using a cannon to kill a fly! So here we go with our favorite message mapping and see how to play around with tag name, occurrence, namespace and prefix at target side. Also we will try to pass value to structure node, seems weird? Let’s see how it is possible with simple user defined function.

In first step to access target field in udf, we take help of getParameter method of Container with parameter STRUCTURE_NODE that returns access to target field/node to which the UDF is being directly mapped to. The node needs to be type casted to structure/leaf as per the type of target field.

  • StructureNode node = ((StructureNode) container.getParameter(“STRUCTURE_NODE”));

          Applicable to fields having child or sub-fields.

  • LeafStructureNode node = ((LeafStructureNode)container.getParameter(“STRUCTURE_NODE”));

          Applicable to fields having no child or sub-fields.

Next step involves using node methods as per the requirements.

  • String getQName()

          Return the name of target field.

  • void setQName(String newName)

          Set the target field name with input parameter newName.

  • void setNSDeclarations(String decl)

          Set the namespace declaration and attribute with input parameter decl.

          e.g decl = ” attribute1=\”a1\” attribute2=\”a2\” xmlns:prefix=\”namespace1\””

  • void setOccurances(int min, int max)

          Set the occurrence of target field with input parameter min and max.

  • void setPreValue(String prevalue)

          Add the string prevalue unescaped just after start tag of target field. Applicable only for structure field.

  • void setPostValue(String postvalue)

          Add the string postvalue unescaped just before end tag of target field. Applicable only for structure field.


For example, lets say we have the source and target structure as shown below:

Field Name Occurence Field name Occurence
Source 1..1 Target 1..1
    Rows 0..Unbounded     Items 0..Unbounded
        Key 1..1         Element 1..1
        Value 1..1

And the requirements are..

  • The Element field name should get changed with value in Key field and should be filled with Value.
  • Output xml should have only 2 items assuming that it is coming in a particular order from source.
  • The namespace and its prefix should be Modified to “http://company.com” and pfx respectively.
  • Assign value “list of top 2 company” to Target node.

Source instance and expected result xml are as shown below.

Source Instance Result

<?xml version=“1.0” encoding=“UTF-8”?>

<ns0:Source xmlns:ns0=http://test.com>

<Rows>

     <Key>CompanyA</Key>

     <Value>abc corp</Value>

</Rows>

<Rows>

     <Key>CompanyB</Key>

     <Value>xyz info</Value>

</Rows>

       …

<Rows>

     <Key>CompanyZ</Key>

     <Value>123 ent</Value>

</Rows>

</ns0:Source>

<?xml version=“1.0” encoding=“UTF-8”?>

<pfx:Target xmlns:pfx=http://company.com>

list of top 2 companies

<Items>

     <CompanyA>abc corp</CompanyA>

</Items>

<Items>

     <CompanyB>xyz info</CompanyB>

</Items>

</pfx:Target>

And here comes the mapping part..

          UDF setNSDeclarations would fix namespace to http://company.com , fix prefix to pfx and assign value “list of top 2 companies” to Target node.

/wp-content/uploads/2012/11/image001_152907.png

public String setNSDeclarations(String prefix, String nmspace ,Container container) {


     StructureNode node = ((StructureNode) container.getParameter(“STRUCTURE_NODE”));

     node.setNSDeclarations(” xmlns:” + prefix + “=” + nmspace); // notice the space before xmlns:

     node.setQName(prefix + “:Target”);

     node.setPreValue(“list of top 2 companies”);


     return“”;                                   

}

  • /ns0:Target/Items=setOccurences(/ns0:Source/Rows, const(value=2))

          UDF setOccurences simply set the max occurence of Items to 2 to ensure top 2 Rows are mapped to Items.

/wp-content/uploads/2012/11/image003_152496.png

public String setOccurences(String sourcenode, int maxOccur, Container container) {


     StructureNode node = ((StructureNode) container.getParameter(“STRUCTURE_NODE”));

     node.setOccurences(0, maxOccur);


     return “”;

}

  • /ns0:Target/Items/Element=setTargetFieldName(/ns0:Source/Rows/Key, /ns0:Source/Rows/Value)

          UDF setTargetFieldName dynamically changes the tag name Element.

/wp-content/uploads/2012/11/image004_152498.png

public String setTargetFieldName(String targetFieldName, String targetFieldvalue, Container container) {


     LeafStructureNode node =((LeafStructureNode) container.getParameter(“STRUCTURE_NODE”));

     node.setQName(targetFieldName);


     return targetFieldvalue;

}

At end, please note that the code discussed above uses undocumented classes/methods that are subject to change in future without notice. In short, use at your own risk as any related issues will not be supported by SAP

Reference :

Accessing Element’s name in message mapping

Mapping question

Assigned Tags

      19 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Former Member
      Former Member

      Nice..info..How did you found the API for these ? Are there published some where ? if so can you add that link in above, so that it will be helpful ..

      Regards

      Rajesh

      Author's profile photo Sunil Chandra
      Sunil Chandra
      Blog Post Author

      Thanks Rajesh. I came across those undocumented methods while writing UDF in developer studio with reference to mapping api ( com.sap.xi.mapping.tool.lib_api.jar,  com.sap.xpi.ib.mapping.lib.jar) that we use for java mapping. Auto completion with ctrl space is the way forward for any further experiment 🙂  

      Regards,

      Sunil Chandra

      Author's profile photo Former Member
      Former Member

      Good one Sunil. I use to develop Java mapping for such scenarios, now I can achieve the same using UDFs. Keep blogging..

      Thanks

      Rakesh Sharma

      Author's profile photo Mark Dihiansan
      Mark Dihiansan

      I agree with Rakesh, thank you for this blog! 🙂

      Author's profile photo Grzegorz Glowacki
      Grzegorz Glowacki

      Hi Sunil,

      Thanks for sharing, it's always good to explore some undocumented features 🙂

      Regards,

      Greg

      Author's profile photo Anupam Ghosh
      Anupam Ghosh

      Hi Sunil,

                   This is very helpful blog. Expect more from you.

      Regards

      Anupam

      Author's profile photo Ambrish Mishra
      Ambrish Mishra

      Hello Sunil,

      This is top class. Would be very helpful for consultants who prefer graphical mapping.

      cheers,

      Ambrish

      Author's profile photo Former Member
      Former Member

      Nice one Sunil

      Author's profile photo Rudra Singh
      Rudra Singh

      Hi Sunil,

      Thanks for a wonderful blog. please let me know can we create Target Structure at run

      time ?

      for exp :-  I want to add extra record or element in target structure at run time ..

      Thanks & Regards

      Rudra

      Author's profile photo Former Member
      Former Member

      Nice Blog. How are you passing integer to UDF ? Is this supported in Integration Builder PI 7.30?

      Author's profile photo Vladimir Balko
      Vladimir Balko

      Thank you for that blog - I`m going to test it right now 🙂

      Author's profile photo Former Member
      Former Member

      Hi Sunil,

      Nice blog, as you said Any PI developer favorite mapping would be Message Mapping and even mine 😎

      Author's profile photo Former Member
      Former Member

      Hi Sunil.

      Very good blog.Really helpful.Thanks for sharing this information.

      Regards,

      Abhi

      Author's profile photo Former Member
      Former Member

      Hi Sunil ,

      So nice, Useful document. This doc helped me in my recent projects..................

      Thanks for sharing.

      Regards,

      Vijay Kumar K.V.N

      Author's profile photo Former Member
      Former Member

      Hi Sunil... Thanks for the blog. Really informative. Can you help me with my requirement ?

      I need to create additional fields in the target(apart from those present in MT) dynamically in mapping.

      How would I do that?

      Find the below screenshots for reference.

      error.JPG

      code.JPG

      Author's profile photo Rashmi Joshi
      Rashmi Joshi

      Very helpful doc for me. Thank you for sharing it.

       

      BR,

      Rashmi

      Author's profile photo Sj venky
      Sj venky

      i'm getting one requirement like it is a grouped interface we have contracts,payments,division order,tax and regulatory combine all these interfaces need to implement in one interface.

      Scenario is REST--->PO---->IDOC

      For all interfaces we have one common IDOC structure available on Target side and this interface need to run hourly basis daily.

      How can i implement this interface.I need your suggestions ?

      BR,

      venkat

      Author's profile photo Danie Luttig
      Danie Luttig

      Thanks for this solution as it helped me a lot to avoid cumbersome xslt mapping which did not work either due to conversion from < > " to &lt; &gt; and &quot values causing the end result to throw error reference not in prolog, THis resolved my solution in a jiffy.

       

      THANKS ONCE AGAIN for SOME GREAT WORK PRODUCED HERE FOR OTHERS TO RESOLVE THEIR OWN PROBLEMS

       

      Danie Luttig

       

      Author's profile photo TCS Support
      TCS Support

      Hi Sunil,

      This blog is very helpful.

      Keep on posting blogs like this!!