ExtensionPointers - JS SDK Series #2: Fetching the related records of a Zoho CRM module from a widget

ExtensionPointers - JS SDK Series #2: Fetching the related records of a Zoho CRM module from a widget

Each business requires a mutual integration between its individual components in order to operate efficiently.Zoho CRM is a complete enterprise setup, with all its modules mutually linked with each other to keep your data up to date. The Related List is one of the mainZoho CRM features that help to maintain synchronized data. The Related Lists are records associated with the parent record. When the data in a single module in your Zoho CRM account is updated, it is also linked as a related list with the associated modules. 

For example, let's say a new contact is created and associated with an existing account. This contact will now be linked to the "Contacts" section (related list) of the account to which it was associated in the "Accounts" module.

In this post, we'll look at ZOHO.CRM.API.getRelatedRecords() JS SDK in detail. Given the association between the different modules in Zoho CRM, it is possible for us to fetch the related list data from a given module through widgets using an API.

ZOHO.CRM.API.getRelatedRecords() is used to fetch the related list details of a given module from a widget.

Syntax:

ZOHO.CRM.API.getRelatedRecords(config)

Here config is the configuration object constructed with the following details:

Configuration Object

Name
Type
Description
Entity
String
API Name of the module.
RecordID
String
RecordID of the entity whose associated details are required.
RelatedListName
String
API Name of the relatedList of the entity.
page
Number
optional, To get the list of related records from the respective page.
per_page
Number
optional, To get the list of related records available per page.

Example

ZOHO.CRM.API.getRelatedRecords({Entity:"Accounts",RecordID:"4440879000000423000",RelatedList:"Contacts",page:1,per_page:200})
.then(function(data){
console.log(data)
})

Here, the Contacts that are associated with the account with account ID "4440879000000423000" of the "Accounts" module is fetched with up to 200 records from page 1.

Scenario

Let's take a simple scenario where you want to know the total profit of a particular account, obtained from the deals associated with it to set up a finance-related managerial operation. We can use ZOHO.CRM.API.getRelatedRecords(config) in a widget to retrieve all the deal-related details of a particular account in the "Closed Won" stage. Let's see how this can be implemented in a widget:

Code snippet
Util={};
function initializeWidget()
{
// Event Listener triggered when the entity page is loaded
ZOHO.embeddedApp.on("PageLoad",function(data)
{
var id = data.EntityId[0];
var totalrevenue=0;
// Fetch the 'Deals' related list details of the Accounts module
ZOHO.CRM.API.getRelatedRecords({Entity:"Accounts",RecordID:id,RelatedList:"Deals",page:1,per_page:200})
.then(function(data){
temp=data;
rec=data.data;
//Looping through every deal
for (i = 0; i < rec.length; i++) 
{
//Fetching the amount field value of the deals
amt = rec[i].Amount;
stg=JSON.stringify(rec[i].Stage);
stage=stg.trim();
//Summing the value in the amount field of every deal in "Closed Won" stage to retrieve the total revenue
if(stage=="\"Closed Won\"")
{
totalrevenue=totalrevenue+amt;
}
}
//If the user chooses the 'Yes' option to update the total revenue
Util.yes=function()
{
var config={
Entity:"Accounts",
APIData:{
"id": id,
"Annual_Revenue": totalrevenue
},
Trigger:[]
}
//Updates the annual revenue field value with the calculated total amount
ZOHO.CRM.API.updateRecord(config)
.then(function(data){
console.log(data)
})
//closes the widget window
ZOHO.CRM.UI.Popup.close()
.then(function(data){
console.log(data)
})
};
//If the user chooses the 'No' option to update the total revenue
Util.no=function()
{
//closes the widget window
ZOHO.CRM.UI.Popup.close()
.then(function(data){
console.log(data)
})
};
})
})
//Initialize the widget
ZOHO.embeddedApp.init();
}


There are a few Zoho CRM APIs that are used in the above code snippet to perform the specified functionality. Let's have a look at those:

ZOHO.embeddedApp.on("PageLoad",function(data)) - An event listener that is triggered whenever an entity Page is loaded. Here, when a particular account is selected from the list of accounts available in the "Accounts" module, the event listener is triggered on PageLoad from which the ID details of that particular account can be retrieved.

ZOHO.CRM.API.getRelatedRecords(config) - The retrieved ID is then passed as the Record ID input of the getRelatedRecords Zoho CRM API along with the other required details. The entity here is Accounts and the related list is Deals to retrieve account related deals. This will fetch all the deals associated with that particular account. Using the details obtained, you can go ahead and calculate the total revenue/profit by summing up the amount of the deals in "Closed Won"stage.

ZOHO.CRM.API.updateRecord(config) - The total calculated revenue is then updated to a field in the "Accounts" module using the updateRecord Zoho CRM API.

We have used ZOHO.CRM.UI.Popup.close() in our example to close the widget popup after the user decides to choose either "Yes"or "No"to update the total revenue.



In a similar way, you can use the ZOHO.CRM.API.getRelatedRecords(config) API to obtain the related list details of modules from a widget and make operations as per your business requirement.

We hope you found this information useful. Keep following this space for more insights on how to get the most out of your Zoho experience.

SEE ALSO
 







                            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