Skip to main content

Node JS SDK Samples - Attachment Operations

Get Attachments
Upload Attachments
              
              
const fs = require("fs");
const path = require("path");
const ActionWrapper = require("@zohocrm/nodejs-sdk-2.0/core/com/zoho/crm/api/attachments/action_wrapper").ActionWrapper;
const APIException = require("@zohocrm/nodejs-sdk-2.0/core/com/zoho/crm/api/attachments/api_exception").APIException;
const ResponseWrapper = require( "@zohocrm/nodejs-sdk-2.0/core/com/zoho/crm/api/attachments/response_wrapper").ResponseWrapper;
const FileBodyWrapper = require( "@zohocrm/nodejs-sdk-2.0/core/com/zoho/crm/api/attachments/file_body_wrapper").FileBodyWrapper;
const {AttachmentsOperations, GetAttachmentsParam, DeleteAttachmentsParam, UploadLinkAttachmentParam} = require( "@zohocrm/nodejs-sdk-2.0/core/com/zoho/crm/api/attachments/attachments_operations");
const SuccessResponse = require("@zohocrm/nodejs-sdk-2.0/core/com/zoho/crm/api/attachments/success_response").SuccessResponse;
const ParameterMap = require("@zohocrm/nodejs-sdk-2.0/routes/parameter_map").ParameterMap;
const StreamWrapper = require("@zohocrm/nodejs-sdk-2.0/utils/util/stream_wrapper").StreamWrapper;
class Attachment{

    static async uploadAttachments(moduleAPIName, recordId, absoluteFilePath){

        //example
        // let moduleAPIName = "Leads";
        // let recordId = 3409643000002267003n;
        // let absoluteFilePath = "/Users/user-name/Documents/image.jpg";

        //Get instance of AttachmentsOperations Class that takes recordId and moduleAPIName as parameter
        let attachmentsOperations = new AttachmentsOperations(moduleAPIName, recordId);

        //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 uploadAttachment method that takes FileBodyWrapper instance as parameter
        let response = await attachmentsOperations.uploadAttachment(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 expected ActionWrapper instance is received. 
                if(responseObject instanceof ActionWrapper){
                    
                    //Get the array of obtained action responses
                    let actionResponses = responseObject.getData();

                    actionResponses.forEach(actionResponse => {
                        //Check if the request is successful
                        if(actionResponse instanceof SuccessResponse){
                            //Get the Status
                            console.log("Status: " + actionResponse.getStatus().getValue());

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

                            console.log("Details");

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

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

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

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

                            console.log("Details");

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

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

                            //Get the Message
                            console.log("Message: " + actionResponse.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 = {Attachment}
 
Delete Attachments
              
              
const fs = require("fs");
const path = require("path");
const ActionWrapper = require("@zohocrm/nodejs-sdk-2.0/core/com/zoho/crm/api/attachments/action_wrapper").ActionWrapper;
const APIException = require("@zohocrm/nodejs-sdk-2.0/core/com/zoho/crm/api/attachments/api_exception").APIException;
const ResponseWrapper = require( "@zohocrm/nodejs-sdk-2.0/core/com/zoho/crm/api/attachments/response_wrapper").ResponseWrapper;
const FileBodyWrapper = require( "@zohocrm/nodejs-sdk-2.0/core/com/zoho/crm/api/attachments/file_body_wrapper").FileBodyWrapper;
const {AttachmentsOperations, GetAttachmentsParam, DeleteAttachmentsParam, UploadLinkAttachmentParam} = require( "@zohocrm/nodejs-sdk-2.0/core/com/zoho/crm/api/attachments/attachments_operations");
const SuccessResponse = require("@zohocrm/nodejs-sdk-2.0/core/com/zoho/crm/api/attachments/success_response").SuccessResponse;
const ParameterMap = require("@zohocrm/nodejs-sdk-2.0/routes/parameter_map").ParameterMap;
const StreamWrapper = require("@zohocrm/nodejs-sdk-2.0/utils/util/stream_wrapper").StreamWrapper;
class Attachment{

    static async deleteAttachments(moduleAPIName, recordId, attachmentIds){

        //example
        // let moduleAPIName = "Leads";
        // let recordId = 3409643000002267003n;
        // let attachmentIds = [3409643000002267024n, 3409643000002267034n, 3409643000002267198n]

        //Get instance of AttachmentsOperations Class that takes recordId and moduleAPIName as parameter
        let attachmentsOperations = new AttachmentsOperations(moduleAPIName, recordId);

        //Get instance of ParameterMap Class
        let paramInstance = new ParameterMap();

        for(let attachmentId of attachmentIds) {
            //Add the ids to parameter map instance
            await paramInstance.add(DeleteAttachmentsParam.IDS, attachmentId);
        }

        //Call deleteAttachments method that takes paramInstance as parameter
        let response = await attachmentsOperations.deleteAttachments(paramInstance);

        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 expected ActionWrapper instance is received. 
                if(responseObject instanceof ActionWrapper){

                    //Get the array of obtained Action Responses.
                    let actionResponses = responseObject.getData();

                    actionResponses.forEach(actionResponse => {
                        //Check if the request is successful
                        if(actionResponse instanceof SuccessResponse){

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

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

                            console.log("Details");

                            let details = actionResponse.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: " + actionResponse.getMessage().getValue());
                        }
                        //Check if the request returned an exception
                        else if(actionResponse instanceof APIException){

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

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

                            console.log("Details");

                            let details = actionResponse.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: " + actionResponse.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");

                    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());
                }
            }
        }
    }
}
module.exports = {Attachment}
 
Download an Attachment
              
              
const fs = require("fs");
const path = require("path");
const ActionWrapper = require("@zohocrm/nodejs-sdk-2.0/core/com/zoho/crm/api/attachments/action_wrapper").ActionWrapper;
const APIException = require("@zohocrm/nodejs-sdk-2.0/core/com/zoho/crm/api/attachments/api_exception").APIException;
const ResponseWrapper = require( "@zohocrm/nodejs-sdk-2.0/core/com/zoho/crm/api/attachments/response_wrapper").ResponseWrapper;
const FileBodyWrapper = require( "@zohocrm/nodejs-sdk-2.0/core/com/zoho/crm/api/attachments/file_body_wrapper").FileBodyWrapper;
const {AttachmentsOperations, GetAttachmentsParam, DeleteAttachmentsParam, UploadLinkAttachmentParam} = require( "@zohocrm/nodejs-sdk-2.0/core/com/zoho/crm/api/attachments/attachments_operations");
const SuccessResponse = require("@zohocrm/nodejs-sdk-2.0/core/com/zoho/crm/api/attachments/success_response").SuccessResponse;
const ParameterMap = require("@zohocrm/nodejs-sdk-2.0/routes/parameter_map").ParameterMap;
const StreamWrapper = require("@zohocrm/nodejs-sdk-2.0/utils/util/stream_wrapper").StreamWrapper;
class Attachment{

    static async downloadAttachment(moduleAPIName, recordId, attachmentId, destinationFolder){

        //example
        // let moduleAPIName = "Leads";
        // let recordId = 3409643000002267003n;
        // let attachmentId = 3409643000002267024n;
        // let destinationFolder = "/Users/user-name/Desktop";

        //Get instance of AttachmentsOperations Class that takes recordId and moduleAPIName as parameter
        let attachmentsOperations = new AttachmentsOperations(moduleAPIName, recordId);

        //Call downloadAttachment method that takes attachmentId as parameters
        let response = await attachmentsOperations.downloadAttachment(attachmentId);

        if(response != null){
            //Get the status code from response
            console.log("Status Code: " + response.statusCode);

            if(response.statusCode == 204){
                console.log("No Content");

                return;
            }

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

            if(responseObject != null){
                //Check if expected FileBodyWrapper instance is received. 
                if(responseObject instanceof FileBodyWrapper){

                    //Get StreamWrapper instance from the returned FileBodyWrapper instance
                    let streamWrapper = responseObject.getFile();

                    //Construct the file name by joining the destinationFolder and the name from StreamWrapper instance
                    let fileName = path.join(destinationFolder, streamWrapper.Name);

                    //Get the stream from StreamWrapper instance
                    let readStream = streamWrapper.Stream;
                    
                    //Write the stream to the destination file.
                    fs.writeFileSync(fileName, readStream);
                }
                //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");

                    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());
                }
            }
        }
    }
}
module.exports = {Attachment}
 
Delete an Attachment
              
              
const fs = require("fs");
const path = require("path");
const ActionWrapper = require("@zohocrm/nodejs-sdk-2.0/core/com/zoho/crm/api/attachments/action_wrapper").ActionWrapper;
const APIException = require("@zohocrm/nodejs-sdk-2.0/core/com/zoho/crm/api/attachments/api_exception").APIException;
const ResponseWrapper = require( "@zohocrm/nodejs-sdk-2.0/core/com/zoho/crm/api/attachments/response_wrapper").ResponseWrapper;
const FileBodyWrapper = require( "@zohocrm/nodejs-sdk-2.0/core/com/zoho/crm/api/attachments/file_body_wrapper").FileBodyWrapper;
const {AttachmentsOperations, GetAttachmentsParam, DeleteAttachmentsParam, UploadLinkAttachmentParam} = require( "@zohocrm/nodejs-sdk-2.0/core/com/zoho/crm/api/attachments/attachments_operations");
const SuccessResponse = require("@zohocrm/nodejs-sdk-2.0/core/com/zoho/crm/api/attachments/success_response").SuccessResponse;
const ParameterMap = require("@zohocrm/nodejs-sdk-2.0/routes/parameter_map").ParameterMap;
const StreamWrapper = require("@zohocrm/nodejs-sdk-2.0/utils/util/stream_wrapper").StreamWrapper;
class Attachment{

    static async deleteAttachment(moduleAPIName, recordId, attachmentId){

        //example
        // let moduleAPIName = "Leads";
        // let recordId = 3409643000002267003n;
        // let attachmentId = 3409643000002267024n;

        //Get instance of AttachmentsOperations Class that takes recordId and moduleAPIName as parameter
        let attachmentsOperations = new AttachmentsOperations(moduleAPIName, recordId);

        //Call deleteAttachment method that takes attachmentId as parameter
        let response = await attachmentsOperations.deleteAttachment(attachmentId);

        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 expected ActionWrapper instance is received.
                if(responseObject instanceof ActionWrapper){

                    //Get the list of obtained Action Response instances
                    let actionResponses = responseObject.getData();

                    actionResponses.forEach(actionResponse => {
                        //Check if the request is successful
                        if(actionResponse instanceof SuccessResponse){

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

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

                            console.log("Details");

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

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

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

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

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

                            console.log("Details");

                            let details = actionResponse.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: " + actionResponse.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");

                    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());
                }
            }
        }
    }
}
module.exports = {Attachment}
 
              
              
const fs = require("fs");
const path = require("path");
const ActionWrapper = require("@zohocrm/nodejs-sdk-2.0/core/com/zoho/crm/api/attachments/action_wrapper").ActionWrapper;
const APIException = require("@zohocrm/nodejs-sdk-2.0/core/com/zoho/crm/api/attachments/api_exception").APIException;
const ResponseWrapper = require( "@zohocrm/nodejs-sdk-2.0/core/com/zoho/crm/api/attachments/response_wrapper").ResponseWrapper;
const FileBodyWrapper = require( "@zohocrm/nodejs-sdk-2.0/core/com/zoho/crm/api/attachments/file_body_wrapper").FileBodyWrapper;
const {AttachmentsOperations, GetAttachmentsParam, DeleteAttachmentsParam, UploadLinkAttachmentParam} = require( "@zohocrm/nodejs-sdk-2.0/core/com/zoho/crm/api/attachments/attachments_operations");
const SuccessResponse = require("@zohocrm/nodejs-sdk-2.0/core/com/zoho/crm/api/attachments/success_response").SuccessResponse;
const ParameterMap = require("@zohocrm/nodejs-sdk-2.0/routes/parameter_map").ParameterMap;
const StreamWrapper = require("@zohocrm/nodejs-sdk-2.0/utils/util/stream_wrapper").StreamWrapper;
class Attachment{

    static async uploadLinkAttachment(moduleAPIName, recordId, attachmentURL){

        //example
        // let moduleAPIName = "Leads";
        // let recordId = 3409643000002267003n;
        // let attachmentURL = "https://5.imimg.com/data5/KJ/UP/MY-8655440/zoho-crm-500x500.png";

        //Get instance of AttachmentsOperations Class that takes recordId and moduleAPIName as parameter
        let attachmentsOperations = new AttachmentsOperations(moduleAPIName, recordId);

        //Get instance of ParameterMap Class
        let paramInstance = new ParameterMap();

        //Add the attachmentURL to parameter Instance
        await paramInstance.add(UploadLinkAttachmentParam.ATTACHMENTURL, attachmentURL);

        //Call uploadLinkAttachments method that takes paramInstance as parameter
        let response = await attachmentsOperations.uploadLinkAttachment(paramInstance);

        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 expected ActionWrapper instance is received.
                if(responseObject instanceof ActionWrapper){

                    //Get the array of obtained Action Response instances
                    let actionResponses = responseObject.getData();

                    actionResponses.forEach(actionResponse => {

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

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

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

                            console.log("Details");

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

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

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

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

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

                            console.log("Details");

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

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

                            //Get the Message
                            console.log("Message: " + actionResponse.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 = {Attachment}