Kaizen Java SDK Version 5.0

Kaizen Java SDK Version 5.0

Hello, everyone! Welcome to another Kaizen week. In this post, we will discuss how third-party applications can integrate with Zoho CRM using Java SDK 5.0.

Use case 

Zylker is a real-estate app which can integrate with any CRM vendor and uses that data for it's use-cases. Zylker uses Java for the backend server code. Now they want to integrate with Zoho CRM.  How will they do it? Those users could be in different Zoho data centers like Europe (.eu), USA(.com), India(.in), Australia(.com.au) etc. With Zoho CRM's Java SDK 5.0, which consumes Zoho CRM v5 APIs, we can to do authorization and establish this integration. Zylker will write one common code and it should be usable for any CRM orgs across all DCs. 

Java SDK Project dependency

Include the Java dependency using either Maven or Gradle. The maven dependency is as below:

<repositories>
<repository>
<id>zohocrmsdk-5-0</id>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.zoho.crm</groupId>
<artifactId>zohocrmsdk-5-0</artifactId>
<version>4.0.0</version>
</dependency>
</dependencies>


If you are not using Maven or Gradle, please include the Zoho CRM Java SDK JAR and dependant JARs directly into the Java build path.

Setting up the OAuth client 

Create a Server-based Application in the corresponding API console page (https://www.zohoapis.{com/in/eu/com.au}). Enable "Multi-DC" under Settings of the app. Choose "Use the same OAuth credentials for all data centers". With this, you can use the same client ID, client secret, and redirect URI to authorize any Zoho account in any data center. Please refer to our previous posts to know more about domains and environments [Part -1] and [Part - 2].

Integrating the client with your application

In Zylker's web UI, there will be an option to integrate with Zoho CRM. The integration can be initiated from a link or a button containing the authorization URL, along with the corresponding client ID, state, redirect URI, and OAuth scopes. Once the user clicks on this link/button and completes the OAuth authorization process on Zoho, they will be redirected to the specified redirect URI with HTTP parameters location, code, accounts server, and state. These parameters can be utilized within the Java code logic of the redirect URI to begin using the Zoho CRM Java SDK.
The codes used in this post are hosted here for reference. We will focus on two distinct steps as mentioned below, for Zylker integration with Zoho CRM using Java SDK.
     1.  Authorize with Zoho CRM
     2. Fetching data from Zoho CRM

1. Authorize with Zoho CRM 

The aim of this step is to connect with the user's Zoho CRM and store the token details such as refresh token, access token and expiry time in the preferred datastore like MySQL, pgSQL etc. Once the user lands on the redirect URI after the OAuth2.0 authorization, we can extract the email of the logged-in user from zylker.com cookies. We also obtain the grant token and location from the redirect_uri of the authorization request. We pass these three values to the below integrateZohoCRM() function. This function takes care of creating a token for the connected Zoho CRM account and persisting it in the preferred datastore.


public static Boolean integrateZohoCRM(String location, String grant_token, String user_email) 
{
Boolean integrationStatus = Boolean.FALSE;
try {
Environment environment = DataCenter.get(location);
OAuthToken userToken = new OAuthToken.Builder()
                .clientID(OAuthClientDetails.CLIENT_ID)
                .clientSecret(OAuthClientDetails.CLIENT_SECRET)
                .grantToken(grant_token)
                .redirectURL(OAuthClientDetails.REDIRECT_URL)
                                 .userSignature(new UserSignature(user_email)) //email of the user who is trying to integrate with Zoho CRM          
                                 .build();
new Initializer.Builder().environment(environment).token(userToken).initialize(); 
integrationStatus = Boolean.TRUE;
    } catch (SDKException e) {
e.printStackTrace();
    } catch (Exception e) {
e.printStackTrace();
    }
return integrationStatus;
}

Refer here for the exact code section.

OAuthToken.Builder() creates a new instance of the builder for the OAuthToken class. It allows you to conveniently set up the necessary details for creating an OAuth token for authentication purposes. On passing the client ID, client secret, grant token, redirect URL, and user signature to the OAuthToken.Builder() method, we get the userToken object. One more important thing to note is that we have set the user signature as .userSignature(new UserSignature(user_email)) . This means that we are referring to the token uniquely by the email id of the logged-in Zylker user. The same signature should be used for any further operation to be done in Zoho CRM on behalf of this user.
Now that we have the OAuthToken object, we can send it to the Initializer class (as shown below) to create the refresh/access tokens and persist it.


 new Initializer.Builder().environment(environment).token(userToken).initialize(); 


As we haven't mentioned anything about where to do the persistence, by default the token details are persisted in a file named 'sdk_tokens.txt' using File Persistence. The content of the file would look like the following:


"id","user_name","client_id","client_secret","refresh_token","access_token","grant_token","expiry_time","redirect_url","api_domain" //Do not clear this default format, or else you will need  to generate a grant token again  in order to run the program.
"1","patricia@gmail.com","1000.xxxxx","xxxxxx","1000.xxxxxx.xxxx","1000.xxxxxxxxx.xxxxxxx","1000.xxxxxxxxx.xxxxxxx","1697370902510","https://www.zoho.com","https://www.zohoapis.com"



Apart from File Persistence, tokens can be persisted using Database persistence or Custom persistence.

Note

When Zoho introduces a new DC in future, it will be enough to update the project with the latest version of Zoho CRM Java SDK 5.0 and no further code change will be required. 

2. Fetching data from ZohoCRM

Once the integration is done, we can create OAuthToken with the user's email as UserSignature and call Zoho CRM Java SDK function. This will fetch the data specific to the user. The following fetchCRMData() function is a sample to fetch data from Zoho CRM.


public static void fetchCRMData(String email) throws Exception {
    OAuthToken userToken1 = new OAuthToken.Builder()
        .userSignature(new UserSignature(email))
        .build();
    new Initializer.Builder().token(userToken1).initialize();
    RecordOperations ro = new RecordOperations();
    ParameterMap paramInstance = new ParameterMap();
    List < String > fieldNames = new ArrayList < > (Arrays.asList("Company", "Email"));

    paramInstance.add(RecordOperations.GetRecordsParam.FIELDS, String.join(",", fieldNames));

    @SuppressWarnings("rawtypes")

    APIResponse response = ro.getRecords("Leads", paramInstance, null);

    .

    .

    .

}
 

Refer here for the exact code section.


In this example, we use the SDK's RecordOperations()  for retrieving records from the Leads module. This is done in the following steps:
  •  A new instance of RecordOperations is being created and assigned to the variable ro. This object handles operations related to records in Zoho CRM.  
  • ParameterMap instance is used to specify request parameters for the API calls. We can fetch the Company and the Email fields from the Leads module by passing them as the FIELDS parameter. 
  • We use the getRecords() method to make the API request via SDK, which fetches the respective CRM data.


We hope you found this post useful and interesting.  Stay tuned for more exciting posts.

Previous Kaizen Post : #107 Field Trackers in Zoho CRM

For more topics on Kaizen, please refer to our Kaizen collection here.











                            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