Add and remove records from My Stuff

<< Click to Display Table of Contents >>

Navigation:  Bizagi Studio > Process wizard > Business Rules > Business rules examples >

Add and remove records from My Stuff

When you want to add and remove items to a Stakeholder's My Stuff you can do so by means of an expression.

To relate records to My stuff you must consider if the record to add is already created, and thus must be attached, or it is a new record.

Also consider if the Stakeholder is the sole user owner of the record, of if other Stakeholders can own (see the record in My Stuff) as well.

 

Example

Imagine in a car dealership you need to design a solution where clients can add and remove vehicles from a favorites list. You can do this using an expression to allow adding a record of a vehicle to a collection in My Stuff, and once there, use another rule action to remove the item from My Stuff.

 

What you need to do

1. Create the data model that will support the scenario

2. Enable the entity in My Stuff

3. Create a Search to find vehicles to add to Favorites

4. Create an action to add a vehicle to Favorites

5. Create an action to remove a vehicle from Favorites

 

Create the data model that will support the scenario

The first thing we need to notice is that many customers can add the same car to their favorites, thus, a car can be included as favorite in the list of many customer. Then we cannot have a direct collection from Client to Vehicle, because this would imply ownership. It would not allow the scenario described above where we need several customers being able to select several vehicles.

To be able to model the requirement we will use an intermediate entity between vehicles and clients, and we will call it Favorite vehicles. Both Vehicle and Client entity will have a one-to-many relationship with it.

 

AddMyStuff1

 

 

Enable the entity in My Stuff

With this approach, the entity that we need to link to the Stakeholder's Clients definition is the new Favorite Vehicles entity.

Go to My Stuff and enable the collection to Favorite vehicles in the Always available context.

 

AddMyStuff2

 

Create a Search to find vehicles to add to Favorites

In the Stakeholder create a search to enable searching for vehicles in the project

 

AddMyStuff3

 

Create an action to add a vehicle to Favorites

We need a button to be displayed on the vehicle records when a Client search for them. Thus, we will select the Vehicles entity to be the one where the action is executed.

Then, we select a rule to execute, and create a new one.

 

AddMyStuff4

 

The expression requires adding a new record to the Favorite vehicles entity, linking the Client as well as the vehicle added.

Thus we need to use the function Me.newCollectionItem as shown below.

 

AddMyStuff5

 

var NewFav = Me.newCollectionItem("Addedtofavorites");

NewFav.setXPath("Client",Credential.Stakeholder("Client"));

 

Notice that we only update the Client in the expression because the vehicle record will be automatically injected by Bizagi.

 

Create an action to remove a vehicle from Favorites

We need a button to display in the records of the vehicle that we have included in our Favorites collection. Thus, we will select the Favorite vehicles to be the one where the action is executed.

Then, we select a rule to execute, and create a new one.

 

AddMyStuff6

 

The expression requires deleting an entire record from the Favorite vehicles entity. We need to use the function Me.deleteCollectionItems as shown below.

 

AddMyStuff7

 

var ThisItem = Me.Context.getXPath("Id");

Me.deleteCollectionItems("Client.Favoritevehicles", Me.getXPath("Client.Favoritevehicles[id = "+ ThisItem +"]")