Create Invoices from Custom Modules

Business scenario:

Usually, invoices are created from the Deals module. After all, only if you win a deal can you proceed to create an invoice. However, there are millions of businesses that do not follow the specific "Deal" pattern. For instance, in a Hospital, whenever a patient receives treatment and pays for it, although there needs to be an invoice produced, can you call that a 'deal'?

Speaking of which, the native modules of Zoho CRM may not be applicable for some businesses. Hence the option to create modules of your own. In other words, the Custom Modules. Let's take the example from above. You can have custom modules like "Patients", "Doctors", "Payments", "Medical Supplies", etc. The Invoice module, which is native, can be used from the Payments as well as the Medical Supplies module.

Now comes the question. Do you have to manually create Invoices based on the Payments? Wouldn't it be better to automate the process? That's where this function plays a role. Using this function, you can create a workflow or a custom button to generate an invoice once the payment is made.

 

Getting started with the function:

  1. Go to Setup > Developer Space > Functions > + Create New Function.
  2. Select Automation as Function type and click Next.
  3. Provide a name for the function. Add a description(optional).
  4. Copy the code given below.
  5. Click Edit Arguments.
  6. Enter the name as dealId and select the type as Int.
  7. Click Save&Execute Script.
  8. Click Save.
 

The Code:


relcust =zoho.crm.getRelatedRecords("CustomModule3relatedlistname","Deals",dealId.toLong());
//info relcust;
prodlist = List();
for each rec in relcust
{
quant = ifnull(rec.get("Quantity_Required_units"),"0").toLong();
name = rec.get("CustomModule3 Product Requirement Name");
mp = Map();
mp.put("Product_Name",name);
mp.put("Unit_Price",0.0);
mp.put("Quantity",quant);
mp.put("Discount",0.0);
mp.put("tax",0.0);
mp.put("total",0.0);
mp.put("net_total",0.0);
create = zoho.crm.create("Products",mp);
info create;
prodid = create.get("id");
prodmp = Map();
prodmp.put("product",{"name":name,"id":prodid.toLong()});
prodmp.put("list_price",0.0);
prodmp.put("quantity",quant);
prodlist.add(prodmp);
}
invmp = Map();
invmp.put("Subject",potname);
invmp.put("Deal_Name",dealId.toLong());
invmp.put("Product_Details",prodlist);
create1 = zoho.crm.create("Invoices",invmp);
info invmp;
info create1;
   

Note:

  • The above code works only for API V2.0 and not the previous version.
 

Found this useful? Try it out and let us know how it works! If you have questions, do not hesitate to ask! Share this with your team if you find it useful!

Return to Tips