Oracle SOA Suite Online Training

Interested in learning Oracle SOA Suite 12c?
Learn from the author of this blog!
A complete and comprehensive course on the #1 platform on SOA - Oracle SOA Suite

Click here to find the complete course details
Click here to check the first session on Oracle SOA Suite 12c

================================================================================================

If-Else in Assign Activity

There are scenarios often that need if-else conditions in your BPEL. For this, you will usually use a switch or if activities.

But if the condition is so simple, and you do not want to use a switch activity, then you might consider the 'translate' xpath function provided in the Assign activity.

This is how the translate function works

translate(arg1, arg2, arg3)

arg1 : Is your value that you want to operate on
arg2 : What characters are to be picked for replacement
arg3 : Character-by-Character replacement

translate funciton replaces the content character by character.
For example, if you want to replace 1 with A, 2 with B, 3 with C, and so on, your translate function would look like

translate(arg1, 'ABCDEFGH','12345678')
Example 1 :
If arg1 = BDGHFCDGA,
then the result would be 247863471

The above example is just for explanation on how it works, but a real world application would be as follows

lets say you get a requestId from input and you want to check if the request id is null or not, and if not null, result value is Y, otherwise N

Psuedo code :
Step 1
First, you will check if the input contains numbers, and replace all the numbers with 'Y' (you can use any letter for that matter in this step)
    So, if the input is 54695, then after translation, your output will be YYYYY
Step 2
Then, you will check if the result in step 1 contains 'Y' using the 'contains' xpath function
    In this step, your output will be either true or false
Step 3
Next, if the output of the previous step is true, replace the first letter 't' with 'Y' and ignore the rest

Actual Code
(These are mappings in assign activity)
Step 1
translate('../ns0:requestId','0123456789','YYYYYYYYYY') ------> $varReqId
Step 2 & 3
translate(contains($varReqId, 'Y'), 'truefals', 'Y') -----------> $varReqId

Explanation :
Lets say varReqId = 3246
Step 1 result will be YYYY (As 3 is replaced with Y, 2 is replaced with Y, 4 is replaced with Y, 6 is replaced with Y)
Next, Contains (YYYY,Y) results in true
Next, translate(true, truefals,Y) will replace t with Y and ignore the rest.
So, the first letter in true will be replaced with Y and the rest is ignored.

If you are not comfortable with this approach, or if its confusing, you always have the switch or if activity in BPEL, but your BPEL will become huge if the number of such conditions are more.

Hope this helps.

No comments:

Post a Comment