Auto-create purchase orders

Automatically create purchase orders from a sales order for each vendor-mapped product, including item, vendor, and shipping details.

For many businesses, maintaining optimal inventory levels is crucial for healthy cash flow management. This involves placing purchase orders with vendors promptly upon confirmation of each sales order.

For example, if your business maintains an inventory of 100 units each for Products A and B, and a customer orders five units of each, you need to generate purchase orders for your respective vendors for exactly five units each. This ensures your inventory remains at the optimal 100-unit level. However, tracking inventory and manually creating purchase orders can be time-consuming and error-prone, especially when dealing with multiple products from different vendors.

What if you could automatically create purchase orders directly from sales orders? This automation solution does just that. A custom button—powered by a Deluge function—automatically creates purchase orders for each vendor-mapped product (including item, vendor, and shipping details) from the sales order with just a click of a button.

Beyond automating purchase order creation, this just-in-time replenishment approach streamlines your procurement process, reduces order processing time, and minimizes data entry errors. Read on to learn more.

Permissions and availability

  • Users with the Modules Customization permission can create custom buttons.
  • Users with the Manage Extensibility permission can write custom functions.
  • Users with the Manage Sandbox permission can manage the sandbox.

Requirements

  • Create and link the button with the custom function, as detailed in the "Create a button" section below. Learn more
  • Test the function in a sandbox before deploying it in your production environment. Learn more

Create a button

Follow the steps below to create a custom button linked to a function.

  1. Navigate to Setup > Customization > Modules and Fields > Sales Orders > Buttons > Create New Button.
  2. Provide a name for the button. For example: "Create Purchase Orders".
    Add a description (optional).
  3. Choose the layouts in which you want the button to be placed.
  4. Specify the button's placement as In Record and its position on the page as Details.
  5. Select the action to be performed as Writing Function.
  6. Provide a name and display name for the function, then click Create.
  7. In the Deluge script editor, do the following:
    1. Copy and paste the code provided below.
    2. Click Edit Arguments.
    3. Enter the name as soid and select the value as Sales Orders - Sale Order Id, then click Save.
    4. Save the function.
  8. Turn on button accessibility for CRM Users.
  9. Select the profiles who can view this custom button.
  10. Click Save.

The code

Code Copied
respMap = zoho.crm.v8.getRecordById("Sales_Orders",soid);
productDet = respMap.get("Ordered_Items");
msg = "";
for each eachProd in productDet
{
	prodinfo = zoho.crm.v8.getRecordById("Products",eachProd.get("Product_Name").get("id"));
	vendor = prodinfo.get("Vendor_Name");
	if(vendor != null)
	{
		data = Collection();
		pdlist = List();
		mp = Map();
		mp.put("Product_Name",eachProd.get("Product_Name"));
		mp.put("Quantity",eachProd.get("Quantity"));
		mp.put("List_Price",eachProd.get("Product_Name").get("Unit_Price"));
		pdlist.add(mp);
		paramap = Map();
		paramap.put("Purchase_Items",pdlist);
		paramap.put("Subject",ifnull(respMap.get("Subject"),""));
		paramap.put("Vendor_Name",prodinfo.get("Vendor_Name"));
		paramap.put("Owner",respMap.get("Owner"));
		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("Shipping_City",ifnull(respMap.get("Shipping_City"),""));
		paramap.put("Shipping_Code",ifnull(respMap.get("Shipping_Code"),""));
		paramap.put("Shipping_Method",ifnull(respMap.get("Shipping_Method"),""));
		paramap.put("Shipping_State",ifnull(respMap.get("Shipping_State"),""));
		paramap.put("Shipping_Street",ifnull(respMap.get("Shipping_Street"),""));
		data.insert(paramap);
		createResp = zoho.crm.v8.createRecord("Purchase_Orders",paramap);
		info createResp;
		if(createResp.get("id") != null)
		{
			msg = "Yes";
		}
	}
}
if(msg == "Yes")
{
	return "PO Created";
}
else
{
	return "Vendor Not available or some error";
}

Notes

  • Make sure to use the accurate API names for their corresponding fields (e.g., "Product Name") in the code snippet. Learn more
  • As a best practice, ensure all products have vendors assigned before creating sales orders.
  • You can customize the script to include additional field mappings per your business requirements.
  • The script above automatically creates purchase orders from a sales order, copying key details at the click of a button. You can use this code for other modules by modifying the module name, field names, and relevant parameters to suit your requirements.

Tip

  • Configure and test the function in a sandbox to ensure that further development doesn't disrupt your production environment.

Test the solution

  1. Navigate to the Sales Orders module.
  2. Open the sales order record for which you want to create a purchase order.
  3. Click the button you previously added in the top-right corner.
    Wait for the success message to appear.
  4. Go to the Purchase Orders module and check the following:
    • New purchase orders have been created for the exact quantities listed in the sales order.
    • All product and shipping details are accurately filled.
  5. Test the edge cases, including products without associated vendors and multiple products sourced from the same vendor.

Did you find this useful? Try it out and let us know how it works. Share this with your team if they'd benefit from it! If you have questions, please don't hesitate to contact us.

More info

  • ModuleSales Orders
  • Trigger PointCustom Button
  • EditionEnterprise and above
  • ComplexityLow
  • Implementation Time15 minutes

Features used in the solution

Custom ButtonDeluge ScriptSandbox
Story Image

Looking for a custom solution?

Contact us, we will help enhance your productivity at lightning speed.

SUBMIT REQUEST

Developers: Share your solution with our community!