**Overview**

**Sugar Logic**is designed to allow custom business logic that is easy to create, manage, and reuse on both the server and client.

- Sugar Logic is made up of multiple components which build off each other and is extensible at every step. The base component is the Sugar Formula Engine which parses and evaluates human readable formulas.

- The fundamental object is called a
**Formula**. A Formula can be evaluated for a given record using the Sugar Logic parser.

- Sugar Logic has several fundamental types. They are:
**number**,**string**,**boolean**, and**enum**(lists). Functions may take in any of these types or combinations and output one of these types. Fields may also often have their values set to only a certain type.

- Fields with
**calculated values**can now be created from within Studio and Module Builder. The values are calculated based on Sugar Logic formulas. These formulas are used to create a new dependency that is executed on the client side in edit views and the server side on save. The formulas are saved in the varies or vardef extensions and can be created and edited directly.

**How to create a Calculated Field?**

- Create 2 fields labeled as Number1 and Number2 in Studio.

- Next, create a field labeled
**Answer**in Studio.

- To create a
**Calculated Field**, click on the**Calculated Value**check box. After clicking the check box, you will have an option to add a**Formula**.

- To insert a formula, select the
**Edit Formula**button and the**Formula Builder**screen will appear.

- To insert a Formula, select a Function from the list. In our example, we
**use the add**function. Summation of**Number1**and**Number2**will be stored in field**Answer**.

- Next, click on add.
- After clicking on add, select Fields from
**Search Fields…**. Scroll down if your fields are not on screen. In our case, we used Number1 and Number2.

- After selecting the first field, use a
**comma (,)**and then select the second field. Formula Builder should appear similar to the image below. Click**Save.**

- After clicking
**Save**, the Formula will be added to the field.

- Click on the
**Save**button to store the formula in the field definition.

- Put the fields in
**EditView**and**DetailView**through studio.

- Open EditView and enter values in fields
**Number1**and**Number2**. In EditView we put all 3 fields those are Number1, Number2, and Answer. But in Answer field,**we are not able to add value in it manually because it is Calculated Field and remain disabled as an input control****.**

- Save the record and it will display in
**DetailView**as follows.

**List of various Number Type functions.**

Function | Description |
---|---|

abs | abs(Number num) : Returns the absolute value of num. Example >> abs(-5) = 5 |

average | average (Number n, ...) : Returns the average of the given numbers.Example >> average(2, 5, 11) = 6 |

ceil | ceil(Number n) : n rounded up to the next integer. Example >> ceil(5.12) = 6 |

divide | divide(Number numerator, Number denominator) : Returns the numerator divided by the denominator. Example >> divide(8, 2) = 4 |

floor | floor(Number n) : Returns n rounded down to the next integer.Example >> floor(5.73) = 5 |

indexOf | indexOf(val, List l) : Returns the position of val in l or -1 if l does not contain val. Examples >> (1) indexOf("a", createList("a", "b", "c")) = 0 (2) indexOf("d", createList("a", "b", "c")) = -1 |

log | Log(number, base) : Returns the supplied base Log of number.Example >> log(100, 10) = 2 |

max | max (Number num, ...) : Returns highest value number passed in.Example >> max(-4, 2, 3) = 3 |

median | median(Number n, ...) : Returns the median of the supplied numbers.Example >> median(4, 5, 5, 6, 7) = 5 |

min | min(Number num, ...) : Returns lowest value number passed in.Example >> min(-4, 2, 3) = -4 |

multiply | multiply(Number n, ...) : Multiplies the supplied numbers and returns the result. Example >> multiply(-4, 2, 3) = -24 |

ln | ln(Number n) : Returns the natural log of n. Example >> ln(e) = 1 |

negate | negate(Number n) : Returns negated value of n. Example >> negate(4) = -4 |

pow | pow(Number n, Number p) : Returns n to the p power. Example >> pow(2, 3) = 8. |

stddev | stddev(Number n, ...) : Returns the population standard deviation of the given values. Example >> stddev(4, 5, 6, 7, 10) = 2.06 |

strlen | strlen(String s) : Returns the number of characters in the String s.Example >> strlen("Hello") = 5 |

subtract | subtract(Number a, Number b) : Returns a minus b. Example >> subtract(9, 2, 3) = 4 |

number | number(String s) : Returns the numeric value of s. |

**abs**:: abs(Number num) : Returns the absolute value of num. Take a look at the following formula.

- Open EditView and enter value in Demo Integer field.

**average**:: average(Number n, ...) : Returns the average of the given numbers. Take a look at the following formula.

- Open EditView and enter value in Demo Integer and Demo Integer 1 field.

**ceil**:: ceil(Number n) : Returns n rounded up to the next integer. Take a look at the following formula.

- Open EditView and enter the value in Number1
**(float)**field.

**divide**:: divide(Number numerator, Number denominator) : Returns the numerator divided by the denominator. Take a look at the following formula.

- Open EditView and enter value in Demo Integer and Demo Integer 1 field.

**floor**:: floor(Number n) : Returns rounded down to the next integer. Take a look at the following formula.

- Open EditView and enter value in Number1 field.

**indexOf**:: indexOf(val, List l) : Returns the position of val in l OR -1 if l does not contain val. Take a look at the following formula.

- Open EditView and enter value in Number1 field. Return 0, if Number1=10. Return 1, if Number1=20. Return 2, if Number1=30. Return -1if Number1 has values other than 10, 20 and 30.

** **

**log**:: Log(number, base): Returns the supplied base Log of number. Take a look at the following formula.

- Open EditView and enter value in Number1 field.

- Change the base and will see the effect.

**max**:: max(Number num, ...) : Returns highest value number passed in. Take a look at the following formula.

- Open EditView and enter value in Number1 and Number2 field. Check the value in Answer field.

**min**:: min(Number num, ...) : Returns lowest value number passed in. Take a look at the following formula.

- Open EditView and enter value in Number1 and Number2 field. Check the value in Answer field.

**multiply**:: multiply(Number n, ...) : Multiplies the supplied numbers and returns the result. Take a look at the following formula.

- Open EditView and enter value in Number1 and Number2 field. Check the value in Answer field.

**negate**:: negate(Number n) : Returns negated value of n. Take a look at the following formula.

- Open EditView and enter value in Number1 field.

**pow**:: pow(Number n, Number p) : Returns n to the power. Take a look at the following formula.

- Open EditView and enter value in Demo Integer and Demo Integer 1 field.

**rollupAve**:: rollupAve(Relate link, String field) : Returns the average value of field in records related by link.**Example**>> rollupAve($opportunities, "amount") = in Accounts, it will return the average amount of all the Opportunities related to this Account.- Consider an account record which has following opportunities related.

- Our aim is to find out the average value of amount field of all opportunities. Build formula for “Answer” field of Accounts as follows.

- Open EditView of account and save the record. Answer field contains average value of Opportunities Amount field.

**rollupMax**:: rollupMax(Relate link, String field) : Returns the highest value of field in records related by link.**Example**>> rollupMax($opportunities, "amount") in Accounts, it will return the highest amount of any Opportunity related to the Account.- Consider an account record which has following opportunities related.

- Our aim is to find out maximum value of amount field from all the opportunities relate with account. Build formula for “Answer” field of Accounts as follows.

- Open
**EditView**of account and save the record. Answer field contains maximum value of amount field of Opportunity.

**rollupMin**:: rollupMin(Relate link , String field) : Returns the lowest value of field in records related by link.**Example**>> rollupMin($opportunities, "amount") in Accounts, it will return the lowest amount of any Opportunity related to the Account.- Consider an account record which has following opportunities related.

- Our aim is to find out minimum value of amount field from all the opportunities relate with account. Build formula for “Answer” field of Accounts as follows.

- Open
**EditView**of account and save the record. Answer field contains minimum value of amount field of Opportunity.

**rollupSum**:: rollupSum(Relate link, String field) : Returns the sum of the values of field in records related by link.**Example**>> rollupSum($opportunities, "amount") in Accounts, it will return the sum of the amount of all the Opportunities related to the Account.- Consider an account record which has following opportunities related.

- Our aim is to find out total value of amount field from all the opportunities relate with account. Build formula for “Answer” field of Accounts as follows.

- Open
**EditView**of account and save the record. Answer field contains total value of amount field of all Opportunities.

**count**:: count(Relate link) : Returns the number of records related to this record by link.**Example**>> count ($opportunities) in Accounts, it will return the number of opportunities related to this account. Take a look at the following formula.

- Open EditView and save the record.Answer field contains number of Opportunities related with account.

**List of various Date Type functions.**

Function | Description |
---|---|

dayofweek | dayofweek(Date d) : Returns the day of week that d falls on. Sun = 0, Mon = 1, ... , Sat = 6 |

daysUntil | daysUntil(Date d) : Returns number of days from now until the specified date. |

addDays | addDays($date, $days) : Returns a date object moved forward or backwards by $days days.Example >> addDays(date("1/1/2010"), 5) = "1/6/2010" |

now | now() : Returns a date object representing todays date and the current time. |

today | Returns a date object representing todays date. |

date | date(String d) : Converts the given string into a date. |

**dayofweek**:: dayofweek(Date d). Take a look at the following formula.

- Open EditView and enter date value in Demo Date field.Answer field will contains number of day of week