Boomi Mapping - User Defined function based on list of elements in a collection
Recently I came across a requirement to validate all the elements in a collection and return an output based on that. Let me explain it with an example. I have a XML document containing information about users including a collection to indicate the roles of the person.
Source Document
Destination Document
I want to map this into a destination profile which has elements to indicate whther the user is an admin or a subsriber.
Source Document
<users> <user> <first_name></first_name> <roles> <role>admin</role> <role>subscriber</role> <role>creator</role> </roles> </user> </users>
Destination Document
I want to map this into a destination profile which has elements to indicate whther the user is an admin or a subsriber.
<users> <user> <isAdmin>true</isAdmin> <isSubscriber>true</isSubscriber> <user> <users> </users>
In order to achieve this I have to write a map funtion. But for a map funtion, we can't input a collection. If we map the "roles" collection into the map fuction, it creates multiple output documents, one per element in the collection.
So I came up with a solution with the help of dynamic document property. Here, all the elements in the collection are concatanated into a single string while using the dynamic document property as a
temporary variable. Then this dynamic document property is validated to derive relevant elements in the destination profile.
Function 1 - Accumulating all the values in the collection into a single property.
- Write a User define funtion with "role" as the input (The individual element inside the collection).
- Add a function step with a scripting function (Javascript). This should take 2 inputs.
- new_role - contains single role.
- all_roles - contains comma separated roles.
- The script should concatanate new_role into all_roles. Then return the result - all_roles_result.
- Add the second function step to set a dynamic document property (DDP_roles) to hold roles result. Map the output of above function step into this one.
- Now add the third function step to get above document property(DDP_roles) and map that into "all_roles" input of scripting function step.
User Defined Function 1
What above will do is, accumulate all the values in the collection into a dynamic document property as a comma separated string.
Function 2 - Validate the property containing all the values
- Write a user deine function with no input.
- As the first step get the dynamic document proerty abdefined above "DDP_roles"
- Add another step for Scripting with one input variable - roles_list. Map above property as the input into it.
- Validate the roles_list using Javascript and return isAdmin and isSubscriber as output.
- Now above output variables can be mapped to the destination profile.
User Defined Function 2
Enable map function ordering
Since Function 1 must run before the Function 2, we have to enable ordering of them. For that, click on "Order the function execution" option in the top right corner of the functions
section in the mapcomponent. Then enable "Enable map function ordering?" option and make sure order is correctly listed.
Map Component with 2 functions |
Split documents
If you have multiple records in the XML document (multiple elements), it will accumulate the document property with values in all the user records. In order to stop that, you have to process send each user record separately into the Map. So we have to place "data-process" shape and split the document by users/user element before the map shape.
Split Document configuration |
Place Data Process shape before the Map |
Thank you!
Comments
Post a Comment