Skip to main content

Node JS SDK Samples - Organization Operations

Get Organization Details
Upload Organization Photo
              
              
const fs = require("fs");
const OrgOperations = require("@zohocrm/nodejs-sdk-2.0/core/com/zoho/crm/api/org/org_operations").OrgOperations;
const ResponseWrapper = require("@zohocrm/nodejs-sdk-2.0/core/com/zoho/crm/api/org/response_wrapper").ResponseWrapper;
const FileBodyWrapper = require("@zohocrm/nodejs-sdk-2.0/core/com/zoho/crm/api/org/file_body_wrapper").FileBodyWrapper;
const APIException = require("@zohocrm/nodejs-sdk-2.0/core/com/zoho/crm/api/org/api_exception").APIException;
const SuccessResponse = require("@zohocrm/nodejs-sdk-2.0/core/com/zoho/crm/api/org/success_response").SuccessResponse;
const StreamWrapper = require("@zohocrm/nodejs-sdk-2.0/utils/util/stream_wrapper").StreamWrapper;

class Organization{
    /**
     *  Upload Organization Photo
     * This method is used to upload the brand logo or image of the organization and print the response.
     * @param {String} absoluteFilePath The absolute file path of the file to be attached
     */
    static async uploadOrganizationPhoto(absoluteFilePath){

        //example
        //let absoluteFilePath = "/Users/user_name/Desktop/logo.png";

        //Get instance of OrgOperations Class
        let orgOperations = new OrgOperations();

        //Get instance of FileBodyWrapper class that will contain the request file
        let fileBodyWrapper = new FileBodyWrapper();
        
        /** StreamWrapper can be initialized in any of the following ways */ 

        /**
         * param 1 -> fileName 
         * param 2 -> Read Stream.
         */
        let streamWrapper = new StreamWrapper(null, fs.createReadStream(absoluteFilePath));

        /**
         * param 1 -> fileName
         * param 2 -> Read Stream
         * param 3 -> Absolute File Path of the file to be attached
         */
        // let streamWrapper = new StreamWrapper(null, null, absoluteFilePath);

        //Set file to the FileBodyWrapper instance
        fileBodyWrapper.setFile(streamWrapper);

        //Call uploadOrganizationPhoto method that takes FileBodyWrapper instance as parameter
        let response = await orgOperations.uploadOrganizationPhoto(fileBodyWrapper);

        if(response != null){

            //Get the status code from response
            console.log("Status Code: " + response.statusCode);

            //Get object from response
            let responseObject = response.object;

            if(responseObject != null){

                //Check if the request is successful
                if(responseObject instanceof SuccessResponse){

                    //Get the Status
                    console.log("Status: " + responseObject.getStatus().getValue());

                    //Get the Code
                    console.log("Code: " + responseObject.getCode().getValue());

                    console.log("Details");

                    let details = responseObject.getDetails();

                    //Get the details map
                    if(details != null){
                        Array.from(details.keys()).forEach(key => {
                            console.log(key + ": " + details.get(key));  
                        });
                    }

                    //Get the Message
                    console.log("Message: " + responseObject.getMessage().getValue());
                }
                //Check if the request returned an exception
                else if(responseObject instanceof APIException){

                    //Get the Status
                    console.log("Status: " + responseObject.getStatus().getValue());

                    //Get the Code
                    console.log("Code: " + responseObject.getCode().getValue());

                    console.log("Details");

                    //Get the details map
                    let details = responseObject.getDetails();

                    if(details != null){
                        Array.from(details.keys()).forEach(key => {
                            console.log(key + ": " + details.get(key));  
                        });
                    }

                    //Get the Message
                    console.log("Message: " + responseObject.getMessage().getValue());
                } 
            }
        }
    }
}
module.exports = {Organization}