Welcome back everyone!
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 week's 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. Essentially,
Getting started with the function:
- Go to Setup > Automation > Actions > Functions > Configure Function > Write your own.
- Provide a name for the function. For example: "Create Invoice". Add a description (optional).
- Copy the code given below.
- Click “Edit arguments”.
- Enter the name as “dealId” and select the value as “Deal Id”.
- Click Save&Execute Script to check the code.
- Save the function.
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 code given above works only for V2 version of Zoho APIs. Please note that the code WILL NOT work for Version 1.0 APIs.
Found this useful? Try it out and let me know how it works! If you have questions, do not hesitate to ask! Do check out other functions shared in this series here.
See you all next week with another interesting function. Ciao!