Welcome back people!
Last week, we looked at a custom function that enabled auto-update of multiple fields in a record with the same details. This week, let's look at another custom function which lets you create multiple purchase orders from a single sales order with different product details.
Business Scenario:
Inventory control is an integral part of any order fulfilment process. Unless you have the required inventory, you’d not be able to fulfil your deals on time. The logical next step once your customer accepts your Estimate/Quote is to issue a Sales Order. At this stage, if you don’t have the required items or, if you are a reseller or a distributor who places an order to another vendor to fulfil the deal, you’d have to issue a corresponding Purchase Order. Based on the nature of the deal, the information in your purchase order may vary and entering the details manually is a major hassle that's best avoided if possible. That's exactly this week's custom functions helps us with.
This week's custom function can be embedded to a button, that gets placed either inside the Sales Order module or inside a record in the Sales Order module. Upon the click of the button, a Purchase Order gets created with all the details from the Sales order. Don't worry, it's just a one-time thing. :)
Getting started with the custom function:
- Go to Setup > Customization > Modules and Fields > Select the 'Deals' module > Links and Buttons > +Create new button.
- Provide a name for the button. For example: “[The Purchase Order name or The Product name]”. Add a description (optional).
- Choose View page from the drop-down list.
- Select Writing custom function from the subsequent drop-down.
- Provide a name for the custom function. Add a description (optional).
- 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.
The script:
Code for Version 2.0 API:
respMap = zoho.crm.getRecordById("Sales_Orders", input.soId.toLong());
productDet=ifnull(respMap.get("Product_Details"),"");
pdlist=List();
for each eachProd in productDet
{
productvalue = eachProd.get("product");
proid = productvalue.get("id");
proname = productvalue.get("name");
mp=map();
mp.put("product",{"name":proname,"id":proid});
mp.put("quantity",ifnull(eachProd.get("quantity"),"0").toLong());
mp.put("list_price",ifnull(eachProd.get("list_price"),"0.0").toDecimal());
mp.put(("discount"),ifnull(eachProd.get("discount"),"0.0").toDecimal());
mp.put("total",ifnull(eachProd.get("total"),"0.0").toDecimal());
mp.put("net_total",ifnull(eachProd.get("net_total"),"0.0").toDecimal());
pdlist.add(mp);
}
paramap=map();
paramap.put("Product_Details",pdlist);
paramap.put("Subject",ifnull(respMap.get("Subject"),""));
paramap.put("Contact_Name",ifnull(respMap.get("Contact_Name"),"").get("id"));
paramap.put("Terms_and_Conditions",ifnull(respMap.get("Terms_and_Conditions"),""));
paramap.put("Description",ifnull(respMap.get("Description"),""));
paramap.put("Adjustment",(ifnull(respMap.get("Adjustment"),"0.0")).toDecimal());
paramap.put(("Discount"),(ifnull(respMap.get(("Discount")),"0.0")).toDecimal());
createResp = zoho.crm.create("Purchase_Orders", 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.
Ciao! Stay tuned folks!
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.