W050 – Routing
What you will learn ?
How to create different end to an activity in a business process
STEP 1: Prerequisites
You must first create all the transitions from one activity to all possible destinations.
Exercise:
Add an activity CreateAddress
in the AppCreateCustomer
process using the wizard:
- Code =
APPCC-ADDRESS
- Name =
CreateAddress
- Type = New
- Irreversible
- User dialog = Yes
- Module = MyModule
then:
- Translate by
Create a main address
with enter an address for the customer - Give the
write permission
toMyAdminGroup
group - From
CustomerEntry
and toInterest
- Data: Object Name =
AppAdresse
Place the activity on the process diagram and save.
STEP 2: Set condition
On each transition, a condition must be set by a code that will return the business (given Return.Code) to go to that direction.
Exercise:
- Double-click on the transition going to the creation of the address
- In the transitional form, write
EmptyAddress
in the Condition field - Save
- The condition (
EmptyAddress
) appears on the diagram above the link between the two activities
STEP 4: Return code activity
Then you will have to set the value of the Return.Code activity data There are several ways to set this value :
- By script or code on the postValidate hook which is called after the validation of an activity and before choosing the path to continue
- By setting the Return.code of a given activity (Group = Return, data = Code), the value is in this case a data activity or data process (ex. for an activity related to a business object:
[<step>.Field.<field name>]
)
Exercise:
At the exit of CustomerEntry
, we want to test if the customer has an address. If not we redirect to the activity of creating a new address. By code:
- Edit Script process
- Add the following code:
AppCreateCustomer.postValidate = function(context) { console.log("BEGIN postValidate"); var step = context.getActivity().getStep(); if (step=="APPCC-CLIENT") { var adresseId = context.getDataFile("Field", "cliAdresseFK", true).getValue(0); console.log("adresseId=["+adresseId+"]"); if (!adresseId || adresseId.equals("")) { console.log("empty address "); context.setDataFile("Return", "Code", "EmptyAddress"); } } console.log("END postValidate"); };
Code Explanation:
- The postValidate hook takes the context of the current activity of the process and the step of the process.
- If the step is creating the customer, we get the reference value of the address
- If this reference is not set, it forces the return code to
EmptyAddress
which is the condition that goes to theCreateAddress
activity
(If nothing is specified the next activity is Interest because the condition is set to[DEFAULT]
)
Option:
- At the definition of
AppClient
object, create a hidden calculated fieldcliMainAddress
which would be equal toEmtyAddress
if the address is missing - And the definition of
CustomerEntry
activity, add the data Return.Code =[APPCC-CLIENT.Field.cliMainAddress]
STEP 5: postValidate hook
The postValidate hook of a process can act:
- After validation of the activity: the activity is complete and saved
- Before choosing a path for the future activity: very useful to give a value to a Return.Code corresponding to a condition transition
- To add a specific treatment on the activity before going to the next activity
Exercise:
After the CreateAddress
activity, associate the new address to the previously created customer.
- Edit
AppCreateCustomer
process script - Update the code of the postValidate hook with the following:
AppCreateCustomer.postValidate = function(context) { console.log("BEGIN postValidate"); var step = context.getActivity().getStep(); if (step=="APPCC-CLIENT") { var adresseId = context.getDataFile("Field", "cliAdresseFK", true).getValue(0); console.log("adresseId=["+adresseId+"]"); if (!adresseId || adresseId.equals("")) { console.log("empty address "); context.setDataFile("Return", "Code", "EmptyAddress"); } }else if (step=="APPCC-ADDRESS") { var a = this.getActivity("APPCC-CLIENT"); var af = this.getContext(a); var clientId = af.getDataFile("Field", "row_id", false).getValue(0); var c = this.getGrant().getTmpObject("AppClient"); c.resetFilters(); if (c.select(clientId)) { var adresseId = context.getDataFile("Field", "row_id", true).getValue(0); console.log("update clientId="+clientId+" adresseId="+adresseId); c.getField("cliAdresseFK").setValue(adresseId); c.save(); } } console.log("END postValidate"); };
Code Explanation:
When the step is the address creation:
- Find the reference field of the customer created in the APPCC-CLIENT activity
- Select a temporary instance of the object
AppClient
- Change the reference field address to the one that has just been created
- Save the customer to update its address
W040 – Redirection to specific page
Redirect to a specific page at the end of the business process