Search code examples
muledataweavemulesoftmule4

dataweave 1.0 to 2.0 migration in mulesoft


I'm trying to convert the below dataweave from 1.0 to 2.0, but everything I've tried gives the following errors like evaluating-expression Error, Invalid input "reduce" and replace.

Mule 3 Dataweave 1.0:

%dw 1.0
%output application/xml
%var basicPolicy = payload.DTOApplication.DTOBasicPolicy
%var insuredInfo = payload.DTOApplication.DTOInsured
%var insuredPrimaryAddr = insuredInfo.*PartyInfo[?($.@PartyTypeCd == "InsuredParty")].*Addr[?($.@AddrTypeCd == "InsuredPrimaryBusAddr")][0]
%var lineLiability = payload.DTOApplication.*DTOLine[?($.@StatusCd == "Active" and $.@LineCd == "Liability")][0]
%var lineProperty = payload.DTOApplication.*DTOLine[?($.@StatusCd == "Active" and $.@LineCd == "CommercialProperty")][0]
%var lineProductLiability = payload.DTOApplication.*DTOLine[?($.@StatusCd == "Active" and $.@LineCd == "ProductLiability")][0]
%var primaryClassTotalExposure = lineLiability.*DTORisk[?($.@Status == "Active" and $.@TypeCd == "Exposure")].*DTOGLClass[?($.@GLClassCode == primaryExposure)].@Exposure default [] reduce ((val,acc=0) -> acc + (val replace "\$" with "" replace "," with "") as :number)
%var primaryClassPLTotalExposure = lineProductLiability.*DTORisk[?($.@Status == "Active" and $.@TypeCd == "Exposure")].*DTOGLClass[?($.@GLClassCode == primaryExposurePL)].@Exposure default [] reduce ((val,acc=0) -> acc + (val replace "\$" with "" replace "," with "") as :number)
---
{
    
    Policy: {
        PolType: basicPolicy.@SubTypeCd default "",
        Zip: insuredPrimaryAddr.@PostalCode[0..4] default "",
        EffDate: basicPolicy.@EffectiveDt default "",
        ExpDate: basicPolicy.@ExpirationDt default "",
        EBCov: lineProperty.@EquipmentBreakdown default "No",
        PolFeeOR2: payload.DTOApplication.*DTOFee[?($.@Status == "Active" and $.@RateDRCFeeInd == "No")][0].@FullTermAmt default "",
        PolFeeOR: (payload.DTOApplication.DTOFeeOverrides.*DTOFeeOverride[?($.@Code == "POLF" and $.@OverrideInd == "Yes")][0].@Amount default "") when payload.DTOApplication.DTOFeeOverrides.*DTOFeeOverride[?($.@Code == "POLF" and $.@OverrideInd == "Yes")][0].@Amount != null
                    otherwise (payload.DTOApplication.*DTOFee[?($.@Status == "Active" and $.@RateDRCFeeInd == "No")][0].@FullTermAmt default "")  when payload.DTOApplication.*DTOFee[?($.@Status == "Active" and $.@RateDRCFeeInd == "No")][0].@FullTermAmt != null
                    otherwise "",
        LiabExp: primaryClassTotalExposure,
        ProdExp: primaryClassPLTotalExposure
    }   
}

Input payload : https://github.com/Manikandan99/rate-dtostep/blob/master/request.xml

Expected output: https://github.com/Manikandan99/rate-dtostep/blob/master/policy_response.xml

I tried dataweave 2.0:

%dw 2.0
output application/xml
var basicPolicy = payload.DTOApplication.DTOBasicPolicy
var insuredInfo = payload.DTOApplication.DTOInsured
var insuredPrimaryAddr = insuredInfo.*PartyInfo[?($.@PartyTypeCd == "InsuredParty")].*Addr[?($.@AddrTypeCd == "InsuredPrimaryBusAddr")][0]
var lineLiability = payload.DTOApplication.*DTOLine[?($.@StatusCd == "Active" and $.@LineCd == "Liability")][0]
var lineProperty = payload.DTOApplication.*DTOLine[?($.@StatusCd == "Active" and $.@LineCd == "CommercialProperty")][0]
var lineProductLiability = payload.DTOApplication.*DTOLine[?($.@StatusCd == "Active" and $.@LineCd == "ProductLiability")][0]
var primaryClassTotalExposure = lineLiability.*DTORisk[?($.@Status == "Active" and $.@TypeCd == "Exposure")].*DTOGLClass[?($.@GLClassCode == primaryExposure)].@Exposure default [] reduce ((val,acc=0) -> acc + (val replace "\$" with "" replace "," with "") as :number)
var primaryClassPLTotalExposure = lineProductLiability.*DTORisk[?($.@Status == "Active" and $.@TypeCd == "Exposure")].*DTOGLClass[?($.@GLClassCode == primaryExposurePL)].@Exposure default [] reduce ((val,acc=0) -> acc + (val replace "\$" with "" replace "," with "") as :number)
---
{
    
    Policy: {
        PolType: basicPolicy.@SubTypeCd default "",
        Zip: insuredPrimaryAddr.@PostalCode[0 to 4] default "",
        EffDate: basicPolicy.@EffectiveDt default "",
        ExpDate: basicPolicy.@ExpirationDt default "",
        EBCov: lineProperty.@EquipmentBreakdown default "No",
        PolFeeOR2: payload.DTOApplication.*DTOFee[?($.@Status == "Active" and $.@RateDRCFeeInd == "No")][0].@FullTermAmt default "",
        PolFeeOR: (payload.DTOApplication.DTOFeeOverrides.*DTOFeeOverride[?($.@Code == "POLF" and $.@OverrideInd == "Yes")][0].@Amount default "") when payload.DTOApplication.DTOFeeOverrides.*DTOFeeOverride[?($.@Code == "POLF" and $.@OverrideInd == "Yes")][0].@Amount != null
                    otherwise (payload.DTOApplication.*DTOFee[?($.@Status == "Active" and $.@RateDRCFeeInd == "No")][0].@FullTermAmt default "")  when payload.DTOApplication.*DTOFee[?($.@Status == "Active" and $.@RateDRCFeeInd == "No")][0].@FullTermAmt != null
                    otherwise "",
        LiabExp: primaryClassTotalExposure,
        ProdExp: primaryClassPLTotalExposure 
    }   
}

Any ideas please on how to write the same in dataweave 2.0?


Solution

  • Adding few more points to @AnuragSharma's comments

    if-else-if

    1. In your dw you have GLClassCode == primaryExposure , primaryExposure is not present in your input payload hence this will result in a null value and you cant use replace function along with null
    2. Since you are trying to cast to number , you can give a default value. Here I have set it to 0.
    3. Added encoding="UTF-8" as required in output

    To test your code I have passed GLClasscode as CANDIS as you had that in your payload.

    Note - > Please check your parenthesis as well.

    %dw 2.0
    output application/xml encoding="UTF-8"
    var basicPolicy = payload.DTOApplication.DTOBasicPolicy
    var insuredInfo = payload.DTOApplication.DTOInsured
    var insuredPrimaryAddr = insuredInfo.*PartyInfo[?($.@PartyTypeCd == "InsuredParty")].*Addr[?($.@AddrTypeCd == "InsuredPrimaryBusAddr")][0]
    var lineLiability = payload.DTOApplication.*DTOLine[?($.@StatusCd == "Active" and $.@LineCd == "Liability")][0]
    var lineProperty = payload.DTOApplication.*DTOLine[?($.@StatusCd == "Active" and $.@LineCd == "CommercialProperty")][0]
    var lineProductLiability = payload.DTOApplication.*DTOLine[?($.@StatusCd == "Active" and $.@LineCd == "ProductLiability")][0]
    var primaryClassTotalExposure = lineLiability.*DTORisk[?($.@Status == "Active" and $.@TypeCd == "Exposure")].*DTOGLClass[?($.@GLClassCode == "CANDIS")].@Exposure default [] reduce ($$++$) replace "\$" with "" replace "," with "" default 0  as Number
    var primaryClassPLTotalExposure = lineProductLiability.*DTORisk[?($.@Status == "Active" and $.@TypeCd == "Exposure")].*DTOGLClass[?($.@GLClassCode == "CANDIS")].@Exposure default []  reduce ($$++$) replace "\$" with "" replace "," with "" default 0 as Number
    ---
    {
        
        Policy: {
            PolType: basicPolicy.@SubTypeCd default "",
            Zip: insuredPrimaryAddr.@PostalCode[0 to 4] default "",
            EffDate: basicPolicy.@EffectiveDt default "",
            ExpDate: basicPolicy.@ExpirationDt default "",
            EBCov: lineProperty.@EquipmentBreakdown default "No",
            PolFeeOR2: payload.DTOApplication.*DTOFee[?($.@Status == "Active" and $.@RateDRCFeeInd == "No")][0].@FullTermAmt default "",
            PolFeeOR: if (payload.DTOApplication.DTOFeeOverrides.*DTOFeeOverride[?($.@Code == "POLF" and $.@OverrideInd == "Yes")][0].@Amount != null)
                            (payload.DTOApplication.DTOFeeOverrides.*DTOFeeOverride[?($.@Code == "POLF" and $.@OverrideInd == "Yes")][0].@Amount default "") 
                    else if (payload.DTOApplication.*DTOFee[?($.@Status == "Active" and $.@RateDRCFeeInd == "No")][0].@FullTermAmt != null)
                        (payload.DTOApplication.*DTOFee[?($.@Status == "Active" and $.@RateDRCFeeInd == "No")][0].@FullTermAmt default "")  
                        else "",
            LiabExp: primaryClassTotalExposure,
            ProdExp: primaryClassPLTotalExposure 
        }   
    }
    

    Output

    <?xml version='1.0' encoding='UTF-8'?>
    <Policy>
      <PolType>CNPK</PolType>
      <Zip>80003</Zip>
      <EffDate>20211117</EffDate>
      <ExpDate>20221117</ExpDate>
      <EBCov>Yes</EBCov>
      <PolFeeOR2/>
      <PolFeeOR/>
      <LiabExp>2000000</LiabExp>
      <ProdExp>0</ProdExp>
    </Policy>