- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-29-2016 10:00 AM
I have 3 fields on a table that I want to calculate and populate that answer to a "total cost" field in the same table (form).
Number of Employees * Hours * Hourly Rate = Total Cost
How can I go about doing this?
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-29-2016 10:19 AM
Hi James,
You'll probably want to do a business rule to accomplish this. You could either add the script to the business rule itself or build out the function within a script include. The following is if it were in a business rule script.
Set the rule to Before. Add conditions if necessary. Check boxes for Insert and Update on 'When to run'. Check the Advanced field.
Under the Advanced tab, enter the following script:
current.u_total_cost = current.u_employees * current.u_hours * current.u_hourly_rate;
The total will update when a record saved. Let me know if you need it to run as a Client Script or want to run it from a script include.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-29-2016 10:19 AM
Hi James,
You'll probably want to do a business rule to accomplish this. You could either add the script to the business rule itself or build out the function within a script include. The following is if it were in a business rule script.
Set the rule to Before. Add conditions if necessary. Check boxes for Insert and Update on 'When to run'. Check the Advanced field.
Under the Advanced tab, enter the following script:
current.u_total_cost = current.u_employees * current.u_hours * current.u_hourly_rate;
The total will update when a record saved. Let me know if you need it to run as a Client Script or want to run it from a script include.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-29-2016 10:32 AM
Thanks Michael, I did it as a business rule, and it works as expected. Though, down the road we may want to switch it to run as a client script.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-30-2016 01:35 PM
The client script is a little more cumbersome. I'm guessing that you'll want to have it recalculate onChange. One way to go about this is to do three client scripts with the onChange variable set to each of the fields: u_employees, u_hours, and u_hourly_rate. The good news is that the script is the same for each:
Type: onChange
Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
var total = 0;
var employees= g_form.getValue('u_employees');
var hours = g_form.getValue('u_hours');
var rate = g_form.getValue('u_hourly_rate');
total = employees * hours * rate;
g_form.setValue('u_total_cost', total);
}
If you take out the default code that gets added to the body, then it will run onLoad, as well. You'll want this in case someone changes the value from a list view, and then enters the record later.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-01-2016 07:13 AM
Thanks Mike! I'll save that for when time comes. And that last bit about list view, I would've never thought of that. Appreciate you sharing your knowledge!