Kaizen#76 - Using ZDK CRM APIs in Client Script

Kaizen#76 - Using ZDK CRM APIs in Client Script

Welcome to another week of Kaizen! In this post, let us discuss about the ZDK CRM APIs in Client Script.

Client Script provides us the flexibility to use its collection of ZDK Client APIs and ZDK CRM APIs. The ZDK CRM APIs invoke the Zoho CRM APIs. 
Here is the documentation on the available list of ZDK CRM APIs. 

Note:
Every execution of a ZDK CRM API affects your API Credits, as it implicitly calls the Zoho CRM API.

Let us see a few sample Use Cases where ZDK CRM APIs are used.

Use Case: 

ABC is a hardware manufacturing company. Consider that you want to achieve the following using Client Script in Detail Page (Canvas) of a custom module named Orders.

  1. The field Status should be editable only for the profiles "Salesperson" and "Administrator" and should be read-only for all other profiles.
  2. When the field Status is changed to "Dispatched" , send an email to the customer saying "Your order has been dispatched" and display a message that the email is sent.
  3. Add a tag "Delivered" to an order when the order status is changed to "Delivered".

Solution using Client Script

In order to send email, add tag and get user details through Client Script, you need to use the appropriate ZDK from the list of ZDK CRM APIs.
 To accomplish this requirement, create two Client Scripts as follows.

  1. Client Script for Detail Page (Canvas) onLoad (for requirement - 1)
  2. Client Script for Detail Page (Canvas) onChange of field Status (for requirements 2 and 3)

1. Client script for Detail Page (Canvas) onLoad (for requirement - 1)

  • 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.


var user = ZDK.Apps.CRM.Users.fetchById($Crm.user.id);
log(user.profile.name);
  if (user.profile.name != "Salesperson")&&(user.profile.name != "Administrator") {
      ZDK.Page.getField("Status").setReadOnly(true);
  }



Using ZDK.Apps.CRM.Users.fetchById(), you will be able to fetch the details of  the current user. The current user ID is captured using the Client Script Constant $Crm.user.id. So the above script will make the field "Status" a read-only field for all profiles other than Salesperson and Administrator. This ZDK CRM API calls the Get Records API to get the user information.
Here is how the Client Script works for Salesperson Profile users.



Here is how the Client Script works for other Profile users.



2. Client script for Detail Page (Canvas) onChange of field Status (for requirements 2 and 3)

  • 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.


var user = ZDK.Apps.CRM.Users.fetchById($Crm.user.id);
var record = $Page.record_id;
// send email when the order status is "Dispatched"
if (value == "Dispatched") {
  var mail = new ZDK.Apps.CRM.__Actions.Models.Mail();
  mail.from = { "user_name": "ABC Industries", "email": "ABCIndustries@ABC.com" };
  mail.to = [{ "user_name": user.first_name + " " + user.last_name, "email": user.email }];
  mail.subject = "You order has been " + value;
  mail.content = "Hello" + " " + user.first_name + " " + user.last_name + ",   " + "We are glad to let you know that your order has been dispatched";
  mail.mail_format = "text";
  var response = ZDK.Apps.CRM.Orders.sendMail(mail, recordrecord);
ZDK.Client.showMessage("Sent mail to notify that the order dispatch is done.", { type: 'success' });
}
// Add tag when the order status is "Delivered"
if (value == "Delivered") {
  var Order = ZDK.Apps.CRM.Orders.fetchById(record);
  var response = Order.__addTags(["Delivered"]);
}


  • Here, sendMail(mail_object, record_id) is the ZDK CRM API that internally calls the Send Mail API and  __addTags(arrayOfTagNames) is the ZDK CRM API that internally calls the Add Tags to Records. To capture the current record id, $Page.record_id is used and to fetch the current user details, the constant $Crm.user is used.

  • Here is how the Client Script works.



  • When the status is updated as "delivered" the tag will be added as shown in the following image.


  • When the status is updated as "delivered" the tag will be added as shown in the following image.

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.

Cheers!


      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 WorkDrive Resources



                                                                      Zoho Campaigns Resources


                                                                        Zoho CRM Resources

                                                                        • CRM Community Learning Series

                                                                          CRM Community Learning Series


                                                                        • Tips

                                                                          Tips

                                                                        • 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