How to update the Deal's Amount field

How to update the Deal's Amount field

Hi

In my Deals module I have the Amount field. This should be updated when a user adds a product to the Deal. Ive found 2 ways I can do this, but both have big disadvantages:

1) Create a subform with a Product lookup field. The unit prices are then aggregated to give a total price. Presumably I could write a function that copies the value of that field to the Amount field when the record has been saved.

However, if I have a subform it means that a CRM user can edit a price. If I set the subform's price field as read-only, it doesnt show when the product has been selected in the lookup! Its just a greyed-out box!! So its need sto be an editable field, but I cant risk a user editing the price field by mistake.

... so this leads me to the next option...


2) Use the Products related list to add products to a Deal. Ive got a function that takes the Products in the deal and updates the Amount field:

  1. RelatedPotential = zoho.crm.getRelatedRecords("Products","Deals",dealId.toLong());
  2. unitprice = 0.0;
  3. for each  ele in RelatedPotential
  4. {
  5. unit = ifnull(ele.get("Unit_Price"),"0.0").toDecimal();
  6. unitprice = unitprice + unit;
  7. }
  8. mp = Map();
  9. mp.put("Amount",unitprice);
  10. update = zoho.crm.updateRecord("Deals",dealId.toLong(),mp);

However, it only triggers when the Deal has been edited. But I need the function to trigger when a Product has been added to the deal, not when the deal has been edited!!

What do you advise?

Many thanks