Requirement Overview
A Zoho CRM user is using a Client Script to update records in the detail page. However, users often face challenges when updating fields or subforms on the Record Detail Page using default methods. Also, a sales representative updates a Lead record in Detail Page, which triggers a Client Script that modifies associated fields (e.g., status, owner, or a custom score field). However, the updated values do not appear instantly on the Record Detail Page. This causes confusion or delays in decision-making.
Current Challenges
Updating a Record in Detail Page - As of now, we do not support setValues() Client method for field updates in the Detail View page using the Client Script feature. The same applies for insetRows() Client method which updates Subform.
Instead, currently we support the mentioned Client methods in other Pages such as Create/Edit/Clone. For Detail View Page, users could use Web API methods (i.e., CRM API) within the client script itself.
How Updates are Reflected in the Record Detail Page - Currently, updates performed in the Record Detail View will not be reflected directly. Instead, users need to refresh the page to view the changes. To overcome this, the user could use navigateTo() Client method in script to provide an automatic navigation to the current record detail page.
Rare Scenario of Automatic refresh loop for OnLoad scripts - Often users configure a Client Script in Detail Page based onLoad Page event and wants the changes to be reflected automatically. However, this creates an automatic loop of page refresh.
Permissions & Availability
-> Users with the Manage Extensibility can configure & setup Client Script.
-> Users with the Manage Sandbox permission can manage the sandbox.
Deploy Client Script in various places
Such as
-> On_Load/On_Save/On_Edit of Page, On_Type/On_Change of Field, Subform Events, Blueprint Events, List View Events, etc.
-> Commands/Shortcut Keys (Trigger script from anywhere & anytime in CRM).
-> Custom Button (Recent Enhancement)
Please refer to the following Help Reference to know more about Client Script Events Details.
To Create Client Script:
You could add Client Script through two ways:
1. Through Setup Page:
Navigate to Setup (⚙️) in Zoho CRM >> Developer Hub >> Client Script >> New Script.
-> Provide a Name & Description appropriate to script
-> Category Details -
- Select Category based on requirement. Module - triggers via Events || Commands - triggers via Command Pallet/Shortcut Keys.
- Choose Module where you want to add script. i.e., Lead, Contact, etc.
- Choose Page to deploy script in specific page. i.e., Create, Edit, Clone, Detail, List, etc.
- Choose Layout. i.e., Standard or Custom layouts.
-> Event Details -
Select Event Type - Field Event or Page Event or Subform Event
- Field Event -> Select the Field & Event (i.e., onChange on First Name field).
- Page Event -> Select the Page & Event (i.e., onLoad on Create Page).
- Subform Event -> Select the Subform & Event (i.e., onCellChange).
2. Through Detail/List/Create/Edit/Clone Pages of a Record in Module
-> Choose a desired module in Zoho CRM, and open either the List, Create, Detail, Edit or Clone page of any record in that module. For example, consider the Create Lead page.
-> For Pages (such as Create/Edit/Clone) - In the right-most corner of the page, click Client Script. Then, Add script.
-> The Category details will be pre-populated by the system. Fill in the other details of the client script, such as Name, Description, Event Details of the client script. Further, click next and add your script to the code editor in Client Script IDE. Then click Save and Close.
Add Client Script via Detail Page:
Add Client Script via Different Pages such as List/Create/Edit/Clone:
Use-case
A Bank loan officer opens a Lead record in the Detail View Page within Zoho CRM. Based on the selected Loan Type, the system should, Auto-populate the Loan Statue field to "In Review". All of this should happen without needing to save manually or reload the page.
Configuration
Now, let us create a Client Script on Detail Page of a Lead Module for OnLoad of Page trigger. As an example, whenever the record detail page loaded, the client script will be triggered and set the action to make an update in the record.
Add two Picklist fields - Loan Type & Loan Status. Also, to avoid an automatic refresh loop in the Detail Page, add a custom checkbox field (e.g., name - ForDetailPage_stop_automatic_reload).
Note : Currently, any updates made to the Detail Page through Client Script do not reflect immediately; the user must reload the page to see the changes applied to the record.
Due to the above limitation, we will automatically refresh the record Detail page using same Client Script so that whenever a user opens a record Detail Page, the script will update the record and make an automatic reload. However, we will have to stop reloading after once. Hence, we are using an other Checkbox field to stop reload.
If you do not want your users to see the custom field, then you could mark it as hidden(i.e. Don't Show) for all profiles in Field Edit Properties.
The Code
- var type = ZDK.Page.getField('Loan_Type').getValue(); //fetching Loan Type field value
- log(type);
- //fetching current record data
- var recDetail = ZDK.Apps.CRM.Leads.fetchById($Page.record_id);
- //checking if type field equals to "Personal Loan". If yes, updating Loan Status field to In Review
- if (type == "Personal Loan") {
- //updating Country & checkbox fields in record
- recDetail.Loan_Status = "In Review";
- recDetail.ForDetailPage_stop_automatic_reload = true;
- var recUpdate = recDetail.__update();
- // log("recUpdate", recUpdate);
-
- //getting checkbox field
- var stopField = ZDK.Page.getField('ForDetailPage_stop_automatic_reload');
- var stopFieldVal = stopField.getValue();
- log("field value - " + stopFieldVal);
- //only when checkbox field is false, then enter inside. If not, skip
- if (stopFieldVal == false) {
- //to refresh the Detail Page automatically after update for one time.
- log("entered");
- ZDK.Client.navigateTo('record_detail', { module: 'Leads', record_id: $Page.record_id });
- }
- }
Working Demo - Screencast
TIPS
-> Ensure to use the correct API Names for both Module & Fields in the script.
-> While writing the script, we would suggest you to add logs() || console.log() to each variable to check the output for seamless functionality. i.e., you could view the output in the "Messages" tab within Client Script IDE for each logs() print. To view console logs, you could follow - "Right Click on browser page >> Inspect >> Console".
-> In case the expected functionality does not work, then try the script by verifying each output & loop along with that, cross-verify the syntax of each ZDK Client/CRM API method in the provided sample help document.
-> If you have any repeated lines of script (i.e., repeated small functions), then you could use Static Resources.
To ensure a smooth implementation, we recommend configuring and testing the setup in the sandbox environment before deploying it to production.