Marc
02/27/2025, 2:27 PMjavascript
function attachFileToMessage(recordObj, fileId) {
var mediaSublistId = 'mediaitem';
var numLines = recordObj.getLineCount({ sublistId: mediaSublistId });
log.debug("Existing Attachments", numLines);
for (var i = 0; i < numLines; i++) {
var attachedFileId = recordObj.getSublistValue({
sublistId: mediaSublistId,
fieldId: 'mediaitem',
line: i
});
if (parseInt(attachedFileId) === fileId) {
log.audit('Attachment Skipped', 'File ID ' + fileId + ' is already attached.');
return;
}
}
try {
var nextLine = numLines;
recordObj.setSublistValue({
sublistId: mediaSublistId,
fieldId: 'mediaitem',
line: nextLine,
value: fileId
});
log.audit('Attachment Successful', 'File ID ' + fileId + ' attached to Message Record ID: ' + <http://recordObj.id|recordObj.id>);
} catch (e) {
log.error('Error Attaching File', e);
}
}
Despite the file existing in the File Cabinet, I receive an error indicating that the file ID is not found. What could be causing this issue, and how can I resolve it?Celigo AI
02/27/2025, 2:27 PMAI generated answer (using internal Slack threads):Based on the conversations, it seems like you are trying to attach a file to a record using SuiteScript. The error you are encountering might be due to incorrect usage of the
record.attach
method or incorrect file ID.
Here's how you can attach a file to a record:
javascript
var id = record.attach({
record : {
type : record.Type.FILE,
id : fileId
},
to: {
type : record.Type.CUSTOM_RECORD,
id : recordId
}
});
In this code, fileId
is the ID of the file you want to attach and recordId
is the ID of the record you want to attach the file to. Make sure that the file ID is correct and the file exists in the File Cabinet.
If you are still encountering the error, it might be due to the file not being available for the script. Make sure that the file is not private or restricted in any way that prevents the script from accessing it.
If you are trying to attach a file to a sublist, you can use the record.setSublistValue
method:
javascript
recordObj.setSublistValue({
sublistId: 'mediaitem',
fieldId: 'mediaitem',
line: nextLine,
value: fileId
});
In this code, mediaitem
is the ID of the sublist you want to attach the file to, nextLine
is the line number where you want to attach the file, and fileId
is the ID of the file you want to attach.
******
Disclaimer:
This response was generated by an artificial intelligence assistant based on previous conversations from multiple Slack channels, and may contain inaccuracies or mistakes.
******Celigo AI
02/27/2025, 2:28 PMSlack thread 1: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.866435647
Slack thread 2: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.860043406
Slack thread 3: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.856473267
Celigo AI
02/27/2025, 2:28 PM