so sanity checking something I discovered with the...
# suitescript
r
so sanity checking something I discovered with the
N/file
module. If you use
Nfile.copy()
method, you can include the
conflictResolution
option to dictate what happens when it tries to copy a file to a folder with the same name. However if you use the
Nfile.create()
method, there is no
conflictResolution
option, and appears to ignore it if provided. Based on my testing,
Nfile.create()
will overwrite an existing file with no warnings if the name is the same. Writing a suitelet that has file uploads, and while I was planning to upload directly to a folder with that conflict resolution, it appears that will just overwrite things, and the only safe way is to 1.
Nfile.create()
the file in a temp folder 2. write contents and
Nfile.save()
3. copy file to the actual target folder with the
conflictResolution
option set 4. delete file out of temp folder unless someone has a better way of handling duplicate name file uploads into the same folder
a
i just add a timestamp of now to my filenames if i need unique names
Copy code
filename = `${filename}-${new Date.valueOf()}`
obviously you can do different stuff with the dates if you want to make it less ugly, and you know you're not gonna get multiple files per second
r
adding timestamp could work, but I'd rather keep the original filename where possible since it's cross-referenced with other data
this was more of a "am I really seeing that creating a file doesn't have any concept of name conflict" ?
a
yeah i wasn't meaning you're approach here wasn't good, just adding my 2 cents, I think you're approach is great and works around something dumb NS did pretty well 🙂
r
oh, sorry, wasn't intending to criticizing. I always appreciate the feedback. This is just another one of those "NetSuite is gonna NetSuite" things that I wanted to make sure I wasn't missing something. To solve in the short-term, I just wrote a small wrapper function in my lib file that does what I listed earlier with a provided File object. I also put a note in to consider timestamp resolution as well if that works better in other situations
👍 1
a
you're good, wasn't taking it as criticism, curious to see if anyone else has a different approach tbh
s
IIRC create is like an
upsert
(in that, it either creates or updates - entirely - the existing file). There are no 'update' functions I'm aware of for arbitrary file types. I think there's one for delimited text only?
👀 1
e
Ooo can you update the contents of a file using the
create()
method? I'm going to need to test that. I frequently see people asking how to update file contents.
r
@erictgrubaugh so I just did a test. Interestingly enough, if you perform a
Nfile.create()
method, and specify the
name
option that is the same as another file within the same folder, it will overwrite the contents, description, even overwrite the file type, but it will not generate a new Internal ID for that file, which just seems wild to me.
So yea, you could absolutely update contents and not even have to update your ID references to that file 😄
1
n
You could perform a search upfront to ascertain if the file exists and then move the existing file to another "archive" folder using it's folder property.
s
we use that characteristic frequently when deploying web apps to the file cabinet - the URLs don't change but you can update the file contents.