This topic contains 8 replies, has 0 voices, and was last updated by leacelosacolumna 6 years, 9 months ago.

  • Author
    Posts
  • #18048

    timgordon

    Not sure how to do this in Suitescript 2.0, can anyone post a code snippet? I have the folder id, just want to list all files in that folder. If you have a code snippet it would be much appreciated.
    This is a cached copy. Click here to see the original post.

  • #18049

    timgordon

    Here is a SS1 scheduled script that does this. How do I do the same but in SS2?

    Code:
    function main()
    {
    var folderid = ‘603907’;

    var filters = new Array();
    filters[0] = new nlobjSearchFilter(‘internalid’, null, ‘is’, folderid);

    var columns = new Array();
    var filename = new nlobjSearchColumn(‘name’, ‘file’);
    var fileid = new nlobjSearchColumn(‘internalid’, ‘file’);

    columns[0] = filename;
    columns[1] = fileid;

    var searchResult = nlapiSearchRecord(‘folder’, null , filters , columns);

    if(searchResult) {
    for (var i = 0 ; i < searchResult.length; i++) {
    thisfile = searchResult[i].getValue(filename);
    thisfileid = searchResult[i].getValue(fileid);
    nlapiLogExecution('DEBUG', 'Test', thisfileid + ' ' + thisfile);
    }
    }

    }

  • #18050

    timgordon

    Doing some more research on this and it seems that in SS2 a search for documents is not possible? Can anyone confirm? The syntax should be like this and of course this works when you create a saved search in the UI.

    Code:
    var mySearch = search.create({
    type: Search.Type.DOCUMENT,
    filters : myFilters,
    columns : myColumns
    });

  • #18051

    j.j

    i did not understand the issue – it is possible in both SS1 and SS2, also you can search on folder object and get all files of that folder, alternatively you can search documents and set filter to parent folder.

  • #18052

    timgordon

    Thanks for confirmation, I must be missing something. Can you post a code snippet of how to search documents and set filter to parent folder? This is the first thing I tried but Search.Type.DOCUMENT doesn’t seem to be a valid search type.

  • #18053

    JohnCCole

    I don’t see Document or File in the Search.Type enum in the help. Try using the string ‘file’ apposed to search.Type.Document.

  • #18054

    budysutjijati

    Hi Tim,

    Just create a saved search first and make sure that in the results tab you select the columns that you need will be displayed of that particular folder. For example select the file name and file type, or anything you would like to have included. Then write down the internal id of the saved search. Now create a custom Suite Script that calls that saved search like so:

    Code:
    function runSearchAndFetchResult(context) {
    var mySearch = search.load({
    id: PLACE HERE THE INTERNAL ID OF YOUR SAVED SEARCH
    });
    var searchResult = mySearch.run().getRange({
    start: 0,
    end: 100
    });
    return searchResult;
    }
    Now all the results are stored in the searchResult object. Then you can do anything you wish with that object since the object will hold all the columns that you’ve selected earlier in the saved search. This way you can create a list of a particular folder in the file cabinet.

    Good luck!

  • #18055

    timgordon

    Thanks, yes that is the workaround that I used. Works fine but just feels like it should be possible to create the search in the script!

  • #18056

    leacelosacolumna

    In case you’re still looking for the snippet for ad-hoc search:

    Code:
    require([‘N/search’], function(search) {
    var documentSearch = search.create({
    type: ‘file’,
    ‘columns’: [‘name’],
    filters: [‘folder’, ‘anyof’, null, 78239]
    });
    var results = documentSearch.run().getRange({
    start: 0,
    end: 10
    });
    console.log(results[0].getValue(‘name’));
    });

You must be logged in to reply to this topic.