Changes

Jump to: navigation, search

Template / Type Formula

2,208 bytes added, 15:39, 2 November 2021
Type Standard Field must be enabled
{{MySQL Syntax}}
 
{{l1}}, {{l2}} and {{l3}} records can perform advanced calculations and logic within their Formula boxes.
 
==General Information==
 
{{l1}}, {{l2}} and {{l3}} records can perform advanced calculations and logic within their Formula boxes.
If you wish to perform calculations on fields associated with Level 3, but store the results in fields at the Level 2, you use the '''Level 2 Formula''' field. You can use any expression and also some built in functions.
You may also wish to use Template Formulas instead of a Calculated field because the calculations are done server side, rather than by the browser.<br>
Thus if you are doing a calculation on a value that needs to be populated or calculated on the same Level 3 item, this will avoid you having to save twice in order to get the final result.
===On Using the Semicolon===
 
When you wish to use multiple template formulas, you must use the semicolon to separate them. For example:
'''@level1.total@=@level1.cost@*@level1.quantity@;@level1.gst@=@level1.total@*0.05''';
* If a semicolon is used in the template formula itself, multiple template formulas can be separated by the term ''<end>''
* This is of use, for example, when a template formula is used to store multiple checkbox selections into a consolidated checkbox as follows:<br />''@level3.Main Checkbox@="@level3.Main Checkbox@;@level3.Checkbox1@;@level3.Checkbox2@"''
* In this case, end this template formula, and all other template formulas on this level (general and template/type formulas) with the term ''<end>''
* The resultant list of template formulas might look like this:
  @level1.total@=@level1.cost@*@level1.quantity@<end> @level3.Main Checkbox@="@level3.Main Checkbox@;@level3.Checkbox1@;@level3.Checkbox2@"<end> @level1.gst@=@level1.total@*0.05<end>
<!--The <end> syntax is only available with May 2014 upgrade-->
 ===Template Standard Type Standard Field must be enabled and visible=== '''IMPORTANT''': ''Template formulas will only work if the Template type standard field is enabled and visible on the page.''
* See [[UTA_Standard_Field_Settings#Adding_a_field|enabling standard fields]] for information on enabling the field.
* See [[Visibility Condition]] for information and making it visible.
==Execution Sequence==
 
* The Formulas are calculated when a record is saved or saved in draft.
* Saving a Level 2 or Level 3 record will also initiate the formulas on the parent level(s):
<u>{{l1}}:</u>
* When a {{l1}} record is saved the formulas are executed in the following sequence:
::''Level 1 Formula-->Level 1 Template Formula'' 
<u>Level 2:</u>
* When a Level 2 record is saved the formulas are executed in the following sequence:
::''Level 2 Formula-->Level 2 Type Formula-->Level 1 Formula-->Level 1 Template Formula'' 
* '''Note''': The Level 1 Formula and Level 1 Template Formula are only executed if one of them refers to a Level 2 variable.
* When a Level 3 record is saved the formulas are executed in the following sequence:
::''Level 3 Formula-->Level 3 Type Formula-->Level 2 Formula-->Level 2 Type Formula-->Level 1 Formula-->Level 1 Template Formula'' 
* '''Note''': The Level 2 Formula and Level 2 Template Formula are only executed if one of them refers to a Level 3 variable.
==Defined Functions==
 
There are a number of functions that are pre-defined:
{| border="1" style="background-color:#ffffcc;" cellpaddingborder="201" cellspacing="0"cellpadding="20"|-!|Function
! Function|Example
! Example ! |Description
|-
||Count(*)
|Count(*) |@level2.#items#@=@level3.count(*)@
||Count the number of Level 3 records and store the result in the Level 2 '''Items''' custom field .
|-
||Sum()
|Sum() |@level2.amount@=@level3.sum(amount)@
||Sum the '''amount''' standard field from all Level 3 items and store the result in the Level 2 '''Amount''' standard field
|-
||Max()
|Max() |@level2.#most#@=@level3.max(amount)@
||Store the largest '''Amount''' in any Level 3 item into the '''Most''' Level 2 [[Custom Field]]
|-
||Avg()
|Avg() |@level2.#average#@=@level3.avg(amount)@
||Store the average '''Amount''' across all associated Level 3 items for the amount field into the Level 2 '''Average''' [[Custom Field]].
|}
===Criteria===
 
When using these defined functions you can restrict which records are included in the aggregated functions using criteria.
*'''Examples:'''
@level2.count(*):typename='Referee Form'@
===Counting and Adding Business Days===
 
Aside from using [[Counting Business Days between Two Dates|Javascript]] to count business days, there are 2 SmartSimple functions you can use within the L1, L2, and L3 template formula to count and add business days to any Date field.
==Examples==
 
* If you want to assign company of the Level 1 '''Owner''' to the Level 1 '''Customer''' field you would use the following:
@level1.companyid@=@level1.owner.companyid@;
:''Note: the '''Customer''' field can be be hidden, and will still populate. This is true for all Template/Type formulas.'' 
* To assign the owner of the company attached to the Level 1 item to the ''Person '' field use the following statement in the Level 1 template formula box:
* To populate the '''End Date''' standard field with the date the status is changed to '''Complete'''
<pre style="white-space: pre-wrap; white-space: -moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; word-wrap: break-word;">@level1.enddate@=CASE WHEN "@level1.status@"="Complete" and "@level1.enddate@"="0000-00-00" THEN CURDATE() WHEN "@level1.status@"="Complete" and "@level1.enddate@"!="0000-00-00" THEN "@level1.enddate@" ELSE "0000-00-00" END;</pre>
==Using a Level 2 Type Formula==
 
In order to use this technique in the current example you will need to first enable the duration field in the Level 2 activity.
==Using a Level 3 Type Formula==
 
You can use the Level 3 Type Formula field on the Level 3 Type page in a similar manner as for Level 2.
==Using Template/Type Formulas for Text==
 
If you are using a Template or Type formula to populate a field with '''text''', rather than perform a calculation, you must enclose the statement on the right-side of the equal sign in quotations.
@level1.name@="@level1.client first name@ @level1.client last name@";
:''Note that the space between the "client first name" and "client last name" variables will place a space between them in the resultant statement.'' 
'''Example 2'''
@level1.name@="@level1.item 1@, @level1.item 2@ and @level1.item 3@";
:''Would result in a comma and space between the first 2 items and the word "and" between item 2 and 3.'' 
'''IMPORTANT''': You can use single quotes instead of double quotes, but if there is a single quote mark stored within one of the variables it will stop the concatenation of the string. Similarly using double quotes will provide incomplete results if one of the variables stores text that includes a double quote.
==Using Conditional Statements==
===IF-Else Condition===
Conditional statements can be used in formulas with the following structure:
:<span style="font -size="3: medium;">'''@variable@=if(''conditional statement'',''value if true'',''value if false'');'''</fontspan
It is recommended to separate out the criteria to avoid issues caused by variable values containing special characters like quotation marks. For example, instead of the following statement...
<pre style="white-space: -o-pre-wrap;word-wrap: break-word;">@level1.Success Criteria Count@=if('@level1.status@' IN ('Draft','Pending'),'@level1.xml.Success Criteria.Criteria.item.nodevalue@','@level1.Success Criteria Count@');</pre>
white-space: -moz-pre-wrap;...use the following syntax...
<pre style="white-space: -o-pre-wrap;word-wrap: break-word;">@level1.Success Criteria Count@=if(? IN ('Draft','Pending'),?,?):=:@level1.status@:,:@level1.xml.Success Criteria.Criteria.item.nodevalue@:,:@level1.Success Criteria Count@;</pre>
white-space: -o-pre-wrap; word-wrap: break-word;">@level1.Success Criteria Count@=Note that the criteria for the ''if('@level1.status@' IN (statement are first referenced as ''Draft?'' marks,then the ''if'' statement is separated from the criteria values using '':=:'Pending'),then the separate criteria values are separated by '@level1.xml.Success Criteria.Criteria.item.nodevalue@':,:'@level1.Success Criteria Count@');</pre>
...use ===Status-Trigger Condition===With this condition the template formula will only trigger if the status of the record is in the status which has been specified in the following syntax..formula.Example:
<pre style="white-space: pre-wrap;
white-space: -moz-pre-wrap;
white-space{"formula trigger" : -pre-wrap;[{"status" : "LOI Draft"}]}::@level1.LOI Deadline Date@=IF("@level1.LOI Extend Deadline@"!="Yes","@level1.[#(?object=utaproviderL2::criteria=typename="Call")$LOI Submission Deadline Date$#]@","@level1.LOI Deadline Date@")
white-space: -o-pre-wrap;
word-wrap: break-word;">@level1.Success Criteria Count@=if(? IN ('Draft','Pending'),?,?):=:@level1.status@:,:@level1.xml.Success Criteria.Criteria.item.nodevalue@:,:@level1.Success Criteria Count@;</pre>
Note that In the criteria for above example, the ''if'' statement are first referenced as ''?'' marks, then formula will trigger when the ''if'' statement record is separated from the criteria values using in '':=:'', then the separate criteria values are separated by '':,:"LOI Draft"''status. 
===Case-When Condition===
You can also use the '''Case''' syntax:
: <span style="font-size: medium;">'''@level1.Notice Textvariable1@=CASE '@level1.statusvariable2@WHEN ' WHEN 'Status condition 1'' THEN 'Submission Cutoff is March 5th.'value for condition 1'' WHEN 'Status 'condition 2' ' THEN 'Thank 'value for your submissioncondition 2'' WHEN 'Status 'condition 3'' THEN 'Congratulations'value for condition 3'' ELSE 'I have no comment'value if not in any condition'' END;'''</span>OR
: <span style="font-size: medium;">'''@variable1@=CASE WHEN @variable2@ ''in (list items)'' THEN ''value for condition 1'' WHEN @variable2@ ''in (list items)'' THEN ''value for condition 2'' WHEN @variable2@ ''in (list items)'' THEN ''value for condition 3'' ELSE ''value if not in any condition'' END'''</span>
===Examples===
 
* At Level 2: if the field called ''locationpreference'' says "Home" put the value in the ''homeaddress'' field into '''Location'''. Otherwise put the value from the ''worksaddress'' field into '''Location'''.
@level2.location@=if('@level2.locationpreference@'='Home','@level2.homeaddress@','@level2.workaddress@');
*If the '''Status''' is ''Closed'' AND no ''enddate'' entered, populate the '''enddate''' with today's date. Otherwise leave the existing ''enddate''.: ::'''Note''': to test for a blank date you should test against both '''=&#39;&#39;''' and '''regexp '@|0000''''
:: '''Note''': to test for a blank date you should test against both '''=''''' and '''regexp '@|0000''''
@level1.enddate@=
if('@level1.status@'='Closed' and ('@level1.enddate@'=&#39;&#39; '' or '@level1.enddate@' regexp '@|0000'),
date_format(now(),'%Y-%m-%d'),
@level3.consultant@=if('@level3.contact.rolelist@' like '%12345%','@level3.contact.fullname@','@level2.owner.fullname@');
 
* At Level 2, populate the Level 2 ''Funding Group'' based on a level 1 ''Funding Area''
 
@level2.FundingGroup@=CASE WHEN "@level1.FundingArea@" in ('Water", "Recycling","Environment") THEN "Water"
WHEN "@level1.FundingArea@" in ("Active Health", "Arts & Culture","Education") THEN "Well-Being"
WHEN "@level1.FundingArea@" in ("Women Empowerment") THEN "Women"
ELSE "" END
==Using Object Syntax==
 
Object syntax can be used in template formulas to loop through the level below and to assign a value or values based on some defined criteria.
<br/><br/>
:<span style="font -size="3: medium;">'''@level1.name@="@level1.[#(?object=activity::criteria=eventtype=3123) ~subject~ #]@"'''</fontspan
<br/>In this example the value in the Subject standard field for the child level 2s that have an eventtype with value of 3123 will be stored in the Name standard field of the parent {{l1}}.
<br/><br/><br/><br/>
 
==Using Trigger Points==
Trigger Point syntax to trigger template formulas under specific conditions e.g. trigger specific template formulas for specific UTA types and statuses.
 
Double colons will be used to separate the formula trigger and the formula itself.
 
<pre>{"formula trigger" : [{"type" : "type1"}]}::template formula
</pre>
 
Each condition can be a combination of type and status.
 
<pre>{"formula trigger" : [{"type" : "type1", "status" : "status1"}]}::template formula
</pre>
 
Each type/status condition can contain multiple type / status names , each delimited by comma.
 
<pre>{"formula trigger" : [{"type" : "type1, type2"}]}::template formula
</pre>
 
In the example below the Current Date field will only be updated under 2 conditions:
 
* Type is 'Type 1' or 'Type 3' and status is 'Submitted'
* Type is 'Type 2' and status is 'Draft'
 
<pre>{"formula trigger" : [{"type" : "Type 1,Type 3", "status" : "Submitted"}, {"type" : "Type 2", "status" : "Draft"}]}::@ level1.Current Date@ =now()
</pre>
 
==Manipulating dates in template formula==
To refer to month in a date field, make use of Date_Format('@level2.fieldname@'),'%m'. Alternatively use date('@level2.fieldname@') to extract yyyy-mm-dd. Go to sscalculation [http://wiki.smartsimple.com/wiki/Sscalculation] for more examples of syntax.
==UTA Provider syntax==
 
* If you are using template formulas to populate {{l1}} fields with details on associated UTA providers then you must use the following syntax:
==Additional Information==
* You cannot change the [[Status]] using a Template / Type Formula.
* You cannot change set the [[Status]] Amount using a Template / Type Formula.
* You can trigger execution of the Template / Type formulas against a group of records without opening and saving them using the [[Batch Update]] feature.
==See Also==
 
* [[Role Formula]]
* [[Ssescape|SSEscape - for using Template Formulas with text fields]]
[[Category:Universal Tracking ApplicationField Update Formulas]][[Category:Date FormatsUniversal Tracking Application]]
Smartstaff, administrator
60
edits

Navigation menu