Calculation field: Difference between revisions

m
no edit summary
mNo edit summary
mNo edit summary
Line 1: Line 1:
{{Stamp|2016-12-04|4.1.0|1.0.7}}
{{Stamp|2016-12-10|4.1.0|1.0.7}}


The values in Calculation fields are calculated from the expressions specified by you in Memento Script, defined herein.
The values in Calculation fields are calculated from the expressions specified by you in Memento Script, defined herein.
Line 103: Line 103:
| '''switch''' || expression,<br/>value/result pairs,<br/>default result || 1+(2*#pairs)+1 arguments<br/>The final result based on matching the expression against the paired values to identify the paired result (or else the default result) to be used<br/>Example: switch(#{count}, 1, 'One', 2, 'Two', 'Many')
| '''switch''' || expression,<br/>value/result pairs,<br/>default result || 1+(2*#pairs)+1 arguments<br/>The final result based on matching the expression against the paired values to identify the paired result (or else the default result) to be used<br/>Example: switch(#{count}, 1, 'One', 2, 'Two', 'Many')
|}
|}
== Accessing values of linked library entries ==
Use the following syntax to get access to the values of Link to Entry and other linking fields:
<source lang="java">
#{link field name.linked field name}
</source>
where '''''link field name''''' is the name of a Link to Entry field, '''''linked field name''''' is the name of a field of a related entry.
You can select a field, including one from a related library, using the '''+Field''' button.
If a link field contains several links, then use an index (starting from zero) to get access to them:
<source lang="java">
#{link field name@0.linked field name}, #{link field name@1.linked field name}, …
</source>
You can specify one of the supported aggregation functions instead of the index:
<source lang="java">
#{link field name@sum.linked field name}
</source>
This expression returns the sum of values of field '''linked field name''' for all entries included into '''link field name'''. The functions supported are: '''sum''', '''avg''', '''min''', and '''max'''.
Use the following syntax to get the number of entries in the library referenced by '''link field name''':
<source lang="java">
#{link field name@size}
</source>
=== Example: Parents and children ===
Parents contains fields Name and Age. Children has fields Name and Parents (link to entry (many-to-many)).
Add field "Average parent age" (calculation, real, scale 1) to Children and select the related field from the '''+Field''' list: parents.age. Then insert "@avg", making it:
<source lang="JavaScript">#{parents@avg.age}</source>
=== Example: Orders of products ===
Products has fields SKU, Description, and Price. Orders has fields #, Date, Customer (link to entry (one-to-many)), and Products (link to entry (many-to-many)).
Add field "Total" (calculation, real, scale 2) to Orders and select the related field from the '''+Field''' list: products.price. Then insert "@sum", making it:
<source lang="JavaScript">#{products@sum.price}</source>
'''Note:''' This example points out a deficiency in using a many-to-many relationship for orders of products (or of services). One would hope to associate a quantity for each product ordered, but that requirement cannot be met with this 3-library technique. So, in real life, order solutions generally involve 4 libraries: Customers, Products, Orders, and Order Items. The quantity can reside in Order Items.


== Examples ==
== Examples ==
Line 219: Line 174:
#{item@avg.price}
#{item@avg.price}
</source>
</source>
== Accessing values of linked library entries ==
Use the following syntax to get access to the values of Link to Entry and other linking fields:
<source lang="java">
#{link field name.linked field name}
</source>
where '''''link field name''''' is the name of a Link to Entry field, '''''linked field name''''' is the name of a field of a related entry.
You can select a field, including one from a related library, using the '''+Field''' button.
If a link field contains several links, then use an index (starting from zero) to get access to them:
<source lang="java">
#{link field name@0.linked field name}, #{link field name@1.linked field name}, …
</source>
You can specify one of the supported aggregation functions instead of the index:
<source lang="java">
#{link field name@sum.linked field name}
</source>
This expression returns the sum of values of field '''linked field name''' for all entries included into '''link field name'''. The functions supported are: '''sum''', '''avg''', '''min''', and '''max'''.
Use the following syntax to get the number of entries in the library referenced by '''link field name''':
<source lang="java">
#{link field name@size}
</source>
=== Example: Parents and children ===
Parents contains fields Name and Age. Children has fields Name and Parents (link to entry (many-to-many)).
Add field "Average parent age" (calculation, real, scale 1) to Children and select the related field from the '''+Field''' list: parents.age. Then insert "@avg", making it:
<source lang="JavaScript">#{parents@avg.age}</source>
=== Example: Orders of products ===
Products has fields SKU, Description, and Price. Orders has fields #, Date, Customer (link to entry (one-to-many)), and Products (link to entry (many-to-many)).
Add field "Total" (calculation, real, scale 2) to Orders and select the related field from the '''+Field''' list: products.price. Then insert "@sum", making it:
<source lang="JavaScript">#{products@sum.price}</source>
'''Note:''' This example points out a deficiency in using a many-to-many relationship for orders of products (or of services). One would hope to associate a quantity for each product ordered, but that requirement cannot be met with this 3-library technique. So, in real life, order solutions generally involve 4 libraries: Customers, Products, Orders, and Order Items. The quantity can reside in Order Items.


{{FieldNameHint}}
{{FieldNameHint}}