Прикрепление записей SuiteScript: изучение методов и примеров кода для удобного вложения

В SuiteScript NetSuite функция «прикрепления записей» позволяет легко прикреплять файлы к записям. Эта функция особенно полезна для управления вложениями, такими как счета-фактуры, квитанции, контракты и т. д. В этой статье блога мы рассмотрим различные методы и приведем примеры кода, чтобы продемонстрировать, как можно использовать SuiteScript для беспрепятственной обработки вложений.

Метод 1: использование метода attach
Метод attach— это самый простой способ прикрепить файлы к записям в SuiteScript. Вот пример того, как его использовать:

/
 * Attaches a file to a record using the attach method.
 * @param {string} recordType - The internal ID of the record type.
 * @param {string} recordId - The internal ID of the record.
 * @param {string} fileName - The name of the file to be attached.
 * @param {string} fileContent - The content of the file to be attached.
 */
function attachFileToRecord(recordType, recordId, fileName, fileContent) {
  var fileObj = nlapiCreateFile(fileName, 'PLAINTEXT', fileContent);
  fileObj.setFolder(-4); // Set the folder ID where the attachment will be stored (-4 represents the SuiteScripts folder).
  fileObj.setIsOnline(true); // Set the file to be available online.
  var attachedFileId = nlapiAttachRecord('file', fileObj, recordType, recordId);
  nlapiLogExecution('DEBUG', 'Attachment ID', attachedFileId);
}

Метод 2: использование метода transform
Метод transformможно использовать для прикрепления существующих файлов из одной записи к другой. Вот пример:

/
 * Attaches files from one record to another using the transform method.
 * @param {string} fromRecordType - The internal ID of the source record type.
 * @param {string} fromRecordId - The internal ID of the source record.
 * @param {string} toRecordType - The internal ID of the target record type.
 * @param {string} toRecordId - The internal ID of the target record.
 */
function attachFilesFromRecord(fromRecordType, fromRecordId, toRecordType, toRecordId) {
  var attachedFiles = nlapiTransformRecord(fromRecordType, fromRecordId, toRecordType, toRecordId);
  nlapiLogExecution('DEBUG', 'Attached Files', attachedFiles);
}

Метод 3. Использование метода uploadFile
Метод uploadFileобеспечивает удобный способ загрузки файлов и прикрепления их к записям. Вот пример:

/
 * Uploads a file and attaches it to a record using the uploadFile method.
 * @param {string} recordType - The internal ID of the record type.
 * @param {string} recordId - The internal ID of the record.
 * @param {string} filePath - The local file path of the file to be uploaded.
 */
function uploadAndAttachFile(recordType, recordId, filePath) {
  var fileObj = nlapiLoadFile(filePath);
  fileObj.setFolder(-4); // Set the folder ID where the attachment will be stored (-4 represents the SuiteScripts folder).
  fileObj.setIsOnline(true); // Set the file to be available online.
  var attachedFileId = nlapiAttachRecord('file', fileObj, recordType, recordId);
  nlapiLogExecution('DEBUG', 'Attachment ID', attachedFileId);
}

В этой статье мы рассмотрели различные способы прикрепления файлов к записям в SuiteScript. Мы рассмотрели метод attachдля создания и прикрепления новых файлов, метод transformдля прикрепления существующих файлов из одной записи к другой и метод uploadFile. для загрузки и прикрепления файлов. Используя эти методы, вы можете эффективно управлять вложениями в среде NetSuite.

Не забудьте обратиться к документации NetSuite SuiteScript API для получения более подробной информации и дополнительных методов, связанных с вложениями записей. Приятного кодирования!