Hi everyone, when I want to attach newly created f...
# suitescript
j
Hi everyone, when I want to attach newly created file to a customer record (so it shows in communication tab) I am using N/module and attach method but still getting attach ID null. Anyone would now why? Suitelet is executing as administrator.
Copy code
const pdfFile = renderer.renderAsPdf();

                    var fileObj = file.create({
                        name: `${custName} - FSA.pdf`,
                        fileType: file.Type.PDF,
                        contents: pdfFile.getContents(),
                        folder: 47453 // Replace with your target folder ID
                    });

                    var fileId = fileObj.save();
                 

                   // Attach the file to the customer record
                    try {
                        var attachId = record.attach({
                            record: {
                                type: 'file',
                                id: fileId
                            },
                            to: {
                                type: 'customer',
                                id: curRecID
                            }
                        });
                        log.debug('File attached to customer', `Attach ID: ${attachId}, File ID: ${fileId}, Customer ID: ${curRecID}`);
                    } catch (attachError) {
                        log.error({
                            title: 'Error Attaching File',
                            details: attachError.message,
                        });
                    }
Debug result: Attach ID: null, File ID: 81907, Customer ID: 229
e
record.attach returns void
j
Hi Eric, Thank you, I have not notice that. But how come then file is not visible in Files section?
e
From what I can see it looks correct. I can't see how customer ID is being set. Have you verified that the file is being created correctly?
j
File is created correctly and I see it in File Cabinet. I am getting customer ID from CS.
e
I still don't see anything wrong with the attachment of the file to the customer record. Could it be a permission issue on the role that you're viewing the customer record?
Also, something looks off on your code for generating the scriptUrl. I don't believe you should need anything besides url.resolveScript there. It should return the fully resolved URL without you needing to get the domain with url.resolveDomain. I could be missing something though.