Ad

Unable To Upload File(jpeg, Png, Pdf Etc) Into File Cabinet / Custom Record Through Online HTML Form In NetSuite

Issue:

I'm facing issue while making a custom record of the online form which I'm getting from Shopify in NetSuite and this happens because I'm unable to upload the attachment or file into the file cabinet.

Tried:

I tried to convert the file or attachment into base64 and also through blob too but unable to get the desired result because Netsuite has limitations that it can't handle more than 1000 characters and this also not a good workaround as the user has the privilege to update the image.

In Code:

Created an online HTML form and map their field with their related field and create a custom record for every record and I'm successfully able to map all the field except attachment(.png, .pdf, .png, etc)

Is there any way to get the result i.e successfully create the custom record through online HTML form or any other workaround to get the result?

Ad

Answer

For, Now whatever I checked or searched regarding upload image, maybe we are not able to upload an image into their respective field until and unless we save the particular file into the file cabinet.

So, I opt for some other method that is by going through suitelet and then POST the same form after storing the file in the file cabinet into the respective field.

Here, is my workaround code:

var dataBody = JSON.parse(request.getBody());
nlapiLogExecution("debug", "POST BLOCK", 'dataBody = ' + JSON.stringify(dataBody));
var file_folder = '2442342';
var contents = dataBody.imgdata;
var fileType = dataBody.fileType;
var date = new Date();
var newDate = date.getMilliseconds() + date.getTime();
var type = '';
var ext = '';
if ((fileType == 'plain') || (fileType == 'PLAIN')) {
    type = 'PLAINTEXT';
    ext = 'txt';
}
if ((fileType == 'pdf') || (fileType == 'PDF')) {
    type = 'PDF';
    ext = 'pdf';
}
if ((fileType == 'png') || (fileType == 'PNG')) {
    type = 'PNGIMAGE';
    ext = 'png';
}
if ((fileType == 'jpeg') || (fileType == 'JPEG')) {
    type = 'JPGIMAGE';
    ext = 'jpeg';
}
if ((fileType == 'jpg') || (fileType == 'JPG')) {
    type = 'JPGIMAGE';
    ext = 'jpg';
}

var name = newDate + '.' + ext;
nlapiLogExecution("debug", "POST BLOCK", 'File Type: ' + type);
try {
    if (type) {
        var uploadFile = nlapiCreateFile(name, type, contents);
        uploadFile.setFolder(file_folder);
        var FileId = nlapiSubmitFile(uploadFile);
        nlapiLogExecution("debug", "POST BLOCK", 'Image File ID: ' + FileId);
    }
    if (FileId) {
        warr.setFieldValue('custrecord_file_id', FileId);
    }
} catch (err) {
    nlapiLogExecution("debug", "POST BLOCK", 'Error: ' + err);
}

Hope it helps others. And one more thing you need to decrypt the data as you saving in the file cabinet but if you change the file type to jpg image in all then you may do not need to need to convert it. Thanks

Ad
source: stackoverflow.com
Ad