This topic contains 4 replies, has 0 voices, and was last updated by ironside 8 years, 1 month ago.

  • Author
    Posts
  • #1767

    jonathan@velaro.com

    I’m trying to create a file with code such as the following, but when it gets to the file.create it throws an exception ‘Unexpected error’. Anyone seen this before?

    Code:
    /**
    *@NApiVersion 2.x
    *@NScriptType Restlet
    */
    define([‘N/file’], function (file) {

    function post(){
    var fileRequest = { name: ‘test’ + ‘.txt’, fileType: file.Type.PLAINTEXT, contents: ‘test’, description: ‘f’, folder: 165 };
    try {
    var resultingFile = file.create(fileRequest);
    }
    catch (ex) { debugger; }
    var fileId = resultingFile.save();
    }
    });
    This is a cached copy. Click here to see the original post.

  • #1768

    JacksonP

    I’ve created files before as a csv, It could be that you need to set the folder after creating the object

    Code:
    var fileObj = file.create({
    name: fname,
    fileType: file.Type.CSV,
    contents: resultSet.join(‘n’)
    });

    fileObj.folder = 4724267;
    fileObj.save()


    jonathan@velaro.com replied on 09/28/2016, 07:55 AM: Thanks i’ll try that. The documentation made it seem that the folder could be set at the time of creation.

  • #1769

    darrenhillconsulting

    Why are you saving inside the catch? It’ll never be reached (unless the file/create throw an exception.

    Try it like this

    Code:
    /** *@NApiVersion 2.x *@NScriptType Restlet */
    define([‘N/file’], function (file) {
    var fileRequest = { name: ‘test’ + ‘.txt’, fileType: file.Type.PLAINTEXT, contents: ‘test’, description: ‘f’, folder: 165 };
    try {
    var resultingFile = file.create(fileRequest);
    var fileId = resultingFile.save();
    } catch (ex) {
    debugger;
    } //TODO: Only put code in here to handle the exception/error.
    });


    jonathan@velaro.com replied on 09/28/2016, 07:57 AM: Save isn’t in the catch, only thing in the catch is debugger; Error is occurring at file.create, not in the save.

  • #1770

    darrenhillconsulting

    Jonathan,

    The bigger issue is, your code has no valid entry point


    jonathan@velaro.com replied on 09/28/2016, 08:06 AM: Ah, guess my example wasn’t 100% correct, i have it wrapped in an entry point function name ‘post’. I’ll update my sample.

  • #1771

    ironside

    You need to define the entrypoints – which means you need to return something from your function(file)…

You must be logged in to reply to this topic.