Attach files in a record from Base64-encoded string. (Taking pdf file as example)

Consider base64Content as array containing Base64-encoded strings

const attachFiles = (base64Content, recId) => {
            try {
                if (base64Content?.length) {
                    for (let i = 0; i < base64Content.length; i++) {
                        let fileObj = file.create({
                            name: 'CG-PROOF-' + recId + '-' + i,
                            fileType: file.Type.PDF,
                            contents: base64Content[i].split(',')[1],// if MIME types is present(application/pdf)
                            encoding: file.Encoding.BASE64,
                            folder: 57403
                        });
                        let fileId = fileObj.save();
                        record.attach({
                            record: {
                                type: 'file',
                                id: fileId
                            },
                            to: {
                                type: 'customrecord',
                                id: recId
                            }
                        });
                    }
                }
            } catch (error) {
                log.error("error@attachFiles", error)
            }
        }

Leave a comment

Your email address will not be published. Required fields are marked *