Send Zoho Workdrive attachments to zoho crm attachments

PHOTO EMBED

Tue Oct 21 2025 13:00:56 GMT+0000 (Coordinated Universal Time)

Saved by @usman13

void add_attachments_to_crm_reservation(string res_id, string P_Folder_ID, string WD_File_name)
{
try 
{
	// Format file name (remove extension if exists)
	remove_dot = ifnull(WD_File_name.lastIndexOf("."),-1);
	if(remove_dot != -1)
	{
		file_name_without_ext = WD_File_name.substring(0,remove_dot);
	}
	else
	{
		file_name_without_ext = ifnull(WD_File_name,"");
	}
	info "Formatted file name: " + file_name_without_ext;
	// Search Deal in CRM
	rec_id = "";
	criteria = "(Deal_Name:equals:" + file_name_without_ext + ")";
	search_resp = zoho.crm.searchRecords("Deals",criteria);
	if(search_resp != null && search_resp.size() > 0)
	{
		for each  data in search_resp
		{
			Deal_Name = ifnull(data.get("Deal_Name"),"");
			if(file_name_without_ext == Deal_Name)
			{
				rec_id = ifnull(data.get("id"),"");
				break;
			}
		}
	}
	// If Deal found, download and attach file
	if(rec_id != "")
	{
		file_download = invokeurl
		[
			url :"https://workdrive.zoho.com/api/v1/download/" + res_id
			type :GET
			connection:"zoho_wd"
		];
		if(file_download != null)
		{
			file_download.setParamName("file");
			file_download.setFileName(file_name_without_ext + " - Oqood");
			upload_resp = invokeurl
			[
				url :"https://www.zohoapis.com/crm/v6/Deals/" + rec_id + "/Attachments"
				type :POST
				files:file_download
				connection:"zoho_crm"
			];
			info upload_resp;
		}
		else
		{
			info "Download failed: No file content received from WorkDrive.";
		}
	}
	else
	{
		info "No matching Deal found for file name: " + file_name_without_ext;
	}
}
catch (e)
{
	sendmail
	[
		from :zoho.loginuserid
		to :"zoho.failure@leosinternational.zohodesk.com"
		subject :"[WorkDrive Attachment Error | File " + ifnull(WD_File_name,"Unknown") + "]"
		message :"An error occurred while uploading WorkDrive file to CRM.\n\n" + "WorkDrive File: " + ifnull(WD_File_name,"") + "\n" + "Resource ID: " + ifnull(res_id,"") + "\n" + "Error Details: " + e.toString()
	]
}
}
content_copyCOPY