4D v16 R6 introduced a new concept of communication between processes: shared object and shared collection variables! Thanks to this, you’re able to easily share information between processes.
Until now, entity selections weren’t shareable between processes. However, that has changed … we’re pleased to announce that in 4D v18 R5 entity selections are shareable!
Gone are the days of building a list of primary keys to move a selection of entities to another process. Enhance your multi-processes code by including ORDA entity selections as shared objects.
Keep reading to learn more.
HDI: Share ORDA entity selections
principle
With 4D v18 R5, most entity selections are shareable. This means:
- they are non-alterable (does not allow the addition of new entities).
- they can be shared between several processes or workers.
Some entity selections are non-shareable (for example, if they’ve been created with dataClass.newSelection() or Create entity selection()). This means:
- they are alterable (allows the addition of new entities)
- they can’t be shared between several processes
shareable entity selections
Starting with 4D v18 R5, most used entity selections (retrieved with query(), all(), …) are shareable.
Let’s take a look at an example to better understand.
Example
In the example below, we build two entity selections:
- one for paid invoices.
- one for unpaid invoices.
We might want to pass them to a worker to delegate sending:
- acknowledgment emails for paid invoices.
- reminder emails for unpaid invoices.
Here’s the code:
If (Storage.info=Null)
Use (Storage)
Storage.info:=New shared object()
End use
End if
Use (Storage.info)
//Put entity selections in a shared object
Storage.info.paid:=ds.Invoices.query("status=:1"; "Paid")
Storage.info.unpaid:=ds.Invoices.query("status=:1"; "Unpaid")
End use
CALL WORKER("mailing"; "sendMails"; Storage.info)
Here’s the sendMails method:
var $info; $1 : Object
var $paid; $unpaid : cs.InvoicesSelection
var $invoice : cs.InvoicesEntity
var $server; $transporter; $email; $status : Object
//Prepare emailss
$server:=New object
$server.host:="exchange.company.com"
$server.user:="myName@company.com"
$server.password:="my password"
$transporter:=SMTP New transporter($server)
$email:=New object
$email.from:="myName@company.com"
//Get entity selections
$info:=$1
$paid:=$info.paid
$unpaid:=$info.unpaid
//Loops on entity selections
For each ($invoice; $paid)
$email.to:=$invoice.customer.address // email address of the customer
$email.subject:="Payment OK for invoice # "+String($invoice.number)
$status:=$transporter.send($email)
End for each
For each ($invoice; $unpaid)
$email.to:=$invoice.customer.address // email address of the customer
$email.subject:="Please pay invoice # "+String($invoice.number)
$status:=$transporter.send($email)
End for each
As you can see, the worker can deal with the entity selections!
going back and forth between shareable / non shareable entity selections
In some cases, you might have a non shareable entity selection. If you need to share it with another process, just use the new .copy() class function available on entity selection objects.
Here’s an example of just that:
The Form.products is a new empty entity selection got with the newSelection() class function. Thus, it is not shareable and accepts adding an entity.
Form.products:=ds.Products.newSelection()
//Form.product is entered by the user
Form.products.add(Form.product)
If you try to put it “as is” in the $sharedObj shared object, you’ll get an error:
var $sharedObj : Object
$sharedObj:=New shared object()
Use ($sharedObj)
//Error "Not supported value type in a shared object or a shared collection"
$sharedObj.products:=Form.products
End use
Since ORDA thinks about everything, there’s a new .copy() class function available on entity selection objects.
It returns a copy of the given entity selection, so we can ask this copy to be shareable or not.
var $sharedObj : Object
$sharedObj:=New shared object()
Use ($sharedObj)
//Returns a shareable copy of the Form.products entity selection
$sharedObj.products:=Form.products.copy(ck shared)
End use
That’s it!
Of course with this new class function, you can also turn a shareable entity selection into a non shareable one by omitting the optional parameter.
Note the OB Copy command has been also updated:
- it accepts an entity selection as parameter.
- entity selections nested in the object to copy are also copied according to the passed option.
Download the HDI and check the documentation to learn more!