Extension Pointers - JS SDK Series #6: Working with ZOHO CRM Field's metadata using the Metadata API in widgets

Extension Pointers - JS SDK Series #6: Working with ZOHO CRM Field's metadata using the Metadata API in widgets

Metadata generally refers to the data that provides information about certain aspects of the given data. Metadata API can be used to request data about data and get a response.

The ZOHO.CRM.META API in JS SDK helps to fetch metadata about fields, layouts, modules, related lists, custom views, and assignment rules that are attached to a module in ZOHO CRM. It supports different methods that handle different functionalities. 
  • getFields - To get the field labels and their respective api names
  • getLayouts - To get the complete layout details of a module
  • getModules - To get the complete ZOHO CRM module list
  • getRelatedList - To get the metadata of the RelatedLists present in a module
  • getAssignmentRules -To get the assignment rules details
  • getCustomViews - To get the custom views of a module
As mentioned above, each of these methods handles different functionalities. In this post, let's delve deeper and check how the getFields method can be used in a widget.

Syntax

getFields

ZOHO.CRM.META.getFields(config)

Here, config refers to the configuration object.

Configuration Object

Name
Type
Description
Entity
String
SysRefName of the module.

For our understanding, let's take a simple scenario where a Zoho CRM user has back-to-back calls and online meetings scheduled with different leads. Upon completion of the schedule, the user wants to update the status of the leads based on the response of the call or the online meeting. In such a case, it would be convenient for the user to select multiple leads and update their status from a single space itself. This allows the user to make status changes for multiple leads, like a bulk update, in the widget itself.

MetaAPI.js code snippet

Util={};
var EntityId;
var EntityName;
var temp;
var leadstatusdata;
var leadids;
var leadid;
var leadname;
var leaddata;
var leadsourcename;

//Subscribe to the EmbeddedApp onPageLoad event before initializing the widget 
ZOHO.embeddedApp.on("PageLoad",function(data)
{
EntityId=data.EntityId[0];
EntityName=data.Entity;
console.log(EntityId);
console.log(EntityName);

//Looping through the selected lead records fetched from pageload
for (l=0;l<EntityIds.length;l++)
{

//Fetching the complete details of the selected leads by passing their IDs
ZOHO.CRM.API.getRecord({Entity:"Leads",RecordID:data.EntityId[l]})
.then(function(datas){
console.log(datas)
leaddata=datas.data[0];

//Retrieving the names of the leads from the details fetched and populating them to a multi-select list
var leadsids = document.getElementById("leadsids");
var option = document.createElement("OPTION");
leadid=leaddata.id;
leadname=leaddata.Full_Name;
option.innerHTML = leadname;
option.value = leadid;
leadsids.appendChild(option);

})
}

//Using the META getFields SDK to fetch the metadata of all the fields in the lead module
ZOHO.CRM.META.getFields({"Entity":"Leads"}).then(function(data){
console.log(data);  
leadmetadata=data;
leadmetadatarec=leadmetadata.fields;
length=leadmetadata.fields.length;

//Looping through all the fields metadata
for(i=0;i<leadmetadatarec.length;i++)
{
if(leadmetadata.fields[i].api_name=="Lead_Status")
{
leadstatusdata=leadmetadata.fields[i].pick_list_values;
console.log(leadstatusdata);
leadmetadata1=leadstatusdata;
length1=leadmetadata1.length;

/*Looping through all the picklist values and appending the Lead status picklist values retrieved into the leadstatuses list*/
for (j=0;j<leadmetadata1.length;j++)
{
console.log(leadmetadata1[j].display_value);
leadstatusid=leadmetadata1[j].id;
leadstatusname=leadmetadata1[j].display_value;
leadstatuses = document.getElementById("leadstatuses");
option = document.createElement("OPTION");
option.innerHTML = leadstatusname;
option.value = leadstatusid;
leadstatuses.appendChild(option);
}
}
}

});

// Updating the selected lead status to the selected lead  
Util.update=function()
{

//The leads chosen from the multi-select list are pushed to an array
var selected = [];
for (var option of document.getElementById('leadsids').options) {
if (option.selected) {
selected.push(option.value);
}
}

//Looping through the leads chosen from the multi-select list 
for (m=0;m<selected.length;m++)
{

//Constructing the parameters to pass to the update api
var config={
Entity:"Leads",
APIData:{
"id":selected[m],
"Lead_Status": document.getElementById("leadstatuses").value
},
Trigger:[]
}

//Updating the chosen leads with the selected lead status value 
ZOHO.CRM.API.updateRecord(config)
.then(function(data){
console.log(data)
})
}
}
})
  • In the above code snippet, the details of the selected lead records in the 'Leads' module are retrieved using the getRecords SDK on the page load. (This feature of retrieving only the selected record details on page load can be utilized while the button is positioned in 'ListView'.) Note: If you wish to retrieve the details of all the leads present in the 'Leads' module, you can use the getAllRecords SDK.
  • From the retrieved data, the lead ID's are fetched and populated into a multi-select list. Based on the API name (api_name), the set of picklist values for lead status field is then retrieved using the ZOHO.CRM.META.getFields SDK and populated into a select list.
  • Finally, upon clicking the 'Update Lead Status' button, the set of leads chosen from the multi-select list are updated with the lead status chosen from the lead status list accordingly.


The leads selected in the 'Leads' module list view page are populated into the multi-select list. By clicking the 'Update Lead Status' button, the picked lead from the multi-select list will be updated with the chosen lead status.



In a similar way, you can use the ZOHO.CRM.META SDK and enhance the functionality according to your business requirements. We hope you found this information useful. Keep following this space for more inputs.

SEE ALSO


    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