Auto-update related fields data

Fetch related field details from a lookup record and update them in custom fields in your current record since they do not update while using a client script in your CRM.

If you take a cursory look at your CRM, you'll notice that updating a lookup field using a client script during an onChange event does not automatically update its related fields or the fields of lookup. This happens because modifying a value through a client script does not trigger the same behind-the-scenes update process that occurs when you enter the value manually. This standard behavior can frustrate salespeople, resulting in incorrect data and redundant data entry. However, this doesn't need to be the case anymore!

Using the client script shared in this solution, you can fetch a field's value from a related record chosen in a lookup field and update it in a custom field of the original module. For example, when you update the account chosen in a Deal's lookup field, the script will fetch the new account's total revenue and automatically update it in the Account Revenue field of the deal.

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 a Lookup field to the Deals module. The lookup establishes a relationship between the deal and the account. Learn more
  • Add a Currency field to the Deals module. This field will hold the total revenue of the related account.
  • Create a client script that triggers when the deal's lookup field is modified on its edit page. Learn more
  • Test the function in a sandbox environment before deploying it to your production environment. Learn more

Add custom fields

The first step is adding the custom lookup and currency fields to the Deals module.

  1. Navigate to Setup > Customization > Modules and Fields.
  2. Click the Deals module to open the layout editor and do the following:
    1. Drag and drop the Lookup field from the New Fields tray to the layout.
    2. Name the lookup field (e.g., "Account Name") and define its properties as required.
    3. Drag and drop the Currency field from the New Fields tray to the layout.
    4. Name the currency field (e.g., "Account Revenue") and define its properties as required.
  3. Once you have finished, 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: "Auto-update related fields data".
  3. Add a description (optional).
  4. In the Category Details section, do the following:
    1. Category - Choose Module.
    2. Page - Choose Edit Page.
    3. Module - Choose Deals.
    4. Choose the layout. It can be Standard or any other custom layout of the deals module.
  5. In the Event Details section, do the following:
    1. Type - Choose Field Event
    2. Field - Choose Account Name
    3. Event -  Choose onChange.
  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
var account = ZDK.Page.getField("Account_Name").getValue();
console.log("Account : ", account);
var accountID = account.id;
//fetch the related field
var amount = ZDK.Apps.CRM.Accounts.fetchById(accountID).Annual_Revenue;
console.log("Amount" + amount);
//update the related field
var amount_field = ZDK.Page.getField('Account_Revenue');
amount_field.setValue(amount);

Notes

  • Make sure you update the script with the accurate API name of their corresponding fields (e.g., "Account Revenue"). Learn more
  • The above script is an example of fetching related field details from an Account lookup record and populating them in custom fields of the Deal record, as these are not updated automatically when using a client script.
  • You can customize the code to update additional related fields from the Account record in the Deal record.

Tips

  • Configure and test the function in a sandbox to ensure that further development doesn't disrupt your production environment.
  • Add a defensive check to the script using "if" and "else" statements to handle cases where the lookup is empty, or the related account cannot be found.

Test the solution

  1. Navigate to the Deals module.
  2. Open the deal record for which you want to update the account lookup.
  3. Click Edit and make changes to the account lookup field.
    The client script will be triggered.
  4. Click Save to apply your changes.
  5. Check whether the Account Revenue field was updated with the Annual Revenue of the modified account.

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

  • ModuleDeals
  • Trigger PointBrowser
  • EditionEnterprise and above
  • ComplexityLow
  • 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!