1
votes
<?xml version="1.0"?>
<tXML>
<Header>
    <Source>J1_RETAIL</Source>
    <Action_Type>Update</Action_Type>
    <Sequence_Number>0</Sequence_Number>
    <Batch_ID>4383352</Batch_ID>
    <Reference_ID>04381645</Reference_ID>
    <User_ID>SAP</User_ID>
    <Password>password</Password>
    <Message_Type>SAP_DO</Message_Type>
    <Company_ID>J1</Company_ID>
    <Msg_Locale>English (United States)</Msg_Locale>
    <Msg_Time_Zone>Eastern Standard Time</Msg_Time_Zone>
    <Version></Version>
    <Internal_Reference_ID></Internal_Reference_ID>
    <Internal_Date_Time_Stamp></Internal_Date_Time_Stamp>
    <External_Reference_ID></External_Reference_ID>
    <External_Date_Time_Stamp></External_Date_Time_Stamp>
  </Header>
  <Message>
    <DistributionOrder>
        <ProcessInfo>
            <RefTextField1></RefTextField1>
            <RefTextField2></RefTextField2>
            <RefTextField3>S082</RefTextField3>
            <RefTextField4></RefTextField4>
            <RefTextField5></RefTextField5>
            <RefTextField6></RefTextField6>
            <RefTextField7>J1</RefTextField7>
            <RefTextField8>[0001333006_SAPTOMIF]</RefTextField8>
            <RefTextField9></RefTextField9>
            <RefTextField10>[ _20191223]</RefTextField10>
            <RefNumberField1>20191220</RefNumberField1>
            <RefNumberField2>34621</RefNumberField2>
            <RefNumberField3></RefNumberField3>
            <RefNumberField4>53</RefNumberField4>
            <RefNumberField5>13</RefNumberField5>
        </ProcessInfo>
        <Comment>
            <NoteType>MB</NoteType>
            <NoteCode>05</NoteCode>
            <CommentText>[00000_8769_741_82_093_965_987_456]</CommentText>
            <Visibility>0</Visibility>
          </Comment>
          <CustomFieldList>
            <CustomField>
              <Name>SiteID</Name>
              <Value></Value>
            </CustomField>
        <LineItem>
            <DoLineNbr>1</DoLineNbr>
            <ItemName>135465</ItemName>
            <Description>A</Description>
            <UpdateActionType></UpdateActionType>
            <PackageType></PackageType>
            <DoLineStatus>Released</DoLineStatus>
            <InventoryAttributes>
              <InventoryType>F</InventoryType>
              <ProductStatus></ProductStatus>
              <BatchNbr></BatchNbr>
              <CountryOfOrigin></CountryOfOrigin>
              <ItemAttribute1>R</ItemAttribute1>
              <ItemAttribute2></ItemAttribute2>
              <ItemAttribute3></ItemAttribute3>
              <ItemAttribute4></ItemAttribute4>
              <ItemAttribute5></ItemAttribute5>
            </InventoryAttributes>
        </LineItem>
    </DistributionOrder>
  </Message>
</tXML> 

My code looks like this

%dw 2.0
output application/csv header = false , separator = "|" , quoteValues = false 
var count = 0
fun outputMap (index, comment="") = 
    {
            column_1: "000000003", 
            column_2: (payload.tXML.Message.DistributionOrder.ProcessInfo.RefTextField8 splitBy "_")[0] replace "[" with "" ,  
            column_3: if(payload.tXML.Message.DistributionOrder.ProcessInfo.RefNumberField1 != null) (payload.tXML.Message.DistributionOrder.ProcessInfo.RefNumberField1) else "0",
            column_4: if(payload.tXML.Message.DistributionOrder.ProcessInfo.RefNumberField2 != null) (payload.tXML.Message.DistributionOrder.ProcessInfo.RefNumberField2) else "0",
            column_5: "SAPTOMIF",
            column_6: payload.tXML.Message.DistributionOrder.SalesOrderNbr default "",
    column_7: if(payload.tXML.Message.DistributionOrder.Comment.NoteType == 'MB' and payload.tXML.Message.DistributionOrder.Comment.NoteCode == '05')"00000" else "",
    column_8: ???,
    column_9: "ST",
    column_10: "0" ++ index,
            column_11: (comment replace "[" with ""  replace "]" with ""default "") , 
            column_12: payload.tXML.Message.DistributionOrder.ReferenceField7 default "",
            column_13: payload.tXML.Message.DistributionOrder.SalesOrderNbr default "",

    }

---
payload.tXML.Message.*DistributionOrder flatMap (DistributionOrder) -> 
    using (filteredComment = DistributionOrder.*Comment[?($.NoteType == "MB" and $.NoteCode == "05")])
    if (filteredComment != null and filteredComment !="") 
        filteredComment flatMap(commentObject) -> 
            using (splitCommentText = commentObject.CommentText splitBy /_/)
            splitCommentText map outputMap($$,$) filter $$ > 0 and trim($[10]) !=""
    else 
        outputMap(DistributionOrder)

Here is how my output looks like

000000003|0001333006|20191220|34621|SAPTOMIF|6500054123|00000||ST|01|8769|J1|6500054123
000000003|0001333006|20191220|34621|SAPTOMIF|6500054123|00000||ST|02|741|J1|6500054123
000000003|0001333006|20191220|34621|SAPTOMIF|6500054123|00000||ST|03|82|J1|6500054123
000000003|0001333006|20191220|34621|SAPTOMIF|6500054123|00000||ST|06|987|J1|6500054123
000000003|0001333006|20191220|34621|SAPTOMIF|6500054123|00000||ST|07|456|J1|6500054123

I need value for column_8 : should assign values starting from 1,2....9 for each line i.e 1 for line 1, 2 line 2 ....

My output should look like this

000000003|0001333006|20191220|34621|SAPTOMIF|6500054123|00000|1|ST|01|8769|J1|6500054123
000000003|0001333006|20191220|34621|SAPTOMIF|6500054123|00000|2|ST|02|741|J1|6500054123
000000003|0001333006|20191220|34621|SAPTOMIF|6500054123|00000|3|ST|03|82|J1|6500054123
000000003|0001333006|20191220|34621|SAPTOMIF|6500054123|00000|4|ST|06|987|J1|6500054123
000000003|0001333006|20191220|34621|SAPTOMIF|6500054123|00000|5|ST|07|456|J1|6500054123

my new code

%dw 2.0 import java!java::util::concurrent::atomic::AtomicInteger var counter = AtomicInteger::new(0) fun increment() = Java::invoke('java.util.concurrent.atomic.AtomicInteger', 'incrementAndGet()', counter, {})

output application/csv header = false , separator = "|" , quoteValues = false

flatten(payload.tXML.Message.DistributionOrder.*LineItem map (LineItem) -> LineItem.*Comment filter($.NoteType == 'I3' and $.NoteCode == 'I3') map (Comment) -> { column_1: "000000003", column_2: (payload.tXML.Message.DistributionOrder.ProcessInfo.RefTextField8 splitBy "_")[0] replace "[" with "" ,
column_3: if(payload.tXML.Message.DistributionOrder.ProcessInfo.RefNumberField1 != null) (payload.tXML.Message.DistributionOrder.ProcessInfo.RefNumberField1) else "0", column_4: if(payload.tXML.Message.DistributionOrder.ProcessInfo.RefNumberField2 != null) (payload.tXML.Message.DistributionOrder.ProcessInfo.RefNumberField2) else "0", column_5: "SAPTOMIF", column_6: payload.tXML.Message.DistributionOrder.SalesOrderNbr default "", column_7: Comment.CommentText, column_8: increment(), column_9: "PT", column_10: Comment.CommentText, column_11: Comment.CommentText, column_12: "", column_13: "" })

1

1 Answers

1
votes

You have to Auto-increment as like below

      import java!java::util::concurrent::atomic::AtomicInteger      
      var counter = AtomicInteger::new(0)
       fun increment() = 
      Java::invoke('java.util.concurrent.atomic.AtomicInteger',  
      'incrementAndGet()', counter, {})

Please refer the Page below for more details

https://dzone.com/articles/implement-a-counter-in-dataweave-2x-and-above