Search code examples
muledataweavemule4

Create Multi Segment Cobol copybook file in Mule 4


I need to create a cobol copybook file from incoming JSON. It is a multisegment file. I am facing an issue while creating the multiple segments in the DW. For the purpose this question I just hardcoded the value as below.

DW Code:
%dw 2.0
output application/flatfile schemaPath = "schemas/VP.ffd", structureIdent="MultiSegment"
var fieldDataArray= 0 to 3
fun filterKey(keyName, keyArray) = sizeOf(keyArray filter ((item, index) -> item ~= keyName ))==1
---
{
    vps: {
        vps: [{
            ":USRDSB:-SOURCE": {
                ":USRDSB:-SOURCE": "SOURCE",
                ":USRDSB:-SOURCE-ID ": "MULESOFT"
            },
            ":USRDSB:-HEADER": {
                ":USRDSB:-HEADER": "HEADER",
                ":USRDSB:-TRANSMISSION-ID": "VTY"
            },
            ":USRDSB:-DETAIL-RECORD": payload map ( payload01 , indexOfPayload01 ) -> {
                
                ( fieldDataArray map  {
                    ":USRDSB:-FIELD-IND": flatten(payload01 pluck $$) match {
                        case k if(filterKey("Option_Set__c",flatten(payload01 pluck $$)) and $$ == 0) -> "019303" as Number 
                        case k if(filterKey("Statement_Message_ID__c",flatten(payload01 pluck $$)) and $$ == 1) -> "014205" as Number
                        case k if(filterKey("CDF23__c",flatten(payload01 pluck $$)) and $$ == 2) -> "008902"  as Number
                        case k if(filterKey("ASC__c",flatten(payload01 pluck $$)) and $$ == 3) -> "011206" as Number
                        else -> "1" as Number

                   }
                   

                }),
                 ":USRDSB:-CARD-NBR": payload01.TSYS_ID__c,
                    ":USRDSB:-CUST-IND": "D",
                    ":USRDSB:-FIELD-DATA": payload01.Statement_Message_ID__c
                
            }
        }]
    }
}

form: COPYBOOK
structures:
- id: 'MultiSegment'
  name: MultiSegment
  data:
  - groupId: 'vps'
    items:
    - groupId: 'vps'
      count: '>1'
      items:
        - { idRef: ':USRDSB:-SOURCE', count: 1 }
        - { idRef: ':USRDSB:-HEADER', count: 1 }
        - { idRef: ':USRDSB:-DETAIL-RECORD', count: '>1' }
  
segments: 
- id: ':USRDSB:-SOURCE'
  values: 
  - { name: ':USRDSB:-COMMENT-IND', type: String, length: 1 }
  - { name: 'FILLER1', usage: U, type: String, length: 1 }
  - { name: ':USRDSB:-SOURCE', type: String, length: 6, value: 'SOURCE' }
  - { name: 'FILLER2', usage: U, type: String, length: 1 }
  - { name: ':USRDSB:-SOURCE-ID ', type: String, length: 8, value: 'MULESOFT' }
- id: ':USRDSB:-HEADER'
  values: 
  - { name: ':USRDSB:-COMMENT-IND', type: String, length: 1 }
  - { name: 'FILLER1', usage: U, type: String, length: 1 }
  - { name: ':USRDSB:-HEADER', type: String, length: 6, value: 'HEADER' }
  - { name: 'FILLER2', usage: U, type: String, length: 1 }
  - { name: ':USRDSB:-TRANSMISSION-ID', type: String, length: 8, value: 'TBC' }
  - { name: 'FILLER3', usage: U, type: String, length: 1 }
- id: ':USRDSB:-DETAIL-RECORD'
  values: 
  - { name: ':USRDSB:-COMMENT-IND', type: String, length: 1 }
  - { name: 'FILLER1', usage: U, type: String, length: 1 }
  - { name: ':USRDSB:-FIELD-IND', type: Integer, length: 6, format: { justify: ZEROES, sign: UNSIGNED }}
  - { name: 'FILLER2', usage: U, type: String, length: 1 }
  - { name: ':USRDSB:-CARD-NBR', type: String, length: 19 }
  - { name: 'FILLER3', usage: U, type: String, length: 1 }
  - { name: ':USRDSB:-CUST-IND', type: String, length: 1 }
  - { name: 'FILLER4', usage: U, type: String, length: 1 }
  - { name: ':USRDSB:-FIELD-DATA', type: String, length: 80 }
  - { name: 'FILLER5', usage: U, type: String, length: 9 }

Input:

[
  {
        "Statement_Message_ID__c": "15673",
        "ActivityDate": "2024-01-02T17:00:00.000Z",
        "Option_Set__c": "801",
        "TSYS_ID__c": "12346849",
        "Id": null,
        "type": "TransactionJournal",
        "ASC__c": "V01",
        "CDF23__c": "1"
    },
    {
        "Statement_Message_ID__c": "15839",
        "ActivityDate": "2024-01-02T17:00:00.000Z",
        "Option_Set__c": "813",
        "TSYS_ID__c": "12347015",
        "Id": null,
        "type": "TransactionJournal",
        "ASC__c": "V01",
        "CDF23__c": "1"
    }
]

Expected output:

  SOURCE MULESOFT
  HEADER NNNNSCRB
  019303 12346849352
  008902 1234684902  
  011206 12346849V02 
  014205 12346849F0015
  019303 12347015353
  008902 1234701503
  011206 12347015V03
  014205 12347015F0015

Currently, it is creating only one record

  SOURCE MULESOFT
  HEADER SPLNSCRB
  019303 12346849            352
  008902 12346849            02  
  011206 12346849            D 15673  
  014205 12346849            F0015
  019303 12347015            353
  008902 12347015            03
  011206 12347015            V03
  014205 12347015            F0015

What am I missing in the dataweave script?


Solution

  • The schema seems to be missing a structure. In the structure you can set the detail record to have multiple records so it can receive an array of records. Then the output keys need to match the schema names.

    Schema with structure:

    form: COPYBOOK
    structures:
    - id: 'main'
      name: Main record
      data:
      - { idRef: ':USRDSB:-SOURCE' }
      - { idRef: ':USRDSB:-HEADER' }
      - { idRef: ':USRDSB:-DETAIL-RECORD', count: '>1' }
    
    segments: 
    - id: ':USRDSB:-SOURCE'
    ...
    

    Note that I set the missing value to 'spaces' for better readability. I removed the structureIdent setting.

    Script:

    %dw 2.0
    output application/flatfile schemaPath = "schemas/schema.ffd" , missingValues="spaces"
    ---
    {
      
        ":USRDSB:-SOURCE": {
            ":USRDSB:-COMMENT-IND": "A"
        },
        ":USRDSB:-HEADER": {
            ":USRDSB:-COMMENT-IND": "B"
        },
        ":USRDSB:-DETAIL-RECORD": payload map ( payload01 , indexOfPayload01 ) -> {
            ":USRDSB:-COMMENT-IND": "1",
            ":USRDSB:-FIELD-IND": 6111 as Number,
            ":USRDSB:-CARD-NBR":"15151515151515151" ,
            ":USRDSB:-CUST-IND": "D",
            ":USRDSB:-FIELD-DATA": "TETETETTETETETETET"
        }
    }
    

    Input:

    [1,2,3,4,5]
    

    Output:

    A SOURCE MULESOFT
    B HEADER TBC
    1 006111 15151515151515151   D TETETETTETETETETET
    1 006111 15151515151515151   D TETETETTETETETETET
    1 006111 15151515151515151   D TETETETTETETETETET
    1 006111 15151515151515151   D TETETETTETETETETET
    1 006111 15151515151515151   D TETETETTETETETETET
    

    In your application you'll want to replace the fixed values by values coming from the payload.