Welcome back everyone!
Last week, we learnt how to
transfer record information from one module to another
, even for custom modules. This week, let's look at a custom function that lets you create multiple purchase orders for each individual products in a Sales Order, rather than a single purchase order for a Contact.
Business scenario
:
Working Capital is the life line of any business. Many components go into working capital and the most important one in the purview of sales and order fulfilment is inventory. Maintaining your stocks at an optimal level helps keep your working capital in check. The vendor management side of CRM helps you do just that. In order to maintain an optimal level of inventory, it is essential to have your inventory, sales orders and purchase orders in sync. One way of achieving this is by maintaining optimal inventory level that fits your line of business and, replenish it as and when you get a new order. More importantly, this help you meet customer deadlines smoothly.
Assume your business deals with 2 products namely Product A and Product B, and say you fix the optimal inventory levels as 100 units each for Product A and Product B respectively. Your customer has just accepted your sales order for 5 units each for either of your products. The custom function I’m sharing today helps you issue PO’s instantly for 5 units of Product A to Vendor A and 5 units of Product B to Vendor B. This helps you maintain the inventory level of 100 units each for either of the products. You do this at the click of a button from inside your Sales Order. Easy?
Getting started with the custom function
:
- Go to Setup > Customization > Modules and Fields > Select the required module > Links and Buttons > +Create New Button.
- Provide a name for the custom button. For example: “Purchase Orders for Products”. Add a description(optional).
- Select the button placements as "View Page". Add a description (optional).
- Select the button action as "Writing Custom Function".
- Give a Name and description(optional) for the function.
- Click “Free flow scripting”.
- Copy the code given below.
- Click “Edit arguments”.
- Enter the name as “soId” and select the value as “Sales Order Id”.
- Save the changes.
- Select the Profiles who can see and use the button.
- Click Save.
The script:
Code for Version 2.0 API:
respMap = zoho.crm.getRecordById("Sales_Orders", input.soId.toLong());
productDet=ifnull(respMap.get("Product_Details"),"");
for each eachProdDet in productDet
{
proid = eachProdDet.get("product").get("id");
proname = eachProdDet.get("product").get("name");
pdlist=List();
mp=map();
mp.put("product",{"name":proname,"id":proid});
mp.put("quantity",ifnull(eachProdDet.get("quantity"),"0").toLong());
mp.put("list_price",(ifnull(eachProdDet.get("list_price"),"0.0")).toDecimal());
mp.put(("Discount"),(ifnull(eachProdDet.get("Discount"),"0.0")).toDecimal());
mp.put("net_total",(ifnull(eachProdDet.get("net_total"),"0.0")).toDecimal());
mp.put("Tax",(ifnull(eachProdDet.get("Tax"),"0.0")).toDecimal());
mp.put("total",(ifnull(eachProdDet.get("total"),"0.0")).toDecimal());
pdlist.add(mp);
paramap=map();
paramap.put("Product_Details",pdlist);
paramap.put("Subject",ifnull(respMap.get("Subject"),""));
paramap.put("Owner",ifnull(respMap.get("Owner"),"").get("id"));
paramap.put("Contact_Name",ifnull(respMap.get("Contact_Name"),"").get("id"));
paramap.put("Tracking_number",ifnull(respMap.get("SO_Number"),""));
paramap.put("Terms_and_Conditions",ifnull(respMap.get("Terms_and_Conditions"),""));
paramap.put("Description",ifnull(respMap.get("Description"),""));
createResp = zoho.crm.create("Purchase_Orders", paramap);
info paramap;
info createResp;
}
return "success";
Code for Version 1.0 API:
respMap = zoho.crm.getRecordById("SalesOrders", input.soId.toLong());
productDet=ifnull(respMap.get("product"),"");
productList=productDet.toJSONList();
pdlist=List();
for each eachProd in productList
{
eachProdDet=eachProd.toMap();
productDesc=ifnull(eachProdDet.get("Product Description"),"");
quantity=ifnull(eachProdDet.get("Quantity"),"0");
listPrice=(ifnull(eachProdDet.get("List Price"),"0.0")).toDecimal();
netTotal=(ifnull(eachProdDet.get("Net Total"),"0.0")).toDecimal();
linediscount=(ifnull(eachProdDet.get(("Discount")),"0.0")).toDecimal();
total=(ifnull(eachProdDet.get("Total"),"0.0")).toDecimal();
productId=ifnull(eachProdDet.get("Product Id"),"");
linetax=(ifnull(eachProdDet.get("Tax"),"")).toDecimal();
mp=map();
mp.put("Product Id",productId);
mp.put("Quantity",quantity);
mp.put("List Price",listPrice);
mp.put(("Discount"),linediscount);
mp.put("Total",total);
mp.put("Tax",linetax);
mp.put("Net Total",netTotal);
pdlist.add(mp);
paramap=map();
paramap.put("Products",pdlist);
paramap.put("Subject",ifnull(respMap.get("Subject"),""));
paramap.put("CONTACTID",ifnull(respMap.get("CONTACTID"),""));
paramap.put("Tracking number",ifnull(respMap.get("SO Number"),""));
paramap.put("Terms and Conditions",ifnull(respMap.get("Terms and Conditions"),""));
paramap.put("Description",ifnull(respMap.get("Description"),""));
paramap.put("Sub Total",netTotal);
paramap.put("Grand Total",netTotal);
createResp = zoho.crm.create("PurchaseOrders", paramap);
info paramap;
info createResp;
pdlist ={};
}
return "success";
Found this useful? Try it out and let me know how it works! If you have questions, do not hesitate to ask! Share this with your team if you find it useful. Do check out other custom functions shared in this series
here
.
See you all next week with another interesting custom function. Ciao!
Update: As you must be aware, API V1.0 will be deprecated and support for version 1.0 API will be available only till Dec 31, 2018. Version 1.0 compatible Functions will continue to work until Dec 31, 2019. You're advised to migrated to API Version 2.0 at the earliest. Check this
announcement
for more. We've updated the post to include the Version 2.0 compatible Function.