Print

Zoho Creator: Add a subform while creating a record

What?
This is an article to document how to include a subform and rows while you are creating a record containing the subform.

Why?
The use case here is for a Quote Builder in Zoho Creator: Create a quote record and include a subform containing the line items. You may have tried code where you add a record first and then add the subform afterwards (insert into <form>[...]) but it can be done all in one go.

How?
Here's a use case example which you will need to adapt to your own but It should do enough (spacing has been added for demo purposes and to make it clearer).

In the below example, my form is called "Quotes" and my subform is called "Line_Items":
copyraw
// initialize variables
// ... this is a demo so ... do your own ...
//
// get source data
r_QuoteDetails = zoho.crm.getRecordById("Quotes", 123456789012345678);
//
// transform variables for main record
v_QuoteName = ifnull(r_QuoteDetails.get("Name"),"Test Quote");
if(!isNull(r_QuoteDetails.get("Contact_Name")))
{
    r_ContactDetails    = zoho.crm.getRecordById("Contacts", r_QuoteDetails.get("Contact_Name").get("id"));
    v_ContactFname      = r_ContactDetails.get("First_Name");
    v_ContactSname      = r_ContactDetails.get("Last_Name");
    v_AddressLine1      = r_ContactDetails.get("Street");
    v_AddressCity       = r_ContactDetails.get("City");
    v_AddressState      = r_ContactDetails.get("State");
    v_AddressZip        = r_ContactDetails.get("Zip");
    v_AddressCountry    = r_ContactDetails.get("Country");
}
//
// get line items from source and populate this subform
l_QuotedItems = r_QuoteDetails.get("Product_Details");
c_LineItems = Collection();
for each  m_LineItem in l_QuotedItems
{
    r_Row               = Quotes.Line_Items();
    r_Row.Product       = m_LineItem.get("Product");
    r_Row.Quantity      = m_LineItem.get("Quantity");
    r_Row.List_Price    = m_LineItem.get("List_Price");
    r_Row.Tax           = m_LineItem.get("Tax");
    r_Row.Discount      = m_LineItem.get("Discount");
    r_Row.Total         = m_LineItem.get("Line_Item_Total");
    c_LineItems.insert(r_Row);
}
//
// create creator record
r_Create = insert into Quotes
[
    Added_User=zoho.loginuser
    Quote_Name=v_QuoteName
    Contact_Name.first_name=v_ContactFname
    Contact_Name.last_name=v_ContactSname
    Billing_Address.address_line_1=v_AddressLine1
    Billing_Address.district_city=v_AddressCity
    Billing_Address.state_province=v_AddressCity
    Billing_Address.postal_Code=v_AddressZip
    Billing_Address.country=v_AddressCountry
    Line_Items=c_LineItems
];
  1.  // initialize variables 
  2.  // ... this is a demo so ... do your own ... 
  3.  // 
  4.  // get source data 
  5.  r_QuoteDetails = zoho.crm.getRecordById("Quotes", 123456789012345678)
  6.  // 
  7.  // transform variables for main record 
  8.  v_QuoteName = ifnull(r_QuoteDetails.get("Name"),"Test Quote")
  9.  if(!isNull(r_QuoteDetails.get("Contact_Name"))) 
  10.  { 
  11.      r_ContactDetails    = zoho.crm.getRecordById("Contacts", r_QuoteDetails.get("Contact_Name").get("id"))
  12.      v_ContactFname      = r_ContactDetails.get("First_Name")
  13.      v_ContactSname      = r_ContactDetails.get("Last_Name")
  14.      v_AddressLine1      = r_ContactDetails.get("Street")
  15.      v_AddressCity       = r_ContactDetails.get("City")
  16.      v_AddressState      = r_ContactDetails.get("State")
  17.      v_AddressZip        = r_ContactDetails.get("Zip")
  18.      v_AddressCountry    = r_ContactDetails.get("Country")
  19.  } 
  20.  // 
  21.  // get line items from source and populate this subform 
  22.  l_QuotedItems = r_QuoteDetails.get("Product_Details")
  23.  c_LineItems = Collection()
  24.  for each  m_LineItem in l_QuotedItems 
  25.  { 
  26.      r_Row               = Quotes.Line_Items()
  27.      r_Row.Product       = m_LineItem.get("Product")
  28.      r_Row.Quantity      = m_LineItem.get("Quantity")
  29.      r_Row.List_Price    = m_LineItem.get("List_Price")
  30.      r_Row.Tax           = m_LineItem.get("Tax")
  31.      r_Row.Discount      = m_LineItem.get("Discount")
  32.      r_Row.Total         = m_LineItem.get("Line_Item_Total")
  33.      c_LineItems.insert(r_Row)
  34.  } 
  35.  // 
  36.  // create creator record 
  37.  r_Create = insert into Quotes 
  38.  [ 
  39.      Added_User=zoho.loginuser 
  40.      Quote_Name=v_QuoteName 
  41.      Contact_Name.first_name=v_ContactFname 
  42.      Contact_Name.last_name=v_ContactSname 
  43.      Billing_Address.address_line_1=v_AddressLine1 
  44.      Billing_Address.district_city=v_AddressCity 
  45.      Billing_Address.state_province=v_AddressCity 
  46.      Billing_Address.postal_Code=v_AddressZip 
  47.      Billing_Address.country=v_AddressCountry 
  48.      Line_Items=c_LineItems 
  49.  ]

From the documentation
Zoho Documentation - Insert Rows in Subform

Additional
Source(s):
Category: Zoho :: Article: 750