Set default values based on Page Layouts using Client Script

Set default values based on Page Layouts using Client Script

Hello everyone!
Welcome back to another interesting Kaizen post.
In this post, let us discuss how to set default values for fields based on different layouts using Client Script.

In this Kaizen post,
  1. What are Page Layouts?
  2. Client Script is specific for each layout
  3. Use Case
  4. Solution
  5. Summary
  6. Related Links

1. What are Page Layouts?

Page layouts in Zoho CRM enables you to manage the organization and display of fields, sections, and associated details on a record's page. These page layouts are instrumental in personalizing the user interface and optimizing the data entry process to align with your organization's unique requirements. You can assign layouts to user profiles based on your business requirements.

2. Client Script is specific for each layout
Whenever you create a Client Script, you should  mention the layout for which the Client Script should work. So whenever you want to perform actions specific to a particular layout, you can easily accomplish it using Client Script.
In the image below, you can observe that selecting a layout name is a necessary step for configuring a Client Script.


3. Use Case

Consider that Zylker is a manufacturing Company. The customers include both Wholesalers and Retailers. For this purpose, Zylker has two Page Layouts in a custom module named as Orders. One is the Wholesaler-layout and the other is the Retailer-layout. The Wholesaler-layout is assigned to the wholesaler profile and the Retailer-layout is assigned to the retailer profile.

1. Whenever a customer with retailer profile creates an order, the read-only field Account Type should be auto-populated as Retailer.  In the Create page of the Orders module, when the value entered in the field "Number of pieces" is more than 100, then the Payment Method should get populated as Prepaid and should be read-only. 

2. Whenever the customer with wholesaler profile creates an order, the read-only field Account Type should be auto-populated as Wholesaler and the Payment Method should be populated as Prepaid and should be read-only.

4. Solution

For a particular module, the number of Client Scripts to be created differs based on 
The page for which you want the script to work.
The events that should trigger the script.
The layouts for which you want the script to work.

To accomplish this requirement, you need to create three Client Scripts, one for each layout.
  • Client Script for Retailer Layout with onLoad Page event 
  • Client Script for Retailer Layout with onChange Field event 
  • Client Script for Wholesaler Layout with onLoad Page event 

1. Client Script for Retailer Layout with onLoad Page event 

  • Go to Setup > Developer Space > Client Script. Click +New Script.
  • Specify the details to create a script and click Next.

  • Enter the following script and click save.

//To populate default value for Account Type
var acc_type = ZDK.Page.getField("Account_Type");
acc_type.setValue("Retailer");
//  To make the field "Account Type" read-only
acc_type.setReadOnly(true);


  • This script gets executed whenever the Retailer - Layout, Create Page of Orders module gets loaded. The getField ZDK fetches the object details about the field Account Type. The value, Retailer can be populated to this field using setvalue(). You can make Account Type field read-only using setReadOnly().
  • Here is how this Client Script works.


2. Client Script for Retailer Layout with onChange Field event 
  • Go to Setup > Developer Space > Client Script. Click +New Script.
  • Specify the details to create a script and click Next.

  • Enter the following script and click save.


//To populate  Payment Method when the quantity is more than 100
var no_of_pieces = ZDK.Page.getField("Number_of_pieces");
var payment_method = ZDK.Page.getField("Payment_Method");
if (no_of_pieces.getValue() > 100) {
    payment_method.setValue("Prepaid");
    // To make the field "Payment Method" read-only
    payment_method.setReadOnly(true);
}


  • This script gets executed whenever the user enters a value in the Number of Pieces field in the Retailer - Layout's Create Page of Orders module. The getField ZDK fetches the object details about the fields Number of Pieces and Payment Method. The value, Prepaid will be populated to Payment Method using setvalue(), whenever the value entered in the Number of Pieces is more than 100. You can use getvalue() to get the value entered in a field. You can make this field read-only using setReadOnly().
  • Here is how this Client Script works.



3. Client Script for Wholesaler Layout with onLoad Page event 
  • Go to Setup > Developer Space > Client Script. Click +New Script.
  • Specify the details to create a script and click Next.

  • Enter the following script and click save.

//To populate default value for Account Type
var acc_type = ZDK.Page.getField("Account_Type");
acc_type.setValue("Wholesaler");
// To make the fiels "Account Type" read-only
acc_type.setReadOnly(true);
//To populate default value for Payment Method
var payment_method = ZDK.Page.getField("Payment_Method");
payment_method.setValue("Prepaid");
// To make the fiels "Payment Method" read-only
payment_method.setReadOnly(true);


  • This script gets executed whenever the Wholesaler - Layout, Create Page of Orders module gets loaded. The getField ZDK fetches the object details about the fields Account Type and Payment Method. The value, Wholesaler and Prepaid can be populated to the corresponding fields using setvalue(). You can make Payment Method field read-only using setReadOnly().
  • Here is how this Client Script works.

  • Thus using Client Script, you can populate different default values for the same fields located in different page layouts, based on custom conditions.
Note :
Alternatively, you can achieve this using workflow rules. However, if you want instant actions i.e., before updating or saving a record, or if you have exceeded the workflow limit, you can implement this using Client Script.

5. Summary
In this post, we have discussed,
1. Configuring Client Script based on Layout.
2. Choosing the type of Event for a requirement.
3. Using setValue & setReadOnly for the ZDK.Page.getField().

We hope you found this post useful. We will meet you next week with another interesting topic!
If you have any questions let us know in the comment section.

Click here for more details on Client Script in Zoho CRM.

6. Related Links

Please take a look at our Kaizen collection here.

Cheers!


    Access your files securely from anywhere

        Zoho Developer Community







                                  Zoho Desk Resources

                                  • Desk Community Learning Series


                                  • Digest


                                  • Functions


                                  • Meetups


                                  • Kbase


                                  • Resources


                                  • Glossary


                                  • Desk Marketplace


                                  • MVP Corner


                                  • Word of the Day



                                      Zoho Marketing Automation


                                              Manage your brands on social media



                                                    Zoho TeamInbox Resources

                                                      Zoho DataPrep Resources



                                                        Zoho CRM Plus Resources

                                                          Zoho Books Resources


                                                            Zoho Subscriptions Resources

                                                              Zoho Projects Resources


                                                                Zoho Sprints Resources


                                                                  Qntrl Resources


                                                                    Zoho Creator Resources



                                                                        Zoho Campaigns Resources


                                                                          Zoho CRM Resources

                                                                          • CRM Community Learning Series

                                                                            CRM Community Learning Series


                                                                          • Kaizen

                                                                            Kaizen

                                                                          • Functions

                                                                            Functions

                                                                          • Meetups

                                                                            Meetups

                                                                          • Kbase

                                                                            Kbase

                                                                          • Resources

                                                                            Resources

                                                                          • Digest

                                                                            Digest

                                                                          • CRM Marketplace

                                                                            CRM Marketplace

                                                                          • MVP Corner

                                                                            MVP Corner





                                                                              Design. Discuss. Deliver.

                                                                              Create visually engaging stories with Zoho Show.

                                                                              Get Started Now