Working with blocks in scripts
A Block-type property is a composite object property that includes several properties combined in a table. In Web Application, a Block property is displayed as a table, whose columns are the block properties. Each block entry is a block object instance, i.e. each block entry is an independent object, which has a parent to which the entry belongs.
If you need to add a new block item, use the method context.Block.Add(item). In this case, the item will be marked as added and when saving the context (clicking Actions - Save in a business process task or going to the next step) the data will be saved automatically.
If you need to delete a block item, use the method context.Block.Remove(item).
If you need to save added block items with a script, save the parent object, e.g. in a business process task it is the process context context.Save(). If you change a block in a context variable object instance, save the object instance. For example, if a block is in the Legal Entity object:
var items = context.LegalEntity.Block.ToList();
foreach(var item in items)
{
item.String = "0";
}
context.LegalEntity.Save();
If you need to delete a block item, read this article.
- Script on a button:
Task: perform actions with two numbers (sum up, multiply, divide) and write the result to a block.
In this case, a Result block with two properties will be created:
1) Result (Fraction type);
2) Action with numbers (String type).
The properties Number 1 (Fraction), Number 2 (Fraction) and Action (Drop-Down List, with values Sum Up, Subtract, Multiply) were added to the form. Also, a button with a script was added. As the result of the script execution, the block will be filled in.
Script:
public virtual void Calculate (Context context, EleWise.ELMA.Model.Views.FormViewBuilder<Context> form)
{
if (context.ActionWithNumbers != null) {
switch (context.ActionWithNumbers.Value) {
case "Sum Up": {
var result = InterfaceActivator.Create<P_CreateUsers_Result> ();
result.Result = context.Number1 + context.Number2;
result.ActionWithNumbers = "Sum";
context.Result.Add (result);
break;
}
case "Subtract": {
var result = InterfaceActivator.Create<P_CreateUsers_Result> ();
result.Result = context.Number1 - context.Number2;
result.ActionWithNumbers = "Remainder";
context.Result.Add (result);
break;
}
case "Multiply": {
var result = InterfaceActivator.Create<P_CreateUsers_Result> ();
result.Result = context.Number1 * context.Number2;
result.ActionWithNumbers = "Product";
context.Result.Add (result);
break;
}
}
}
}
Script for changing a block value:
You can find an example of a script for changing a block value here
Script for changing a block property value:
You can find an example of a script for changing a block property value here
Script for changing a form property value:
The script for changing a form property value is similar to the script on a button, described above.
A simple script in a Script activity:
Task: Set property values for all the block items when executing a script in a Script activity.
In this example, a Product List block with two properties was created:
1) Name (String type);
2) In Stock (Yes/No type).
In the first business process task, a user fills out the block manually. A Script activity is placed after this task. The script sets the False value in the In Stock property for all the block items.
Script:
public virtual void deleteAllItems (Context context)
{
var items = context.ProductList;
foreach(var item in items)
{
item.InStock = false;
}
}