Add Invoiced Products automatically to Products on Account

Add Invoiced Products automatically to Products on Account

I'm struggling to set up a function which will be triggered when an invoice is created, to automatically add the invoiced items (products) to the Account the invoice belongs to.

I'm currently trying to adapt the following code:
  1. relprod = zoho.crm.getRelatedRecords("Products","Deals",potid);
  2. if(relprod.size() > 0)
  3. {
  4. for each  rec in relprod
  5. {
  6. mp = Map();
  7. prodid = rec.get("id");
  8. update = zoho.crm.updateRelatedRecord("Products",prodid,"Accounts",accid,mp);
  9. update = zoho.crm.updateRelatedRecord("Products",prodid,"Contacts",conid,mp);
  10. }
  11. }
But I keep running into issues, as I'm trying to pull from Invoices, rather than Deals.

Any help would be greatly appreciated!