Adsense

December 05, 2020

splitBy and joinBy function dataweave2

 Hello friends, 

In this article I want show you the uses of two very common dataweave functions splitBy and joinBy. 

These two functions are very useful while doing transformation with Array and Strings. 

Here, we will see how we can use these dataweave functions converting data from String to Array and vice versa.

splitBy

Splits a string into a string array based on a value of input or matches part of that input string. It filters out the matching part from the returned array.

A string or a Java regular expression used to split the string. If it does not match some part of the string, the function will return the original, unsplit string in the array.

splitBy(String, Regex): Array<String>

Example 1:
------------------------------
Input
Hello World
------------------------------
Dataweave 2.0 Expression
------------------------------
%dw 2.0
output application/java
---
"Hello World" splitBy(/\s/)
------------------------------
Output
[Hello, World]

splitBy(String, String): Array<String>


Example 2:
------------------------------
Input
Manish Kumar
------------------------------
Dataweave 2.0 Expression
------------------------------
%dw 2.0
output application/java
---
payload splitBy(" ")
------------------------------
Output
[Manish, Kumar]


The regex/separator can match any character in the input. splitBy performs the opposite operation of joinBy.


joinBy

joinBy function Merges an array into a single string value and uses the provided string as a separator between each item in the list. 

You can use joinBy Dataweave 2.0 function to convert array to string.

Example 3:
------------------------------
Dataweave 2.0 Expression
------------------------------
%dw 2.0
output application/json
---
{ "joinName" : ["Manish","Kumar"] joinBy "-" }
------------------------------
Output
{
  "joinName": "Manish-Kumar"
}

joinBy performs the opposite task of splitBy.


Happy Learning :)


Read More »

December 01, 2020

Array comparison use case Dataweave2

Hello friends,

In this article we will see one of the common and challenging real-time use-case in IT industry during application development where we require to compare between arrays. These arrays could be from two different source or api responses.

Here we will see how we can compare two arrays results/payloads and get required results. Also we will see the complete example of our use-case.

To achieve this we will see how we can use the combination of some dataweave functions and how we can utilise these functions for our use-case.

  • flatMap
  • filter
  • contains
  • partation


There are many ways to achieve these functionality. Here, I have tried to show you two different methods to achieve this.


Method I:


flatMap

flatMap(Array<T>, (item: T, index: Number) -> Array<R>): Array<R>
It comes under dw::Core package from dataweave library.

Iterates over each item in an array and flattens the results. It produces output as Array. 

Instead of returning an array of arrays (as map does when you iterate over the values within an input like [ [1,2], [3,4] ]), flatMap returns a flattened array that looks like this: [1, 2, 3, 4]. flatMap is similar to flatten, but flatten only acts on the values of the arrays, while flatMap can act on values and indices of items in the array.

%dw 2.0
output application/json indent=false
---
[ ["a":1,"b":0], ["c":0.1,"d":7] ] flatMap (value, index) -> value
-------------------------------------------------------------------
Output:
[{"a": 1},{"b": 0},{"c": 0.1},{"d": 7}]


filter

filter(Array<T>, (item: T, index: Number) -> Boolean): Array<T>

It comes under dw::Core package from dataweave library. It produces output as array.

Iterates over an array and applies an expression that returns matching values. The expression must return true or false. If the expression returns true for a value or index in the array, the value gets captured in the output array. If it returns false for a value or index in the array, that item gets filtered out of the output. If there are no matches, the output array will be empty.


%dw 2.0
output application/json
---
[9,2,3,4,5] filter (value, index) -> (value > 4)

-------------------------------------------------
Output:
[
9,
5
]


Complete example below by using flatMap and filter function:



%dw 2.0
output application/json
var filterCriteria = [
{
"IDENTITY": "D40000",
"NM": "Delta",
"CODE": "D12"
},
{
"IDENTITY": "C30000",
"NM": "Charlie",
"CODE": "C11"
}
]
var responsePayload= [
{
"CODE": "A11",
"NAME": "Alpha",
"ID": "C10000"
},
{
"CODE": "B12",
"NAME": "Bravo",
"ID": "B20000"
},
{
"CODE": "C11",
"NAME": "Charlie",
"ID": "C30000"
},
{
"CODE": "D12",
"NAME": "Delta",
"ID": "D40000"
},
{
"CODE": "E12",
"NAME": "Echo",
"ID": "E50000"
}
]
---
filterCriteria flatMap(v) -> (
responsePayload filter (v.IDENTITY == $.ID and v.NM == $.NAME and v.CODE == $.CODE)
)
---------------------------------------------------------------------------------------------------------------
Output:
[
{
"CODE": "D12",
"NAME": "Delta",
"ID": "D40000"
},
{
"CODE": "C11",
"NAME": "Charlie",
"ID": "C30000"
}
]

----------------------------------------------------------------------------------------------------------------------

Method II:


Now, let use see the 2nd way to compare array by using contains and partition function. Here the input arrays can be from two different source or api responses. Let's see how we can compare and filter the incoming arrays by using these two useful function.

contains

contains(Array<T>, Any): Boolean

It comes under dw::Core package from dataweave library. It returns true if an input contains a given value, false if not.

This version of contains accepts an array as input. Other versions accept a string and can use another string or regular expression to determine whether there is a match.


partation

partition(Array<T>, (item: T) -> Boolean): { success: Array<T>, failure: Array<T> }


It comes under dw::core::Arrays package from dataweave library.

It separates the array into the elements that satisfy the condition from those that do not.
Elements that satisfies conditions will come under "success" array partation and which it don't satisfy comes "failure" array partation. So, you can retrieve the matching results under "success" partation node.


Note: Introduced in DataWeave 2.2.0. Supported by Mule 4.2 and later.


%dw 2.0
import partition from dw::core::Arrays
output application/json
var responsePayload = [
{
"CODE": "A11",
"NAME": "Alpha",
"ID": "C10000"
},
{
"CODE": "B12",
"NAME": "Bravo",
"ID": "B20000"
},
{
"CODE": "C11",
"NAME": "Charlie",
"ID": "C30000"
},
{
"CODE": "D12",
"NAME": "Delta",
"ID": "D40000"
},
{
"CODE": "E12",
"NAME": "Echo",
"ID": "E50000"
}
]

var filterCriteria = [
{
"IDENTITY": "D40000",
"NM": "Delta",
"CODE": "D12"
},
{
"IDENTITY": "C30000",
"NM": "Charlie",
"CODE": "C11"
}
]
---
responsePayload partition (e) -> filterCriteria contains {IDENTITY:e.ID,NM:e.NAME,CODE:e.CODE}
--------------------------------------------------------------------------------------------------------------------------

Output
{
"success": [
{
"CODE": "C11",
"NAME": "Charlie",
"ID": "C30000"
},
{
"CODE": "D12",
"NAME": "Delta",
"ID": "D40000"
}
],
"failure": [
{
"CODE": "A11",
"NAME": "Alpha",
"ID": "C10000"
},
{
"CODE": "B12",
"NAME": "Bravo",
"ID": "B20000"
},
{
"CODE": "E12",
"NAME": "Echo",
"ID": "E50000"
}
]
}

You can choose any of these methods while doing comparison of arrays for your use-cases.

Happy Learning :)
Read More »

May 17, 2020

Dataweave useful String functions-Part1

Hello friends, In this article we will see some common dataweave String functions from String core package(dw::core::Strings)  along with their use-cases.


To use these functions in dataweave scripts we need to add/import the module header as below.

you can either import complete package by using * or if you want to use specific function you can use just function name in place of placeholder as given below <functionName>

%dw 2.0

import * from dw::core::Strings or import <functionName> from dw::core::Strings 

output application/json

---

{}


Here, we will see 4 common String functions and their usage.

  • capitalize(String): String
  • camelize(String): String
  • dasherize(String): String
  • underscore(String): String 


capitalize(String): String


This function accepts input as String and returns results as String. It capitalizes the first letter of each word in a string. It also removes underscores between words and puts a space before each capitalized word.

%dw 2.0

import capitalize from dw::core::Strings
output application/json
---
{
"a" : capitalize("manish"),
"b" : capitalize("mr_manish_kumar"),
"c" : capitalize("manish-KUMAR"),
"d" : capitalize("manishKumar")
}

Output:
{
"a": "Manish",
"b": "Mr Manish Kumar",
"c": "Manish Kumar",
"d": "Manish Kumar"
}


camelize(String): String


This function accepts input as String and returns results as String.  It returns a string in camel case based on underscores in the string. All underscores are deleted, including any underscores at the beginning of the string.


%dw 2.0
import camelize from dw::core::Strings
output application/json
---
{
"a" : camelize("1_manish"),
"b" : camelize("mr_manish_kumar"),
"c" : camelize("_manish_kumar")
}

Output:
{
"a": "1Manish",
"b": "mrManishKumar",
"c": "manishKumar"
}

dasherize(String): String


This function accepts input as String and returns results as String. It replaces spaces, underscores, and camel-casing in a string with dashes (hyphens). If no spaces, underscores, and camel-casing are present, the output will match the input. 

%dw 2.0

import dasherize from dw::core::Strings
output application/json
---
{
"a" : dasherize("Manish"),
"b" : dasherize("Manish_first_name"),
"c" : dasherize("Manish Kumar"),
"d" : dasherize("ManishKumar")
}

Output:
{ "a": "manish", "b": "manish-first-name", "c": "manish-kumar", "d": "manish-kumar" }



Above function works based String input containing underscore. Below underscore function will help you to revert String  to underscore format. 


underscore(String): String 


This function accepts input as String and returns results as String. It replaces hyphens, spaces, and camel-casing in a string with underscores. If no hyphens, spaces, and camel-casing are present, the output will match the input. Also it converts string into lower case.

%dw 2.0
import underscore from dw::core::Strings
output json
---
{
"a" : underscore("Manish"),
"b" : underscore("Manish-first-name"),
"c" : underscore("Manish KUMAR"),
"d" : underscore("ManishKumar")
}

Output:
{ "a": "manish", "b": "manish_first_name", "c": "manish_kumar", "d": "manish_kumar" }

Also, If you want to use multiple functions in your transformation
you can import with either * or comma separated as example below:

%dw 2.0
import dasherize,underscore,capitalize,camelize from dw::core::Strings
output json
---
{
"a" : capitalize("manish_kumar"),
"b" : underscore("Mr-Manish-Kumar"),
"c" : dasherize("Manish KUMAR"),
"d" : camelize("manish_kumar")
}

Output:
{ "a": "Manish Kumar", "b": "mr_manish_kumar", "c": "manish-kumar", "d": "manishKumar" }

Note:: dasherize, underscore will convert result strings into lower case.


Happy Learning :)
Read More »

April 15, 2020

Salesforce Process Builder

Salesforce Process Builder
Process Builder is a tool provided by Salesforce to automate your business process via point and click.For Example:If Amount has crossed its threshold(1000) then send a notification to the sales representative with the details.


Actions Available In Process Builder
There are several different actions you can trigger the process builder. These actions are:
  • Trigger Apex code: You can use the Salesforce process builder to invoke Apex code you have written within Salesforce. Apex can be anything from custom logic to save a record to complex business processes. To invoke an apex class in process builder we have to use @InvocableMethod attribute.Example:There is a Apex class named CampingListController having a method defined as @InvocableMethod. Hence we can call this method from Process builder as shown below:
    Calling Apex from Process Builder
  • Create a record: This will allow you to create new records and set certain field values for the new record.Example: When an opportunity is 'Closed Won' then create a case for the sales team.
    New Case Creation
  • Email alerts: Once a criteria is met then you can send a notification to pre defined user. To do so you first create an email alert and associate it with an existing Email Template. Email Template will be having the format in which recipients will get the message.Example: If Opportunity is 'Closed Won' and Account's industry is Banking then send an email alert to users.
    Process Builder to send email alert
  • Trigger a flow: You can launch a flow from your process to automate complex business processes.Only active autolaunch flows can be launched from Process Builder. It can launch active flows which do not contain screens.Example: If Opportunity is 'Closed Won' and Account's industry is Banking then launch a flow for his/her experience on the Opportunity journey.
  • Post to Chatter: Process builder post to chatter action helps to post information to any user or group chatter feed within Salesforce. The post will appear in the chatter field as if the person who triggered the process had written it. You can reference groups or topics and add merge fields.Example: When a case status is changed, post to chatter so that user knows that some action has been taken on the case.
    Process Builder posting to Chatter
  • Submit for approval: Only the record that started the process will be submitted. You can’t submit any related records for approval.
  • Update records: Update one or more records that are related to the record that started the process. You can update the record with manually entered values or by using the values from related records. We can update records of parent or child whereas workflow only updates same or parent object from the child.
  • Quick actions: You must already have global actions or an object specific action created within Salesforce to use these quick actions. You can then select to log a call, send an email, or update a record.Example: A contact will be created for Camping Item once the flow returns a CampList item.
    Quick Action via Process Builder
  • Process: This action will call another process to another process. For this action, you need to choose process type as it invoked by another process.You can invoke processes with objects that share at least one unique ID. For example, in the Account and Case objects, the AccountId field is unique to Account and also used by Case. You can create an invocable process that updates a Case record. Then you can invoke it from:
           A process that updates an Account record’s owner
               A process that adds an Account shipping address or updates it


Read More »

April 05, 2020

Munit validation for Multipart message - Mule4


In this article, I will try to show how we can write and execute MUnit validation for Multipart messages.

Mulesoft provide functions for creating MultiPart and formats and parts (including fields and boundaries) of MultiPart formats.

Below header needs to be added while using dataweave scripts to create multipart messages.

%dw 2.0
import dw::module::Multipart
output multipart/form-data




You will find a github link for sample project at the end of this article.

Now let us understand how we can parse and validate Multipart messages using Munit.

Multipart message formats doesn't provide flexibility for normal traversing to validate the transformation or output. To validate multipart message we need to transform the message with "application/java" format as highlighted below.

%dw 2.0 output application/java input payload multipart/form-data --- payload.parts


Validate size with function sizeOf():




Validate content:




https://github.com/manishkumarsah/multipart-munit-validation-mule4

Happy Learning :)
Read More »