Print

Zoho Deluge - Modify the product line items in an invoice module

What?
This is a reminder on how to swap out a product in an invoice.

Why?
A client wanted one of their products to be swapped out with another as they had a 3rd-party feed that kept creating a product that they didn't stock.

How?
So editing the product_details field of an invoice record seems straightforward but it isn't. Undocumented, the trick is to remove the existing ID of the line item and posting the map back to the record. In the following example, we are swapping out the product "123456789012345678" with the product "987654321098765432" and p_InvoiceID is the ID of the invoice record:
copyraw
v_Index = 0;
m_Update = Map();
l_ProductItems = List();
r_InvoiceDetails = Map();
r_InvoiceDetails = zoho.crm.getRecordById("Invoices",p_InvoiceID);
for each  r_ProductLineItem in r_InvoiceDetails.get("Product_Details")
{
    m_NewProduct = Map();
    m_NewProduct = r_ProductLineItem;
    if(r_ProductLineItem.get("product").get("id") == "123456789012345678")
    {
        m_NewProduct.remove("product");
        m_NewProduct.remove("id");
        m_NewProduct.remove("product_description");
        m_Mini = Map();
        m_Mini.put("id","987654321098765432");
        m_NewProduct.put("product",m_Mini);
    }
    l_ProductItems.add(m_NewProduct);
}
m_Update = Map();
m_Update.put("Product_Details",l_ProductItems);
r_Update = zoho.crm.updateRecord("Invoices",p_InvoiceID,m_Update);
  1.  v_Index = 0
  2.  m_Update = Map()
  3.  l_ProductItems = List()
  4.  r_InvoiceDetails = Map()
  5.  r_InvoiceDetails = zoho.crm.getRecordById("Invoices",p_InvoiceID)
  6.  for each  r_ProductLineItem in r_InvoiceDetails.get("Product_Details") 
  7.  { 
  8.      m_NewProduct = Map()
  9.      m_NewProduct = r_ProductLineItem; 
  10.      if(r_ProductLineItem.get("product").get("id") == "123456789012345678") 
  11.      { 
  12.          m_NewProduct.remove("product")
  13.          m_NewProduct.remove("id")
  14.          m_NewProduct.remove("product_description")
  15.          m_Mini = Map()
  16.          m_Mini.put("id","987654321098765432")
  17.          m_NewProduct.put("product",m_Mini)
  18.      } 
  19.      l_ProductItems.add(m_NewProduct)
  20.  } 
  21.  m_Update = Map()
  22.  m_Update.put("Product_Details",l_ProductItems)
  23.  r_Update = zoho.crm.updateRecord("Invoices",p_InvoiceID,m_Update)

Additional Note(s):
Just a list of fields to build up a Product Line Item:
copyraw
"Product_Details": [
    {
        "product": {
            "Product_Code": null,
            "Currency": "USD",
            "name": "Test Product",
            "id": "1642130000000104087"
        },
        "quantity": 1,
        "Discount": 0,
        "total_after_discount": 0,
        "net_total": 0,
        "book": null,
        "Tax": 0,
        "list_price": 0,
        "unit_price": 0,
        "quantity_in_stock": 0,
        "total": 0,
        "id": "1642130000047106029",
        "product_description": null,
        "line_tax": []
    }
],
  1.  "Product_Details": [ 
  2.      { 
  3.          "product": { 
  4.              "Product_Code": null, 
  5.              "Currency": "USD", 
  6.              "name": "Test Product", 
  7.              "id": "1642130000000104087" 
  8.          }, 
  9.          "quantity": 1, 
  10.          "Discount": 0, 
  11.          "total_after_discount": 0, 
  12.          "net_total": 0, 
  13.          "book": null, 
  14.          "Tax": 0, 
  15.          "list_price": 0, 
  16.          "unit_price": 0, 
  17.          "quantity_in_stock": 0, 
  18.          "total": 0, 
  19.          "id": "1642130000047106029", 
  20.          "product_description": null, 
  21.          "line_tax": [] 
  22.      } 
  23.  ], 
Category: Zoho :: Article: 692