netsuite upload file and attach to record via REST...
# ask-stanley-ai
l
netsuite upload file and attach to record via REST api
c
AI generated answer (using internal Slack threads):
To upload a file and attach it to a record via the REST API in NetSuite, you can follow these steps: 1. First, you need to upload the file to the NetSuite File Cabinet. This can be done using a Suitelet or Restlet. Here is a sample SuiteScript 2.0 code for a Suitelet that accepts a base64 encoded file and saves it to the File Cabinet:
Copy code
javascript
/**
 * @NApiVersion 2.x
 * @NScriptType Suitelet
 */
define(['N/file', 'N/record', 'N/runtime', 'N/encode', 'N/log'], function(file, record, runtime, encode, log) {
    function onRequest(context) {
        if (context.request.method === 'POST') {
            try {
                var base64Content = context.request.parameters.fileContent;
                if (!base64Content) {
                    throw new Error("Base64 content is missing");
                }

                var decodedContent = encode.convert({
                    string: base64Content,
                    inputEncoding: encode.Encoding.BASE_64,
                    outputEncoding: encode.Encoding.UTF_8
                });
                
                var fileName = context.request.parameters.fileName;
                if (!fileName) {
                    throw new Error("File name is missing");
                }

                var fileType = file.Type.PDF;

                var uploadFile = file.create({
                    name: fileName,
                    fileType: fileType,
                    contents: decodedContent,
                    folder: 8977501 // Specify your folder ID here
                });
                var fileId = uploadFile.save();

                var fileRecord = file.load({
                    id: fileId
                });
                var fileUrl = fileRecord.url;

                context.response.write({
                    output: 'Success! File uploaded. File ID: ' + fileId + ', File URL: ' + fileUrl
                });
            } catch (e) {
                log.error('Error in Suitelet', e.toString());
                context.response.write({
                    output: 'Error: ' + e.toString()
                });
            }
        }
    }

    return {
        onRequest: onRequest
    };
});
2. After the file is uploaded, you can then attach it to a record. This can be done using the
record.attach
method in SuiteScript. Here is a sample code:
Copy code
javascript
var attachResult = record.attach({
    record: {
        type: 'file',
        id: fileId // the ID of the file you uploaded
    },
    to: {
        type: 'vendor', // the type of the record you want to attach the file to
        id: vendorId // the ID of the record you want to attach the file to
    }
});
Please note that the REST API does not currently support file upload or attachment. You would need to use SuiteScript or SuiteTalk (SOAP API) for these operations. ****** Disclaimer: This response was generated by an artificial intelligence assistant based on previous conversations from multiple Slack channels, and may contain inaccuracies or mistakes. ******
Slack thread 1: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.84590739
Slack thread 2: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.84476018
Slack thread 3: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.840743542
Please provide your feedback on this answer.
152 Views