Validate order quantity in subform

Validate product quantities in subforms by enforcing minimum order and multiple quantity rules, ensuring error-free and compliant quotes.

Many businesses, particularly those involved in B2B sales, wholesale, or distribution, adhere to strict policies regarding the quantity of products that can be sold in each order. For instance, the quantity ordered must meet or exceed the Minimum Order Quantity (MOQ), and any additional quantity must be a multiple of the Minimum Multiple Quantity (MMQ). These policies exist to ensure compliance with packaging requirements, batching guidelines, and supplier fulfillment constraints.

For example, if a customer wishes to order 10 units of Product A, and Product A has an MOQ of 6 and an MMQ of 3. In this case, while the MOQ requirement is satisfied, the remaining quantity of 4 does not meet the MMQ condition, as 4 is not a multiple of 3. Therefore, the quantity specified in the quote's line item should be invalidated and an error thrown—before the quote is submitted. This prevents salespeople from accidentally entering invalid quantities during the quotation process, avoiding processing delays and compliance issues. However, your CRM does not include such validation for subform entries as a default feature.

This requirement is unique to a small number of businesses, so we support it through our low-code tools. You can write a client script to smoothly execute these validations in your browser whenever you create or edit and save a quote. Read on to learn more.

Permissions and availability

  • Users with the Modules Customization permission can add custom fields.
  • Users with the Manage Extensibility permission can create client scripts.
  • Users with the Manage Sandbox permission can manage the sandbox.

Requirements

  • Add two Number fields in the Products module. These fields will hold the minimum order quantity and the minimum multiple quantity of the related product. Learn more
  • Create a client script that triggers whenever you update any value in the quote's subform cell. Learn more
  • Test the function in a sandbox environment before deploying it to your production environment. Learn more

Add a custom field

The first step is adding the custom number fields to the Products module.

  1. Navigate to Setup > Customization > Modules and Fields.
  2. Click the Products module to open the layout editor and do the following:
    1. Drag and drop the Number field from the New Fields tray to the layout.
    2. Name the number field (e.g., "Minimum Order Quantity") and define its properties as required.
    3. Drag and drop another Number field from the New Fields tray to the layout.
    4. Name the number field (e.g., "Minimum Multiple Quantity") and define its properties as required.
  3. Once you're finished, select Done, then click Save and Close.

Create a client script

The next step is to create a client script in your Zoho CRM account:

  1. Navigate to Setup > Developer Hub > Client Script > +New Script.
  2. Provide a name for the script. For example: "Validate order quantities".
  3. Add a description (optional).
  4. In the Category Details section, do the following:
    1. Category - Choose Module.
    2. Page - Choose Create Page.
      You may also need to add the script for the Edit Page.
    3. Module - Choose Quotes.
    4. Choose the layout.
      It can be Standard or any other custom layout of the quotes module.
  5. In the Event Details section, do the following:
    1. Type - Choose Subform Event
    2. Field - Choose Quotes Items
    3. Event -  Choose onCellChange.
  6. Click Next.
    The Client Script IDE will open.
  7. Copy and paste the script provided below.
    You can customize the script, if needed.
  8. Click Save and Close to save the client script.

The code

Code Copied
console.clear(); 
console.log("start");
var subform = ZDK.Page.getSubform("Quoted_Items").getValues();
if (subform.length > 0) {
    for (i = 0; i < subform.length; i++) {
        var productID = subform[i].Product_Name.id;
        var product = ZDK.Apps.CRM.Products.fetchById(productID);
        var qty = subform[i].Quantity;
        console.log("qty : ", qty);
        var moq = product.Minimum_Order_Quantity;
        var mmq = product.Minimum_Multiple_Quantity;
        if (qty < moq) {
            ZDK.Client.showAlert('Order quantity must be at least ' + moq + ' .');
            return false;
        }
        var remainder = qty - moq;
        if (remainder % mmq !== 0) {
            ZDK.Client.showAlert("Order doesn't contain the minimum multiple quantity.");
            return false;
        }
    }
}

Notes

  • Make sure you update the script with the accurate API name of their corresponding fields (e.g., "Quoted Items"). Learn more
  • You may also need to include the script for the Edit and Clone pages to ensure that any quantity updates are validated.
  • The above script is an example of validating product quantities entered in subforms by enforcing minimum order and multiple quantity rules. You can use this client script code for any other module, such as Deals or custom modules, by modifying the module name and parameters.

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 Quotes > +Create Quote.
  2. On the Quote Details page, enter the required information, including the product name and quantity in the Quoted Items subform.
    The client script will be triggered.
  3. Check if any alerts are triggered when interacting with the subform.
  4. Enter different product quantities and see if alerts are displayed when they do not comply with the minimum order quantity and multiple quantity rules defined for the corresponding product.

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

  • ModuleQuotes
  • Trigger PointBrowser
  • EditionEnterprise and above
  • ComplexityMedium
  • Implementation Time30 minutes

Features used in the solution

Custom FieldClient 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!