Showing posts with label Conditional Assign in SOA. Show all posts
Showing posts with label Conditional Assign in SOA. Show all posts

Tuesday, July 29, 2014

Groovy script in Script Task in Oracle BPM

Groovy script in ScriptTask/Activity in Oracle BPM


As specified in my previous post , you can add groovy script for a script activity in Oracle BPM 12c.

Using this, you can easily use if else, nested if, switch case etc, in  ScriptActivity.
You can avoid using multiple gate-ways in the flow by adding all the necessary conditions in groovy scripts.

In this example I will show a simple if else and a switch case.


You will see a new option "Go To Script" in BPM Process script activity. On clicking this, you will be shown a page where you can add your groovy script based on your use-case.



Adding couple of sample scripts here:

 


 

The use-cases above are self explanatory, you could add multiple conditions based on your needs.

Shoot me in the comments, if you need any help regarding this.


Sample project can be downloaded from here

Follow the blog to get to know about new features in Oracle BPM 12c.


Wednesday, June 18, 2014

IF Else in Assign/Script Activity in SOA/BPM

If else conditional assign in Assign or Script Activity of SOA or BPM

Note: It's not a complete solution. however, you can use this method in most of the cases.


Use Case:

Assume an input string which can have value "Submit" or "Cancel" and based on this you want to output string to be either "APP" or "REJ"

if  input_arg = "Submit"
Then
output_arg = "APP"
Else
output_arg = "REJ"


General way of giving a solution for this to write an XSLT or use switch/exclusive gateway and have 2 Assign/Script tasks to assign the appropriate value to the output element.

Here in this example, I will use "translate" function to achieve the same without gateways and in a single Assign/Script Task.



The xpath expression used here is:

translate(string(contains(bpmn:getDataObject('projectDataObject1'),"Submit")),"trufalse","APPREJ")

Lemme split the function and explain it in detail.

1. contains(bpmn:getDataObject('projectDataObject1'),"Submit")  :  This is obvious, checking whether the input argument has the string "Submit". If yes, the function returns "true" else it returns "false"

2. string(contains(bpmn:getDataObject('projectDataObject1'),"Submit")) : Converting the above returned Boolean "true or false" into a string.

3. translate(string(contains(bpmn:getDataObject('projectDataObject1'),"Submit")),"trufalse","APPREJ") : This is the important part, translate function does replacement each character by character.
So, in this above statement  
string(contains(bpmn:getDataObject('projectDataObject1'),"Submit"))  is the input argument to "translate" function

"trufalse"  Indicates what characters to be replaced. This is the 2nd argument.
 "APPREJ"  Indicates with which characters they need to be replaced. This is the 3rd argument to the function.
 "t" would be replaced with "A"
 "r" would be replaced with "P"
"u" would be replaced with "P"
 "f" would be replaced with "R"
so... on

The last letter "e" doesn't have the corresponding replacement element. So, that will be ignored. 
Even "true" has "e" in it, but I didn't mention it explicitly, since we need to mention each character only once. 


So, finally summing up, based on the input argument containing "Submit" or not, the output argument will be updated to "APP" or "REJ".


This might be little confusing :-)

To help you understand more, here are few examples on how the translate function works:

translate("AABBCDBA","ABCD","XYZW") AABBCD is the input and output would be XXYYZWYX
Each character is replaced appropriately.

translate("ABC123DDC","ABCD32154","abcd00000") :   ABC123DDC is the input here and output would be abc000ddc


You can have pretty complicated xpath's to achieve nested if's too. Example here:

translate(string(not(
contains(bpmn:getDataObject('applicationDataDO')/ns:ApplicationStatus,'REJ'))),
 'trufalse',
concat(translate(string(
contains(bpmn:getDataOutput('outcome'),'REJECT')),'trufalse','REJSUB'),'REJ'))
  

You can literally replace any character with any character. Just make sure what you are doing. :-)


Oracle BPM 12c supported much easier way of nested if's in Script tasks, I will post on a blog in it soon. Keep watching !


Shoot me in the comments, if you need any further help.