I have extend compose.js file. In this file I want to add attachment. For attachment I have create custom endpoint and in this endpoint I'm getting pdf row data. I can convert this row data and tried to attached as attachment but it's not work.
compose.js code.
fetchAndAttachPDF: function () {
var self = this;
var email = this.model;
app.api.call('GET', app.api.buildURL('ComposeEmailAttachPdfApi/getBase64'), null, {
success: function (response) {
var filename = response.filename || 'Report.pdf';
var base64 = response.base64data;
var mimeType = response.mimeType || 'application/pdf';
try {
// Convert base64 to Blob
var byteCharacters = atob(base64);
var byteNumbers = new Array(byteCharacters.length);
for (var i = 0; i < byteCharacters.length; i++) {
byteNumbers[i] = byteCharacters.charCodeAt(i);
}
var byteArray = new Uint8Array(byteNumbers);
var fileBlob = new Blob([byteArray], { type: mimeType });
// Create a File object (optional fallback name)
var file = new File([fileBlob], filename, { type: mimeType });
// Prepare FormData
var formData = new FormData();
formData.append('filename', file); // Important: use `filename` key here
// Upload to temporary Notes file endpoint
var uploadUrl = app.api.buildURL('Notes/temp/file/filename', null, {
delete_if_fails: true,
platform: 'base'
});
console.log('formData');
console.log(formData);
app.api.call('create', uploadUrl, formData, {
processData: false,
contentType: false,
success: function (note) {
// Attach the uploaded note to the email
email.get('attachments_collection').add({
_link: 'attachments',
name: note.name,
filename: note.filename,
id: note.id,
mime_type: note.file_mime_type
});
app.alert.show('pdf-attach-success', {
level: 'success',
messages: 'PDF successfully attached.',
autoClose: true
});
},
error: function (err) {
console.error('Upload failed', err);
app.alert.show('pdf-attach-fail', {
level: 'error',
messages: 'PDF upload failed.',
autoClose: true
});
}
});
} catch (e) {
console.error('Base64 decode error:', e);
app.alert.show('pdf-process-fail', {
level: 'error',
messages: 'Error processing PDF content.',
autoClose: true
});
}
},
error: function (err) {
console.error('PDF fetch failed', err);
app.alert.show('pdf-fetch-fail', {
level: 'error',
messages: 'Failed to fetch PDF.',
autoClose: true
});
}
});
},