calculating total cost

brown9394
Tera Expert

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?

1 ACCEPTED SOLUTION

mloranc
Mega Expert

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.


View solution in original post

8 REPLIES 8

Michael



I have similar case...



Question for you: when you create the business rule, do I select table = task?



Thanks


lharrison
Giga Contributor

I know this thread is old but i'm trying to do a similar thing.   I tried modifying the example above, however one field for the calculation is on a different table ('u_item_price' (price per item)) table: u_item_type.  



How would I get the value of the field from the other table in my business rule script?   I've tried this: - (i'm not a scripting expert!)   What am I doing wrong?



TIA




(function executeRule(current, previous /*null when async*/) {



var total = 0;


    var items = current.number_of_items;


    var price = new GlideRecord('u_item_type);


  price.addQuery('u_item_price');


  price.query();


  while(price.next()) {



  total = price*nights;


  }


  g_form.setValue('total_cost', total);




})(current, previous);


Fredo1
Giga Expert

James



Which table did you select when you created the Business Rule?



Thanks


RiteshSwarnakar
Giga Guru