cancel
Showing results for 
Search instead for 
Did you mean: 

Adding nodes in Target message Dynamically

0 Kudos

Hi Gurus,

I would like to ask for your help on a complex requirement that I have.

We're trying to add a dynamic node to a target message. The schema cannot be changed because it's standard for all interfaces. The only option is to add a node in mapping runtime.

Upon browsing SCN, I saw a blog regarding playing around with the target structure. Thank you Sunil Chandra  for this I used the code below as suggested by Sunil and using Single Values in the execution type:

StructureNode node = ((StructureNode) container.getParameter("STRUCTURE_NODE"));

node.setPostValue("<Body><Node><Field1>"+input1+"</Field1><Field2>"+input2+"</Field2></Node></Body>");

This worked for me for the following example:

<Body>

  <Node>

  <Field1>A</Field1>

  <Field2>B</Field2>

  </Node>

</Body>

<Body>

  <Node>

  <Field1>C</Field1>

  <Field2>D</Field2>

  </Node>

</Body>

But when I'm having multiple Nodes per Body, I'm still only getting one Node per body.

<Body>

  <Node>

  <Field1>A</Field1>

  <Field2>B</Field2>

  </Node>

  <Node>

  <Field1>E</Field1>

  <Field2>F</Field2>

  </Node>

</Body>

<Body>

  <Node>

  <Field1>C</Field1>

  <Field2>D</Field2>

  </Node>

  <Node>

  <Field1>G</Field1>

  <Field2>H</Field2>

  </Node>

</Body>

By the way, let's assume that these are the source nodes that I need to put in directly to the target message.

I tried changing the execution type to All values of a context and all values of a queue, but that did not work. Also, with that, I tried adding a for loop but I'm getting a syntax error regarding methods. I guess the node.setPostValue cannot be repeated.

Any ideas are welcome kind sirs

Thanks!

Regards,

SAPEnthusiast

Accepted Solutions (1)

Accepted Solutions (1)

former_member190293
Active Contributor
0 Kudos

Hi SAPenthusiast!

I've just tried adding multiple nodes within one parent node. It works.

<?xml version="1.0" encoding="UTF-8"?>

<ns0:MT_Order_Items xmlns:ns0="urn://ns_train">

     <Item>

          <AAA>

               <BBB>value</BBB>

               <BBB>value2</BBB>

          </AAA>

          <AAA>

               <BBB>value3</BBB>

               <BBB>value4</BBB>

          </AAA>

     </Item>

</ns0:MT_Order_Items>

Please check your function . It should work.

I guess what you mean: it doesn't create second <Item> element?

Regards, Evgeniy.

former_member190293
Active Contributor
0 Kudos

But I suggest you to go for XSLT mapping in this case. Don't look for "tricky" workarounds if you have a straight way.

Regards, Evgeniy.

0 Kudos

Thanks Evgeniy I'm not much of an XSLT expert, but I'll try it out. Have you tried it before though?

former_member190293
Active Contributor
0 Kudos

I often use XSL transformations in mapping development. It may seem a bit complicated at first sight but when you start you find out that many requirements can be achieved with a couple of strings of code.

Regards, Evgeniy.

0 Kudos

Hi Evgenly,

Would very much appreciate it if you can help me get started with the XSLT

I've tried using xslt before in editing the content, but i haven't tried adding whole segments.

I have an idea on how to add the nodes and fields. But I have no idea on how to put it in a specific node? How do I put it in a node that I want? Also, how can I control the occurrences?

Thanks Sir!

former_member190293
Active Contributor
0 Kudos

Hi SAPenthusiast!

What's your exact requirement?

Regards, Evgeniy.

0 Kudos

Hi Evgeniy,

Please see my exact requirement below:

Source Structure/ message:

<Header>

  <Node1>

  <Field1>fieldtest</Field1> .. field2,3,4 etc.

  </Node1>

  <Node2/>

  <Node3>

  <SubNodeofNode3 attr1="attr1test" attr2="attr2test" attr3="attr3test">subnodetest</SubNodeofNode3>

  </Node3>

</Header>

<Body>

  <RepeatingNode>

  <Node2/>

  <Node3>

  <SubNodeofNode3 attr1="attr1test" attr2="attr2test" attr3="attr3test">subnodetest</SubNodeofNode3>

  </Node3>

  <Node3>

  <SubNodeofNode3 attr1="attr1test2" attr2="attr2test2" attr3="attr3test2">subnodetest2</SubNodeofNode3>

  </Node3>

  <Node3>

  <SubNodeofNode3 attr1="attr1test3" attr2="attr2test3" attr3="attr3test3">subnodetest3</SubNodeofNode3>

  </Node3>

  <RepeatingNode>

  <Node2/>

  <Node3>

  <SubNodeofNode3 attr1="attr1test4" attr2="attr2test4" attr3="attr3test4">subnodetest4</SubNodeofNode3>

  </Node3>

  <Node3>

  <SubNodeofNode3 attr1="attr1test5" attr2="attr2test5" attr3="attr3test5">subnodetest5</SubNodeofNode3>

  </Node3>

  <RepeatingNode>

  <Node2/>

  <Node3>

  <SubNodeofNode3 attr1="attr1test6" attr2="attr2test6" attr3="attr3test6">subnodetest6</SubNodeofNode3>

  </Node3>

  <Node3>

  <SubNodeofNode3 attr1="attr1test7" attr2="attr2test7" attr3="attr3test7">subnodetest7</SubNodeofNode3>

  </Node3>

  <Node3>

  <SubNodeofNode3 attr1="attr1test8" attr2="attr2test8" attr3="attr3test8">subnodetest8</SubNodeofNode3>

  </Node3>

</Body>

Target Structure Message:

<Header>

  <Node1>

  <Field1>fieldtest</Field1> .. field2,3,4 etc.

  </Node1>

  <Node2/>

</Header>

<Body>

  <RepeatingNode>

  <Node2/>

  <RepeatingNode>

  <Node2/>

  <RepeatingNode>

  <Node2/>

</Body>

The Node3s in the target structure does not exist. I must be able to add it in the target output message dynamically. It has to be the same copy as the source message.

But we cannot use the source schema as our target schema because the target schema is standard for all our interfaces.

Thanks again for your help!

Answers (0)