Download File from Zoho CRM and Upload as Attachment to Zoho Mail
@celestebancoshp
code:
data:privatelast updated:3 years ago
today
Build integrations remarkably fast!
You're viewing a public workflow template.
Sign up to customize, add steps, modify code and more.
Join 800,000+ developers using the Pipedream platform
steps.
trigger
Cron Scheduler
Deploy to configure a custom schedule
This workflow runs on Pipedream's servers and is triggered on a custom schedule.
steps.
zoho_crm_get_object
Gets record data given its id.
auth
(auths.zoho_crm)
params
Module

Module where the record is located.

string ·params.module
Record id

Id of the record to get.

 
string ·params.record_id
code
async (params, auths) => {
1
2
3
4
5
6
7
8
9
10
11
12
13
}
14
//See Zoho CRM API docs at: https://www.zoho.com/crm/developer/docs/api/v2/get-records.html

if (!params.module || !params.record_id) {
  throw new Error("Must provide module, and record_id parameters.");
}

return await require("@pipedreamhq/platform").axios(this, {
  url: `https://www.zohoapis.com/crm/v2/${params.module}/${params.record_id}`,
  headers: {
    "Authorization": `Zoho-oauthtoken ${auths.zoho_crm.oauth_access_token}`,
  },
})
steps.
zoho_crm_download_attachment
Downloads an attachment file from Zoho CRM, saves it in the temporary file system and exports the file path for use in a future step.
auth
(auths.zoho_crm)
params
Module
string ·params.module
Record ID
{{steps.zoho_crm_get_object.$return_value.data[0].id}}
string ·params.record_id
Attachment ID
 
string ·params.attachment_id
code
async (params, auths) => {
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
}
16
const fs = require('fs')

const file = await require("@pipedreamhq/platform").axios(this, {
  url: `${auths.zoho_crm.api_domain}/crm/v2/${params.module}/${params.record_id}/Attachments/${params.attachment_id}`,
  headers: {
    "Authorization": `Zoho-oauthtoken ${auths.zoho_crm.oauth_access_token}`,
  },
  responseType: 'arraybuffer'
})

const file_path = '/tmp/' + params.attachment_id
fs.writeFileSync(file_path, file)

this.file_path = file_path
steps.
zoho_mail_upload_attachment
Upload a file stored in the Pipedream filesystem to Zoho Mail for use as an attachment to an email. This step returns an attachment object that you can put into the attachments array when sending an email with attachments. https://www.zoho.com/mail/help/api/post-upload-attachments.html https://www.zoho.com/mail/help/api/post-send-email-attachment.html
auth
(auths.zoho_mail)
params
File Name

Whatever you put here will become the name of the file when it is sent as an attachment to an email.

 
my_file.txt
string ·params.file_name
File Path

In a previous step, store a file in the Pipedream file system so that you have a file path to provide for this step.

{{steps.zoho_crm_download_attachment.file_path}}
string ·params.file_path
code
async (params, auths) => {
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
}
38
const fs = require('fs')
const {axios} = require('@pipedreamhq/platform')

const file_name = params.file_name
const file_path = params.file_path

try{
  const account_info = await axios(this, {
    method: 'GET', 
    url: 'http://mail.zoho.com/api/accounts',
    headers: {
      Authorization: `Zoho-oauthtoken ${auths.zoho_mail.oauth_access_token}`
    }
  })
  const account_id = account_info.data[0].accountId

  const file = await fs.createReadStream(file_path)

  const response = await axios(this, {
    method: 'POST',
    url: `https://mail.zoho.com/api/accounts/${account_id}/messages/attachments`,
    headers: {
      "Authorization": `Zoho-oauthtoken ${auths.zoho_mail.oauth_access_token}`,
      'Content-Type': 'application/octet-stream'
    },
    params: {
      fileName: file_name
    },
    data: file
  })

  this.attachment = response.data
} catch (err){
  console.log(err.message)
  console.log(err.response)
}