Search code examples
dataweavemulesoft

Dataweave 2.0 withMaxSize function


I've transformation in external dwl file. I'm trying to use 'withMaxSize' to limit the size of string. But I'm getting below issue.

fun providerObjMapping(payload) = (if(! isEmpty(payload.ProviderUniqueID) ) {

  providerUniqueID:payload.ProviderUniqueID dw::core::Strings::withMaxSize 3,
  npi:payload.NPI,
  salutation:payload.Salutation,
  firstName:payload.FirstName,
  middleName:payload.MiddleName

  }else {})

""Script '%dw 2.0 import * from dw::appian_crf_mapping

output application/json
---
generateObj(vars.tableName, payload) ' has errors:


    Unable to resolve reference of dw::core::Strings::withMaxSize. at 29 : 29" evaluating expression: "%dw 2.0
import * from dw::appian_crf_mapping

output application/json
---
generateObj(vars.tableName, payload)"."

Below are the ways I've tried.

  1. providerUniqueID:payload.ProviderUniqueID dw::core::Strings::withMaxSize 3 - Not Working

  2. fun maxLength(data) = data dw::core::Strings::withMaxSize 3 providerUniqueID:maxLengh(payload.ProviderUniqueID) - not Working

  3. import * dw::core::Strings fun maxLength(data) = data withMaxSize 3 providerUniqueID:maxLengh(payload.ProviderUniqueID) - not working

Runtime: 4.2.1enter image description here


Solution

  • According to MuleSoft documentation, withMaxSize was introduced in DataWeave 2.3.0, supported by Mule Runtime 4.3.0 or later (https://docs.mulesoft.com/mule-runtime/4.3/dw-strings-functions-withmaxsize)

    As you are using Mule Runtime 4.2.1, you could implement the withMaxSize function as:

    %dw 2.0
    output application/json
    
    fun withMaxSize(val, maxSize) = val[0 to (((min([sizeOf(val), maxSize])) as Number) - 1)]
    ---
    withMaxSize(payload.message, 3)