Skip to main content

Java SDK Samples - Profile Operations

Get All Profiles
              
              
package com.zoho.crm.sample.profile;

import java.util.Arrays;

import java.util.List;

import java.util.Map;

import com.zoho.crm.api.profiles.APIException;

import com.zoho.crm.api.profiles.Category;

import com.zoho.crm.api.profiles.PermissionDetail;

import com.zoho.crm.api.profiles.ProfilesOperations;

import com.zoho.crm.api.profiles.ResponseHandler;

import com.zoho.crm.api.profiles.ResponseWrapper;

import com.zoho.crm.api.profiles.Section;

import com.zoho.crm.api.util.APIResponse;

import com.zoho.crm.api.util.Model;

public class Profile
{
	/**
	 * Get Profiles
	 * This method is used to retrieve the data of profiles through an API request and print the response.
	 * @throws Exception
	 */
	public static void getProfiles() throws Exception
	{
		
		//Get instance of ProfilesOperations Class that takes the value of the "If-Modified-Since" header
		ProfilesOperations profilesOperations = new ProfilesOperations(OffsetDateTime.now());
		
		//Call getProfiles method
		APIResponse<ResponseHandler> response = profilesOperations.getProfiles();
		
		if(response != null)
		{
			//Get the status code from response
			System.out.println("Status Code: " + response.getStatusCode());
			
			if(Arrays.asList(204,304).contains(response.getStatusCode()))
			{
				System.out.println(response.getStatusCode() == 204? "No Content" : "Not Modified");
				return;
			}
			
			//Check if expected response is received
			if(response.isExpected())
			{
				//Get object from response
				ResponseHandler responseHandler = response.getObject();
				
				if(responseHandler instanceof ResponseWrapper)
				{
					//Get the received ResponseWrapper instance
					ResponseWrapper responseWrapper = (ResponseWrapper) responseHandler;
					
					//Get the list of obtained Profile instances
					List<com.zoho.crm.api.profiles.Profile> profiles = responseWrapper.getProfiles();
				
					for(com.zoho.crm.api.profiles.Profile profile : profiles)
					{
						//Get the DisplayLabel of the each Profile
						System.out.println("Profile DisplayLabel: " + profile.getDisplayLabel());
						
						if(profile.getCreatedTime() != null)
						{
							//Get the CreatedTime of each Profile
							System.out.println("Profile CreatedTime: " + profile.getCreatedTime());
						}
						
						if(profile.getModifiedTime() != null)
						{
							//Get the ModifiedTime of each Profile
							System.out.println("Profile ModifiedTime: " + profile.getModifiedTime());
						}
						
						//Get the Name of the each Profile
						System.out.println("Profile Name: " + profile.getName());
						
						//Get the modifiedBy User instance of each Profile
						com.zoho.crm.api.users.User modifiedBy = profile.getModifiedBy();
						
						//Check if modifiedBy is not null
						if(modifiedBy != null)
						{
							//Get the ID of the modifiedBy User
							System.out.println("Profile Modified By User-ID: " + modifiedBy.getId());
							
							//Get the name of the modifiedBy User
							System.out.println("Profile Modified By User-Name: " + modifiedBy.getName());
							
							//Get the Email of the modifiedBy User
							System.out.println("Profile Modified By User-Email: " + modifiedBy.getEmail());
						}
						
						//Get the Description of the each Profile
						System.out.println("Profile Description: " + profile.getDescription());
						
						//Get the ID of the each Profile
						System.out.println("Profile ID: " + profile.getId());
						
						//Get the Category of the each Profile
						System.out.println("Profile Category: " + profile.getCategory().toString());
						
						//Get the createdBy User instance of each Profile
						com.zoho.crm.api.users.User createdBy = profile.getCreatedBy();
						
						//Check if createdBy is not null
						if(createdBy != null)
						{
							//Get the ID of the createdBy User
							System.out.println("Profile Created By User-ID: " + createdBy.getId());
							
							//Get the name of the createdBy User
							System.out.println("Profile Created By User-Name: " + createdBy.getName());
							
							//Get the Email of the createdBy User
							System.out.println("Profile Created By User-Email: " + createdBy.getEmail());
						}
					}
				}
				//Check if the request returned an exception
				else if(responseHandler instanceof APIException)
				{
					//Get the received APIException instance
					APIException exception = (APIException) responseHandler;
					
					//Get the Status
					System.out.println("Status: " + exception.getStatus().getValue());
					
					//Get the Code
					System.out.println("Code: " + exception.getCode().getValue());
					
					System.out.println("Details: " );
					
					//Get the details map
					for(Map.Entry<String, Object> entry : exception.getDetails().entrySet())
					{
						//Get each value in the map
						System.out.println(entry.getKey() + ": " + entry.getValue());
					}
					
					//Get the Message
					System.out.println("Message: " + exception.getMessage().getValue());
				}
			}
			else
			{//If response is not as expected
				
				//Get model object from response
				Model responseObject = response.getModel();
				
				//Get the response object's class
				Class<? extends Model> clas = responseObject.getClass();
				
				//Get all declared fields of the response class
				java.lang.reflect.Field[] fields = clas.getDeclaredFields();
				
				for(java.lang.reflect.Field field : fields)
				{
					//Get each value
					System.out.println(field.getName() + ":" + field.get(responseObject));
				}
			}
		}
	}
}
Get Specific Profile