Help Needed! How to update Related Subforms when Item removed

Help Needed! How to update Related Subforms when Item removed

Simple Scenario:
You sell Lamps and Bulbs but don't know what bulb goes with each lamp so you add a  "Related Items" subform to the product page. For each Lamp you then add the compatible bulbs. When you do this you want each bulb to update its own "Related Items" subform to show the lamp(s) it is compatible with.

All the above I have working fine.

The Problem:
A Bulb is no longer compatible with a Lamp so I open the Bulb product and remove the lamp from the "Related Items" subform. I then need the Lamp "Related Items" subform to update and remove the Bulb.

How to get the "Id" of the removed related item? Anybody with idea's!!

I thought about adding another product field to store a list of subform product ids and compare on each edit but not sure if Zoho support (or Sunderjan) can suggest an alternative!


Sample code below...

//Sample Product ID and Name
thisPdt = Map();
thisPdt.put("sf_Name",{"name":"Tall Lamp","id":841555038707033576});

//Add "thisPdt" to the SubformList
thisSubformList = list();
thisSubformList.add(thisPdt);

//Add "SubformList" to the Subform map
thisPdtSubform = Map();
thisPdtSubform.put("Related_Items",thisSubformList); //Related_Items is the Subform API_Name


//Get the current product info
pdtDetails = zoho.crm.getRecordById("Products",thisPdtId);

//Check the subform data
pdtSubFormDetails = ifnull(pdtDetails.get("Related_Items"),"");

//List to update parts subform
subformList = list();

for each rec in pdtSubFormDetails
{
  //Get product id / name of the subform related item
  pId = ifnull(rec.get("sf_Name").toMap().get("id"),"");
  pName = ifnull(rec.get("sf_Name").toMap().get("name"),"");

  pMap = Map();
  pMap.put("product",{"name":pName,"id":pId});

  subformList.add(pMap);
  relPdtMap = Map();
  relPdtMap.put("Related_Items",thisSubformList);

  //Add the lamp as a related item to the bulb
  updateRelatedPdt = zoho.crm.update("Products",pId.toLong(),relPdtMap);
}