For Zoho Services only:


I'm actually part of something bigger at Ascent Business Solutions recognized as the top Zoho Premium Solutions Partner in the United Kingdom.

Ascent Business Solutions offer support for smaller technical fixes and projects for larger developments, such as migrating to a ZohoCRM.  A team rather than a one-man-band is always available to ensure seamless progress and address any concerns. You'll find our competitive support rates with flexible, no-expiration bundles at https://ascentbusiness.co.uk/zoho-services/uk-zoho-support.  For larger projects, talk to our experts and receive dedicated support from our hands-on project consultants at https://ascentbusiness.co.uk/zoho-services/zoho-crm-implementation.

The team I manage specializes in coding API integrations between Zoho and third-party finance/commerce suites such as Xero, Shopify, WooCommerce, and eBay; to name but a few.  Our passion lies in creating innovative solutions where others have fallen short as well as working with new businesses, new sectors, and new ideas.  Our success is measured by the growth and ROI we deliver for clients, such as transforming a garden shed hobby into a 250k monthly turnover operation or generating a +60% return in just three days after launch through online payments and a streamlined e-commerce solution, replacing a paper-based system.

If you're looking for a partner who can help you drive growth and success, we'd love to work with you.  You can reach out to us on 0121 392 8140 (UK) or info@ascentbusiness.co.uk.  You can also visit our website at https://ascentbusiness.co.uk.

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 :: Article: 865

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

Related Articles

Joes Revolver Map

Accreditation

Badge - Certified Zoho Creator Associate
Badge - Certified Zoho Creator Associate

Donate & Support

If you like my content, and would like to support this sharing site, feel free to donate using a method below:

Paypal:
Donate to Joel Lipman via PayPal

Bitcoin:
Donate to Joel Lipman with Bitcoin bc1qf6elrdxc968h0k673l2djc9wrpazhqtxw8qqp4

Ethereum:
Donate to Joel Lipman with Ethereum 0xb038962F3809b425D661EF5D22294Cf45E02FebF
© 2024 Joel Lipman .com. All Rights Reserved.