I created this helper-function that gets the folde...
# suitescript
w
I created this helper-function that gets the folderId from a path and if it doesn't exist, it creates the full path. Is there perhaps a better/faster way to handle this dynamically? I experimented with finding it with the full path using a BUILTIN.HIERARCHY() but it was really slow. (edit: did a compromise and used BUILTIN.HIERARCHY() together with name, same speed but a bit more compact)
Haha, make up your mind
b
looks like it does more queries than necessary, if ``My Test Folder/with/*`*` does not exist, then
My Test Folder/with/some/
does not as well
wrote it backwards the first time around
w
True, but then its a matter of optimising for expecting that the full hierarchy doesn’t exist compared to hoping that most of it exists.
But there isn’t some API for it that I’m unaware of?
If the folder exists it’s only one query in this way. If only the last part doesn’t exist doing it backwards, then it will have to do as many queries as there are levels in the hierarchy
b
if you really wanted to optimize the queries, you only need to do 1
w
The query is pretty fast. 30ms in debugger.
b
you dont need to do 1 query for every path, you can do 1 query for all paths at the same time
w
What did you have in mind?
The most time consuming in the function is to create several folders (if necessary).
b
you can write the query using or, for example, if the folder path is
My Test Folder
, or
My Test Folder'/with
other than that, you are going to be creating a single folder at a time, no known way to create multiple unless you are working in the ui uploading zips
w
Running builtin.hierarchy() on the full table with only a where on that column took 7 seconds in our environment
By constraining the query to a folder with the
name
really helped it
I could probably constrain into folders that has a name in(folderInThePath)
b
you would still be constraining with both the folder name and folder path
but would be using
or
to do multiple folder names and paths at the same time
you already know what all the folder names and paths are at the beginning
w
Copy code
where
  name in(‘My Test Folder’,’with’,’some’,’subfolder’)
  And builtin.hierarchy(parent, ‘self_display’) in([the different paths])
Were you thinking about something like that?
FYI, my usage of this was not to dynamically create a new folder for every record/execution. It was more to dynamically find/create the single big folder to store a bunch of files in. But I suppose it could be applied to similarly how drag and drop can create a folder for each records files.
b
Copy code
SELECT id
FROM MediaItemFolder
WHERE
     name = 'subFolder2' AND BUILTIN.HIERARCHY(parent,'SELF_DISPLAY') = 'My Test Folder/with/some/subFolder2' OR
     name = 'some' AND BUILTIN.HIERARCHY(parent,'SELF_DISPLAY') = 'My Test Folder/with/some/'
is what i would have done, though i got lazy and only did 2
w
Ok, got it. So then I would only need one query and then, from the result, programmatically figure out which are missing and needs to be created.
Might try it out to see how my coding would make that look.
b
the recursive solution is not helping, it essentially makes you work from the right to the left
where in reality left to right is what you want to do
w
Do you really, though? Isn’t pretty likely that the parent folders already exists? And if it’s a root folder, the point it moot.
If it is with js code it probably doesn’t matter anyways
But yes, worst case is that no folder exists out of the x levels, and then it needs to do x-number of 30ms extra queries.
b
you can probably just keep the code the same and just change how getFolderIdFromPath works, but once you find a missing folder, every folder after it is missing, you dont need to do any other checks
w
running a query with the 4 or's for all levels in a four level hierarchy takes ~2.5x in exec time in a very limited test.
2.5x compared to one of the four separate queries. 33% less compared to running all four separate queries.
I could also optimize for that the folder do exist by checking that first and then going from left to right if it doesn't
oooor just create the folder manually in production and set the ID in a script-parameter, but where's the fun in that.