# Frequently used functions

1. Calculate the sum of linked activities

`let total = 0;// Make sure you select correct dependencies from the 'Linked From field' selection.// Loop through the array of linked activities and match against allowed fields (Total price)for (let value of dep['Total price']) {    total += value || 0;}// Use Math.round function to limit number of decicamls, and then return.function round(input) {    return Math.round(input * 100) / 100;}return round(total);`

2. Calculate the actual project margin in percent (%)

`let total = 0;//Set project budget and cost, default to 0 if emptyconst totalPrice = dep['Total Price'] || 0;const totalCost = dep['Total Cost'] || 0;total += (1-(totalCost / totalPrice))*100;//Round total in return to keep numer of decimals and output cleanreturn Math.round(total*100)/100`

3. Automatic naming of project activity

`// Fetch customer name when customer is selected in an activity link field // DO NOT USE dep['Customer'].name propertyconst customer = dep['Customer name'] || 'Customer not selected';// Fetch project nameconst project = dep['Project name'] || 'Project name missing';// Return customer and project namereturn customer + ' - ' + project;`

4. Fetch unit price of the product but prevent a field from auto-updating and allow manual overwrite

`// Set activity field and linkedTo field dependencies with same name// Place activity field dep above linkedTo field dep// Function will read activity field first, if empty, it will read linkedTo field.// Field where this is returned is "Price per unit"// Return current value if not 0 or nullreturn dep['Price per unit'] || 0;`

5. Taking the values of two date fields into one date range field

`return {start:dep['Start date'], end:dep['End date']};`

Take a look at more examples in Bitbucket