For Zoho services only


I'm currently part of a wider delivery team at Ascent Business Solutions, recognised as a leading Zoho Premium Solutions Partner in the United Kingdom.

Ascent Business Solutions support organisations with everything from targeted technical fixes through to full Zoho CRM implementations and long-term platform adoption. Working as a team rather than a one-person consultancy allows projects to move forward consistently, with access to the right skills at each stage.

The team I manage specialises in API integrations between Zoho and third-party finance and commerce platforms such as Xero, Shopify, WooCommerce, and eBay. Much of our work involves solving integration challenges that fall outside standard documentation, supporting new ideas, new sectors, and evolving business models.

Success is measured through practical outcomes and return on investment, ranging from scaling small operations into high-turnover businesses to delivering rapid gains through online payments, automation, and streamlined digital workflows.

If you are looking for structured Zoho expertise backed by an established consultancy, you can contact Ascent Business Solutions on 0121 392 8140 (UK), email info@ascentbusiness.co.uk, or visit https://www.ascentbusiness.co.uk.
ZCRM Client Script: Correct Decimal Fields OnLoad

ZCRM Client Script: Correct Decimal Fields OnLoad

What?
This is an article detailing the client script to fix any fields which exceed their decimal places limit in ZohoCRM.

Why?
So we have a customer that has some decimal fields on the products module that are calculated and will sometimes return numbers with more than 6 decimal places. As this is more than specified on the ZohoCRM field properties, as soon as the staff user tries to save the record, they will get a validation error on the decimal field even if that's not the field that is being changed:
Decimal places for the Unit Price field should be less than or equal to 6
.
Joellipman.com - Decimal places for the Unit Price field should be less than or equal to 6


How?
The workaround for staff is to round this up manually and then save the record. But this can be an annoying overhead, especially if it can be auto-corrected with a client script.

Pre-amble
For both of the methods below, you will need to setup a client script which runs when the product page is being modified:
  1. Login to ZohoCRM as a system administrator
  2. Give it a name, I'm calling mine "Products - Correct Decimals"
  3. Give it a description, I'm giving it the one I have in the function header below
  4. Category is "Module"
  5. Page is "Edit Page (Standard)"
  6. Module is "Products"
  7. Layout is "Standard"
  8. Event Type is "Page Event"
  9. Event is "onLoad"
  10. Click "Next"

Method 1 This includes a function which counts the number of decimals. I didn't need to use this method for my client; just rounding the decimals would suffice but here is my first version of the script:
copyraw
/* *******************************************************************************
Function:       -
Label:          Products - Correct Decimals 
Trigger:        On Products Edit Page (Standard) > Page Event OnLoad
Purpose:	    ZohoCRM only allows up to 6 decimals but some calculations are resulting in more than this 
                which blocks users from saving a record without changing this.  This script will round up decimal fields onload.
Inputs:         -
Outputs:        -

Date Created:   2023-12-13 (Joel Lipman)
                - Initial release
		        - Correctly rounds any decimal fields to their maximum decimal place.
Date Modified:	???
                - ???

******************************************************************************* */

// freezes the GUI?
ZDK.UI.freeze(true);

// attempt
try {

    // taken from stackoverflow: https://stackoverflow.com/questions/17369098/simplest-way-of-getting-the-number-of-decimals-in-a-number-in-javascript
    var fn_CountDecimals = function(value) {
        if (Math.floor(value) !== value)
            return value.toString().split(".")[1].length || 0;
        return 0;
    }

    // get the value from the field "Weight (kg)" (decimal field only allowed 2 decimals)
    var v_ProductWeight = ZDK.Page.getField('Weight_kg').getValue();

    // count if more than 2 decimals, then round it to 2 decimals else use the current value
    var v_ProductWeightFormatted = fn_CountDecimals(v_ProductWeight) > 2 ? parseFloat(v_ProductWeight).toFixed(2) : v_ProductWeight;

    // set the field value to the formatted value
    ZDK.Page.getField("Weight_kg").setValue(v_ProductWeightFormatted);

    // now for currency fields
    var v_ProductPrice = ZDK.Page.getField('Unit_Price').getValue();
    var v_ProductPriceFormatted = fn_CountDecimals(v_ProductPrice) > 6 ? parseFloat(v_ProductPrice).toFixed(6) : v_ProductPrice;
    ZDK.Page.getField("Unit_Price").setValue(v_ProductPriceFormatted);

    // and if we don't want/need to check, just round em (also means you don't need the above function to count decimals)
    var v_ProductCost = ZDK.Page.getField('Purchase_Rate').getValue();
    ZDK.Page.getField("Purchase_Rate").setValue(parseFloat(v_ProductCost).toFixed(6));

} catch (e) {

    // return error (don't display it, just show it in the logs)
    console.log(e);
    //ZDK.Client.showMessage("Client Script error");
}

// unfreeze the GUI
ZDK.UI.freeze(false);
  1.  /* ******************************************************************************* 
  2.  Function:       - 
  3.  Label:          Products - Correct Decimals 
  4.  Trigger:        On Products Edit Page (Standard) > Page Event OnLoad 
  5.  Purpose:        ZohoCRM only allows up to 6 decimals but some calculations are resulting in more than this 
  6.                  which blocks users from saving a record without changing this.  This script will round up decimal fields onload. 
  7.  Inputs:         - 
  8.  Outputs:        - 
  9.   
  10.  Date Created:   2023-12-13 (Joel Lipman) 
  11.                  - Initial release 
  12.                  - Correctly rounds any decimal fields to their maximum decimal place. 
  13.  Date Modified:    ??? 
  14.                  - ??? 
  15.   
  16.  ******************************************************************************* */ 
  17.   
  18.  // freezes the GUI? 
  19.  ZDK.UI.freeze(true)
  20.   
  21.  // attempt 
  22.  try { 
  23.   
  24.      // taken from stackoverflow: https://stackoverflow.com/questions/17369098/simplest-way-of-getting-the-number-of-decimals-in-a-number-in-javascript 
  25.      var fn_CountDecimals = function(value) { 
  26.          if (Math.floor(value) !== value) 
  27.              return value.toString().split(".")[1].length || 0
  28.          return 0
  29.      } 
  30.   
  31.      // get the value from the field "Weight (kg)(decimal field only allowed 2 decimals) 
  32.      var v_ProductWeight = ZDK.Page.getField('Weight_kg').getValue()
  33.   
  34.      // count if more than 2 decimals, then round it to 2 decimals else use the current value 
  35.      var v_ProductWeightFormatted = fn_CountDecimals(v_ProductWeight) > 2 ? parseFloat(v_ProductWeight).toFixed(2) : v_ProductWeight; 
  36.   
  37.      // set the field value to the formatted value 
  38.      ZDK.Page.getField("Weight_kg").setValue(v_ProductWeightFormatted)
  39.   
  40.      // now for currency fields 
  41.      var v_ProductPrice = ZDK.Page.getField('Unit_Price').getValue()
  42.      var v_ProductPriceFormatted = fn_CountDecimals(v_ProductPrice) > 6 ? parseFloat(v_ProductPrice).toFixed(6) : v_ProductPrice; 
  43.      ZDK.Page.getField("Unit_Price").setValue(v_ProductPriceFormatted)
  44.   
  45.      // and if we don't want/need to check, just round em (also means you don't need the above function to count decimals) 
  46.      var v_ProductCost = ZDK.Page.getField('Purchase_Rate').getValue()
  47.      ZDK.Page.getField("Purchase_Rate").setValue(parseFloat(v_ProductCost).toFixed(6))
  48.   
  49.  } catch (e) { 
  50.   
  51.      // return error (don't display it, just show it in the logs) 
  52.      console.log(e)
  53.      //ZDK.Client.showMessage("Client Script error")
  54.  } 
  55.   
  56.  // unfreeze the GUI 
  57.  ZDK.UI.freeze(false)

Method #2
Get value, round it up, set value:
copyraw
/* *******************************************************************************
Function:       -
Label:          Products - Correct Decimals 
Trigger:        On Products Edit Page (Standard) > Page Event OnLoad
Purpose:	    ZohoCRM only allows up to 6 decimals but some calculations are resulting in more than this 
                which blocks users from saving a record without changing this.  This script will round up decimal fields onload.
Inputs:         -
Outputs:        -

Date Created:   2023-12-13 (Joel Lipman)
                - Initial release
		        - Correctly rounds any decimal fields to their maximum decimal place.
Date Modified:	???
                - ???

******************************************************************************* */

// freezes the GUI?
ZDK.UI.freeze(true);

// attempt
try {

    // 2 decimal places allowed
    var v_ProductWeight = ZDK.Page.getField('Weight_kg').getValue();
    ZDK.Page.getField("Weight_kg").setValue(parseFloat(v_ProductWeight).toFixed(2));

    // 6 decimal places allowed
    var v_ProductPrice = ZDK.Page.getField('Unit_Price').getValue();
    ZDK.Page.getField("Unit_Price").setValue(parseFloat(v_ProductPrice).toFixed(6));
    var v_ProductCost = ZDK.Page.getField('Purchase_Rate').getValue();
    ZDK.Page.getField("Purchase_Rate").setValue(parseFloat(v_ProductCost).toFixed(6));

} catch (e) {

    // return error (don't display it, just show it in the logs)
    console.log(e);
}

// unfreeze the GUI
ZDK.UI.freeze(false);
  1.  /* ******************************************************************************* 
  2.  Function:       - 
  3.  Label:          Products - Correct Decimals 
  4.  Trigger:        On Products Edit Page (Standard) > Page Event OnLoad 
  5.  Purpose:        ZohoCRM only allows up to 6 decimals but some calculations are resulting in more than this 
  6.                  which blocks users from saving a record without changing this.  This script will round up decimal fields onload. 
  7.  Inputs:         - 
  8.  Outputs:        - 
  9.   
  10.  Date Created:   2023-12-13 (Joel Lipman) 
  11.                  - Initial release 
  12.                  - Correctly rounds any decimal fields to their maximum decimal place. 
  13.  Date Modified:    ??? 
  14.                  - ??? 
  15.   
  16.  ******************************************************************************* */ 
  17.   
  18.  // freezes the GUI? 
  19.  ZDK.UI.freeze(true)
  20.   
  21.  // attempt 
  22.  try { 
  23.   
  24.      // 2 decimal places allowed 
  25.      var v_ProductWeight = ZDK.Page.getField('Weight_kg').getValue()
  26.      ZDK.Page.getField("Weight_kg").setValue(parseFloat(v_ProductWeight).toFixed(2))
  27.   
  28.      // 6 decimal places allowed 
  29.      var v_ProductPrice = ZDK.Page.getField('Unit_Price').getValue()
  30.      ZDK.Page.getField("Unit_Price").setValue(parseFloat(v_ProductPrice).toFixed(6))
  31.      var v_ProductCost = ZDK.Page.getField('Purchase_Rate').getValue()
  32.      ZDK.Page.getField("Purchase_Rate").setValue(parseFloat(v_ProductCost).toFixed(6))
  33.   
  34.  } catch (e) { 
  35.   
  36.      // return error (don't display it, just show it in the logs) 
  37.      console.log(e)
  38.  } 
  39.   
  40.  // unfreeze the GUI 
  41.  ZDK.UI.freeze(false)

Category: Zoho CRM :: Article: 394

Credit where Credit is Due:


Feel free to copy, redistribute and share this information. All that we ask is that you attribute credit and possibly even a link back to this website as it really helps in our search engine rankings.

Disclaimer: Please note that the information provided on this website is intended for informational purposes only and does not represent a warranty. The opinions expressed are those of the author only. We recommend testing any solutions in a development environment before implementing them in production. The articles are based on our good faith efforts and were current at the time of writing, reflecting our practical experience in a commercial setting.

Thank you for visiting and, as always, we hope this website was of some use to you!

Kind Regards,

Joel Lipman
www.joellipman.com